java Calendar setFirstDayOfWeek not working

后端 未结 3 610
不知归路
不知归路 2020-12-19 11:36

Here is the real calendar now:

   March 2015       
Su Mo Tu We Th Fr Sa  
 1  2  3  4  5  6  7  
 8  9 10 11 12 13 14  
15 16 17 18 19 20 2         


        
3条回答
  •  暖寄归人
    2020-12-19 12:15

    tl;dr

    LocalDate.of( 2015 , Month.MARCH , 24 )  // `LocalDate` object for 2015-03-24.
             .getDayOfWeek()                 // DayOfWeek.TUESDAY constant object
             .getValue()                     // 2
    

    Avoid legacy date-time classes

    Calendar is a ugly mess, as are its sibling classes. Fortunately these old date-time classes are now legacy, supplanted by the java.time classes.

    ISO 8601

    If you want Monday as the first day of the week, Sunday the last, numbered 1-7, then use the ISO 8601 calendar used by default in the java.time classes.

    DayOfWeek

    The DayOfWeek enum hold predefined objects for each of those ISO days of the week. You can interrogate for its number if need be, though generally better to pass around objects of this enum rather than mere integers.

    LocalDate

    The LocalDate class represents a date-only value without time-of-day and without time zone.

    LocalDate ld = LocalDate.of( 2015 , Month.MARCH , 24 );
    DayOfWeek dow = ld.getDayOfWeek();
    int value = dow.getValue(); // 1-7 for Monday-Sunday. But often better to use the `DayOfWeek` object rather than a mere integer number.
    

    For working with other definitions of a week where Monday is not day number one, see the WeekFields class.


    About java.time

    The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

    The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

    To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

    Where to obtain the java.time classes?

    • Java SE 8 and SE 9 and later
      • Built-in.
      • Part of the standard Java API with a bundled implementation.
      • Java 9 adds some minor features and fixes.
    • Java SE 6 and SE 7
      • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
    • Android
      • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
      • See How to use ThreeTenABP….

    The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

提交回复
热议问题