Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get all the dates of this week and last week in java

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

It is believed that many inexperienced people don't know what to do about how to get all the dates of this week and last week in java. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

/ * get the date range of the week (Monday and Sunday dates) based on the current date * / public static Map getTimeInterval (Date date) {Map map = new HashMap (); Calendar cal = Calendar.getInstance (); cal.setTime (date) / / determine whether the date to be calculated is Sunday, and if so, subtract one day to calculate Saturday, otherwise there will be problems, int dayWeek = cal.get (Calendar.DAY_OF_WEEK) in the next week; / / get the day of the week if (1 = = dayWeek) {cal.add (Calendar.DAY_OF_MONTH,-1) } / / set the first day of the week, according to Chinese custom, the first day of the week is Monday cal.setFirstDayOfWeek (Calendar.MONDAY); / / get the day of the week int day = cal.get (Calendar.DAY_OF_WEEK) / according to the rules of the calendar, subtract the difference between the day of the week and the first day of the week from the current date cal.add (Calendar.DATE, cal.getFirstDayOfWeek ()-day); Long imptimeBegin = cal.getTime (). GetTime (); cal.add (Calendar.DATE,6); Long imptimeEnd = cal.getTime (). GetTime (); map.put ("start", imptimeBegin) Map.put ("end", imptimeEnd); return map;} / * * get last week's date range based on the current date (last week's Monday and Sunday dates) * / public static Map getLastTimeInterval (Date date) {Map map = new HashMap (); Calendar calendar1 = Calendar.getInstance (); Calendar calendar2 = Calendar.getInstance (); calendar1.setTime (date) Calendar2.setTime (date); int dayOfWeek = calendar1.get (Calendar.DAY_OF_WEEK)-1; if (dayOfWeek calBegin.getTime (). GetTime ()) {/ / add or subtract a specified amount of time calBegin.add (Calendar.DAY_OF_MONTH, 1) from a given calendar field according to the rules of the calendar. LDate.add (calBegin.getTime (). GetTime ());} return lDate;} public static void main (String [] args) {Long t = SalesTimeUtil.dateToStamp ("20191028", "yyyyMMdd"); / / this week's time range System.out.println (getTimeInterval (new Date (t) / / Last week time range System.out.println (getLastTimeInterval (new Date (t)); System.out.println (formatYyyyMMdd (getWeekStart (); System.out.println (formatYyyyMMdd (getWeekEnd (); System.out.println (formatYyyyMMdd (getLastWeekStart (); System.out.println (formatYyyyMMdd (getLastWeekEnd () } {start=1572192000000, end=1572710400000} {lastend=1572105600000, laststart=1571587200000} this week: 20191028 Magna 20191103 Last week: 20191021 Magna 2019102772234212622 purge 2019102815723612622 purge 2019102815723612622Vl2019103015724934622Vl20191031572579812622Fre20191071572666222622pur2019110215727512622pur20191103 have you mastered the method of how to get all the dates of this week and last week in java? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report