Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the time processing in Java8

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to understand the time processing in Java8. Many people may not understand it very well. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something from this article.

Preface

Java8 draws on many of the advantages of the third-party date library joda.

Java.time package

Class name describes Instant timestamp Duration duration, time difference LocalDate contains only date, for example: 2020-05-20LocalTime only contains time, for example: 13:14:00LocalDateTime contains date and time, for example: 2020-05-20 13:14:00Period time zone offset ZoneOffset, such as: + 8:00ZonedDateTime time Clock clock with time zone, such as getting the current time in New York, USA.

Java.time.format package

Class name description DateTimeFormatter time format

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

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

Here are some of the main classes

LocalDateTime

This is what we usually use to denote 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, in different time zones, reflected in the time axis is different.

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

ZonedDateTime

ZonedDateTime can be understood as adding a time zone to LocalDateTime, so it can be reflected in the timeline.

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

Daylight saving time is what is not carried out here, the specific can be checked online. Look at the daylight saving time that begins in 1986 in our country.

To put it simply, set the clock back one hour at the beginning of daylight saving time. The practice corresponding to the daylight saving time that our country began in 1986 is to set the clock directly to 3 o'clock in the morning when the clock reaches 2 am on the first Sunday in mid-April every year. That's the difference between 1: 00 a. M. and 3: 00 a. M.

Since 1986 was implemented, daylight saving time in 1986 began on May 4, 1986.

Let's look at the beginning of daylight saving time in 1987.

According to China's daylight saving time policy at that time, 1987 should have begun on April 12, 1987. Specifically, an hour after 01:00:00 on 1987-04-12, the time should be 1987-04-12 03:00:00.

LocalDateTime localDateTime = LocalDateTime.of (1987, 4, 12, 1, 0, 0, 0); System.out.println (localDateTime); System.out.println (localDateTime.plusHours (1))

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

This is also easy to understand, because LocalDateTime does not include the time zone, the daylight saving time of 02:00:00 1987-04-12 is only for China, not globally unified. It would be a mistake if 02:00:00 on 1987-04-12 will directly become 1987-04-12 03:00:00 in any country other than China.

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

By executing the above code, you can see that when 1987-04-12 01:00:00 increases by one hour, the time becomes 1987-04-12 03:00:00. This will tell the story.

At the same time, you can see from the print result that the time zone automatically changed from + 08:00 [Asia/Shanghai] to + 09:00 [Asia/Shanghai].

Instant

Instant represents an instantaneous time on the timeline, simply the number of seconds, milliseconds, and so on since the 1970-01-01T00:00:00Z era.

Both ZonedDateTime and Instant can correspond 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 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 ()) / / 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 daylight saving time is 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 = newDate (); 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 (); after reading the above, do you have any further understanding of how to understand the time processing in Java8? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report