How to get the last Sunday before current date?

后端 未结 7 1191
花落未央
花落未央 2021-02-12 15:07

I have the following code for getting the last Sunday before the current date:

Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.WEEK_OF_YEAR, cale         


        
7条回答
  •  野性不改
    2021-02-12 15:58

    java.time.temporal.TemporalAdjuster

    This can be easily achieved by a TemporalAdjuster implementation found in TemporalAdjusters along with the DayOfWeek enum. Specifically, previous​(DayOfWeek dayOfWeek).

     LocalDate
     .now()
     .with(
         TemporalAdjusters.previous( DayOfWeek.SUNDAY )
     ) ;
    

    If today is Sunday and you would like the result to be today rather than a week ago, call previousOrSame​(DayOfWeek dayOfWeek).

     LocalDate
     .now()
     .with(
         TemporalAdjusters.previousOrSame( DayOfWeek.SUNDAY )
     ) ;
    

    These classes are built into Java 8 and later, and built into Android 26 and later. For Java 6 & 7, most of the functionality is back-ported in ThreeTen-Backport. For earlier Android, see ThreeTenABP.

提交回复
热议问题