Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

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

Shulou Source: shulou.com Published: 2022-06-02 10:33:52 09月28日 Update

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!

Tags: Date time week calendar range question content interval method more rule helpless for this reason field difference location skill article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Shulou Tech Info Apple Linux Huawei