Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the function of LocalDateTime in java8

Shulou Source: shulou.com Published: 2022-06-02 10:27:11 09月30日 Update

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.

Tags: Time character string type security three object hour data format thread trouble help support function clarity precision almost code content Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Microsoft macOS Huawei NVidia