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

What is the function of LocalDateTime in java8

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

What is the role of LocalDateTime in java8, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

two。 A brief introduction to LocalDateTime

Java's Date,Calendar type is not very convenient to use, and the Date class (it is said) has many drawbacks such as thread safety. At the same time, if it is not encapsulated, it will be particularly troublesome each time it is used. So Java8 launched a thread-safe, simple, highly reliable time package. And the LocalDateTime type is also supported in the database, which makes time easier when storing data. Java8's new launch includes three related time types: LocalDateTime, month, day, ten seconds; LocalDate date; LocalTime time; and the methods of all three packages are similar.

3. Simple daily use of LocalDateTime ①. Convert to and from a string

We all know that we used to format Date with the help of SimpDateFormart objects. It's not troublesome to operate, but it's not so easy. The following paste the code LocalDateTime and string operation

/ / format the current time as a string pattern to LocalDateTime.now (). Format (DateTimeFormatter.ofPattern (pattern)), such as yyyy-MM; / / convert the time string to LocalDateTime LocalDateTime.parse (dateTimeStr, DateTimeFormatter.ofPattern (pattern)); ②. Get the interval between two time periods (days, hours.) Count Duration duration = Duration.between (startLocalDateTime,endLocalDateTime); / / get the interval days duration.toDays (); / / obtain the interval hour duration.toHours (); / / obtain the interval minute duration.toMinutes (); / / obtain the interval seconds uration.getSeconds () / / obtain the interval millisecond duration.toMillis (); Period period = Period.between (startLocalDateTime.toLocalDate (), endLocalDateTime.toLocalDate ()); / / obtain the interval year period.getYears (); / / obtain the interval month period.getMonths () Period.toTotalMonths (); ③. Add or subtract / increase plusXX such as: increase one month LocalDateTime.now (). PlusMonths (1); / / decrease minusXX such as: decrease one month LocalDateTime.now (). MinusMonths (1); ④. Get the last day of the month LocalDateTime endLocalDateTime = now.with (TemporalAdjusters.lastDayOfMonth ()); ⑤. LocalDateTime and Date interchange / / LocalDateTime to Date//ZoneId is the time zone where the system default time zone ZoneId zone = ZoneId.systemDefault (); LocalDateTime localDateTime = LocalDateTime.now (); / / Instant is a time object accurate to nanosecond Instant instant = localDateTime.atZone (zone). ToInstant (); Date date = Date.from (instant); / / Date to LocalDateTimeDate date = new Date (); Instant instant = date.toInstant (); ZoneId zone = ZoneId.systemDefault (); LocalDateTime localDateTime = LocalDateTime.ofInstant (instant, zone) Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, 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

Internet Technology

Wechat

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

12
Report