Date handling in old Java was a very awkward but necessary API for well-known reasons.

Joda Datetime objects are often confusing and error-prone.

It’s time for JDK 8 or later. Do we still need to use Joda?

conclusion

Officially, the Joda project is no longer actively maintained.

Joda-time is no longer in active development except to keep timezone data up to date. From Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) – a core part of the JDK which replaces this project.

Joda is no longer actively in development, but is doing some time data maintenance.

You should be able to do all this using java.time.

Simply put, if you are using JDK 8 or later, you can slowly remove Joda from your project, but if you are using an earlier version, you will still need to import it.

According to the official code, it has been over a year since any code was submitted.

It’s a bit of a shame, because maybe a lot of people are used to DateTime coming in and doing a bunch of calculations.

Experience with

Java.time for JDK if you need to get the current time.

One of the core classes of the Date-time API is the Instant class, which represents the start of nanoseconds on the timeline.

The value returned by the Instant class computes the time from the first second of January 1, 1970 (1970-01-0t00:00:0z), also known as the EPOCH. Moments before a period have negative values, and moments after a period have positive values.

Instant does not contain the units of year, month, day, etc. However, it can be converted to LocalDateTime or ZonedDateTime. Here is how to convert an Instant + default time zone to a LocalDateTime.

Either a ZonedDateTime or OffsetTimeZone object can be converted into an Instant object because both map to the exact moment on the timeline. However, the opposite is not the case.

To convert an Instant object into a ZonedDateTime or OffsetDateTime object, you need to provide the time zone or time zone offset information.

We can examine the following code snippets:

        Instant instant = Instant.now();
        System.out.println("------");
        System.out.println(instant.toString());
        System.out.println(instant.truncatedTo(ChronoUnit.SECONDS).toString());
        System.out.println(instant.toString());

        ZonedDateTime zonedDateTime = ZonedDateTime.now();
        System.out.println("------");
        System.out.println(zonedDateTime.toString());
        System.out.println(zonedDateTime.toInstant().toString());

        Date date = new Date();
        System.out.println("------");
        System.out.println(date.toString());
        System.out.println(date.toInstant().toString());
Copy the code

The corresponding output is:

------ 2021-08-13T18:20:18.977845200z 2021-08-13T18:20:18z 2021-08-13T18:20:18.977845200z ------ 2021-08-13T14:20:18.992847200-04:00[America/New_York] 2021-08-13T18:20:18.992847200z ------ Fri Aug 13 14:20:18EDT 2021 The 2021-08-13 T18: prepare. 993 zCopy the code

For the above understanding, the Instant object gets the current UTC time, in which you can format the display using the truncatedTo method if you don’t want milliseconds to be displayed.

The Instant class is immutable, so you cannot alter an Instant object even after truncatedTo. Unless you get a new one.

Creating an object with ZonedDateTime is the same as creating an object with Date.

For example, if we are in Eastern Time, the above two objects will get the current computer time, and the above two objects also provide a toInstant() method that will display the current time corresponding to the UTC time.

You can see that in the output.

Some of the specific transformations will be listed step by step in subsequent learning articles.

To sum up:

  • Instant is immutable after new, always pointing to the UTC time.
  • If you need to convert the local time with the time zone, you need to add the time zone offset at the time of conversion.
  • Date and ZonedDateTime create the time for the local computer with the time zone.
  • Both Date and ZonedDateTime can be converted to Instant without adding a time zone offset.

www.ossez.com/t/java-joda…