Joda-Time: DateTime, DateMidnight and LocalDate usage

后端 未结 2 951
一个人的身影
一个人的身影 2021-02-01 15:49

Joda-Time library includes different datetime classes

DateTime - Immutable replacement for JDK Calendar
DateMidnight - Immutable class representin

2条回答
  •  感情败类
    2021-02-01 15:57

    The Answer by leonbloy is correct and vitally important. I am merely translating to the java.time classes that replace the Joda-Time project.

    java.time

    Specific moment

    For a specific moment on the timeline:

    • Always in UTC is represented by Instant.
    • Assigned an offset-from-UTC is represented by OffsetDateTime.
    • Assign a full time zone rather than mere offset is represented by ZonedDateTime.

    These all replace the Instant & DateTime class in Joda-Time. These java.time classes all have a resolution of nanoseconds versus the milliseconds used by Joda-Time.

    Midnight versus Start-of-day

    For midnight, the Joda-Time project concluded “midnight” is a vague and unproductive concept. The midnight-related classes and midnights were all deprecated in later versions of Joda-Time, replaced with the practical concept of “first moment of the day”.

    The java.time classes took the same lesson, using a "first moment of the day" approach. Look for atStartOfDay methods on java.time classes such as LocalDate.

    Never assume a day starts at 00:00. Anomalies such as Daylight Saving Time (DST) mean the day may start at other times such as 01:00.

    ZonedDateTime zdt = 
        LocalDate.of( 2017 , Month.MARCH , 12 )                    // Instantiate a date-only value without time zone.
                 .atStartOfDay( ZoneId.of( "America/Havana" ) ) ;  // Cuba jumps from 00:00 to 01:00 on Spring DST cut-over.
    

    For example, see how Cuba starts the day at 1 AM on their Spring DST cut-over.

    zdt: 2017-03-12T01:00-04:00[America/Havana]

    Unzoned

    For representing the vague idea of possible moments over a range of about 26-27 hours but not an actual moment on the timeline, use LocalDateTime. This class purposely lacks any offset-from-UTC or time zone.

    LocalDateTime ldt = LocalDateTime.of( 2017 , Month.JANUARY , 23 , 1 , 2 , 3 , 0 ) ;
    

    If your business context implies a specific time zone, you can apply it to get a ZonedDateTime.

    ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
    ZonedDateTime zdt = ldt.atZone( z ) ;  // Determine a specific point on timeline by providing the context of a time zone.
    

    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, Java 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 Java 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.

提交回复
热议问题