Time processing in Java 8

Posted by dvt85 on Sat, 08 Jan 2022 04:27:18 +0100

Time processing in Java 8

Java 8 provides a new time processing framework, where you can completely discard the previous Date and Calendar.

The use of specific API s is relatively simple. There will be no introduction here.

Here are some main classes


We usually use this to represent the date and time. For example, localdatetime Now () can get the current date and time based on the current default time zone.

Because there are many time zones in the world, the same date and time are reflected differently on the time axis in different time zones.

The date time of LocalDateTime type does not contain a time zone, so it cannot correspond to the timeline. The white point is that LocalDateTime cannot be converted into milliseconds since 1970-01-01T00:00:00Z era.


ZonedDateTime can be understood as adding a time zone on the basis of LocalDateTime, so it can be reflected on the timeline.

Let's take daylight saving time as an example to see the difference between LocalDateTime and LocalDateTime.

What is daylight saving time? It doesn't start here. You can check it online. Look at daylight saving time, which began in 1986.

Simply put, set the clock back one hour at the beginning of daylight saving time. The practice corresponding to the daylight saving time started in 1986 in China is to directly set the clock to 3 a.m. when the clock reaches 2 a.m. on the first Sunday in mid April every year. That's an hour between 1 a.m. and 3 a.m.

Since it was implemented in 1986, the summer time in 1986 began on May 4, 1986.

Let's look at the beginning of summer time in 1987

According to China's daylight saving time policy at that time, 1987 should begin on April 12, 1987. Specifically, one hour after 01:00:00 on April 12, 1987, the time should be 03:00:00 on April 12, 1987

        LocalDateTime localDateTime = LocalDateTime.of(1987, 4, 12, 1, 0, 0, 0);

By executing the above code, you can see that when 1987-04-12 01:00:00 is increased by 1 hour, the time is 1987-04-12 02:00:00.

This is understandable because LocalDateTime does not include a time zone. The 1987-04-12 02:00:00 daylight saving time is only Chinese and is not globally unified. It would be wrong to put it in other countries other than China if the 1987-04-12 02:00:00 would be directly changed into 1987-04-12 03:00:00.

        ZonedDateTime zonedDateTime = ZonedDateTime.of(1987, 4, 12, 1, 0, 0, 0, ZoneId.systemDefault());

By executing the above code, you can see that when 01:00:00 on April 12, 1987 is increased by 1 hour, the time becomes 03:00:00 on April 12, 1987. This can explain the problem.

At the same time, it can be seen from the print results that the time zone automatically changes from + 08:00[Asia/Shanghai] to + 09:00[Asia/Shanghai]


Instant represents an instantaneous time on the time axis. In short, it represents the number of seconds, milliseconds, etc. since 1970-01-01T00:00:00Z era

ZonedDateTime and Instant can be mapped to the timeline, so they can be transformed into each other.

        Instant instant = zonedDateTime.toInstant();
        ZonedDateTime zonedDateTime1 = instant.atZone(zonedDateTime.getZone());

Other commonly used API s for conversion between various types

        //ZonedDateTime to Instant
        Instant instant = ZonedDateTime.now().toInstant();

        //Get UTC milliseconds
        long epochMilli = instant.toEpochMilli();

        //Instant to ZonedDateTime
        ZonedDateTime zonedDateTime = instant.atZone(ZoneId.systemDefault());

        //String to ZonedDateTime
        ZonedDateTime zonedDateTime2 = ZonedDateTime.parse(zonedDateTime.toString());

        //Number of milliseconds based on UTC offset
        int totalSeconds = zonedDateTime.getOffset().getTotalSeconds();

        //Instant to LocalDateTime
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());

        //LocalDateTime to ZonedDateTime
        ZonedDateTime zonedDateTime1 = localDateTime.atZone(ZoneId.systemDefault());

        ZoneRules zoneRules = ZoneId.systemDefault().getRules();

        //Determine whether it is daylight saving time
        boolean daylightSavings = zoneRules.isDaylightSavings(instant);

        Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
        //Calendar to Instant
        Instant instant1 = calendar.toInstant();

        //Calendar to ZonedDateTime
        Calendar now = Calendar.getInstance();
        ZonedDateTime zdt = ZonedDateTime.ofInstant(now.toInstant(), ZoneId.systemDefault()));

        //Date to Instant
        Date date = new Date();
        Instant inst = date.toInstant();

        // Instant to Date
        Date newDate = Date.from(inst);

        //GregorianCalendar to ZonedDateTime
        GregorianCalendar cal = GregorianCalendar.from(ZonedDateTime.now());
        TimeZone tz = cal.getTimeZone();
        ZonedDateTime zdt1 = cal.toZonedDateTime();
        //ZonedDateTime to GregorianCalendar
        GregorianCalendar newCal = GregorianCalendar.from(zdt1);

        LocalDateTime ldt = zdt.toLocalDateTime();
        LocalDate date2 = zdt.toLocalDate();
        LocalTime time2 = zdt.toLocalTime();

For more detailed information, please refer to the official documents. https://docs.oracle.com/javase/tutorial/datetime/index.html

Topics: Java