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 use the time interface LocalDateTime in Java8

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

Share

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

This article mainly introduces "how to use the time interface LocalDateTime in Java8". In daily operation, I believe many people have doubts about how to use the time interface LocalDateTime in Java8. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use the time interface LocalDateTime in Java8". Next, please follow the editor to study!

Introduction of new time and date API commonly used and important objects

ZoneId: time zone ID, which is used to determine the rules for the conversion between Instant and LocalDateTime

Instant: used to represent a point on the timeline (instantaneous)

LocalDate: indicates a date without a time zone, LocalDate is immutable and thread-safe

LocalTime: indicates a time without a time zone, LocalTime is immutable and thread-safe

LocalDateTime: indicates a date and time without a time zone, LocalDateTime is immutable and thread safe

Clock: used to access the current time, date, time, and time zone

Duration: the amount of time (length) in seconds and nanoseconds, used to calculate the "time" interval between two dates

Period: used to calculate two "date" intervals

Among them, LocalDate, LocalTime and LocalDateTime are the basic objects in the new API, and most operations are carried out around these objects, so it is necessary to make clear:

LocalDate: a date object that contains only the year, month and day

LocalTime: a time object containing only time minutes and seconds

LocalDateTime: a date object that also contains the year, month, day, hour, and second

II. New classes of date and time added to JDK 8

Java 8 introduces a new API for Date and Time to address the shortcomings of old java.util.Date and java.util.Calendar.

The core classes in the Java 8 time class library, such as LocalDate, LocalTime, LocalDateTime, ZonedDateTime, Period, Duration and their api.

1. Get the time

@ Testpublic void testGetDate () {/ * * 1. Get time * / LocalDateTime localDateTime = LocalDateTime.now (); System.out.println ("current time:" + localDateTime); System.out.println ("current year:" + localDateTime.getYear ()); System.out.println ("current month:" + localDateTime.getMonth ()); System.out.println ("current day:" + localDateTime.getDayOfMonth ()); System.out.println ("current tense:" + localDateTime.getHour ()) System.out.println ("current score:" + localDateTime.getMinute ()); System.out.println ("current second:" + localDateTime.getSecond ());}

Console output:

Current time: 2021-05-17T14:20:49.680

Current year: 2021

Current month: MAY

Current date: 17

Current tense: 14

Current score: 20

Current second: 49

2. Date modification

@ Testpublic void testUpdateLocalDateTime () {/ * * 2. Date modification * / LocalDateTime localDateTime1 = LocalDateTime.now (); localDateTime1.plusYears (50); / / increase 50 years localDateTime1.minusMonths (6); / / decrease 6 months localDateTime1.withYear (2100); / / modify year localDateTime1.withMonth (6); / / modify month}

3. Date and date time formatting

@ Testpublic void testFormatLocalDateTime () {/ * * 3. Date format * / LocalDateTime rightNow = LocalDateTime.now (); rightNow.format (DateTimeFormatter.ISO_DATE); / / format: yyyy-MM-dd rightNow.format (DateTimeFormatter.BASIC_ISO_DATE); / / format: yyyyMMdd rightNow.format (DateTimeFormatter.ofPattern ("yyyy/MM/dd")); / / format: yyyy/MM/dd rightNow.format (DateTimeFormatter.ofPattern ("yyyy-MM-dd HH:mm:ss")) / / format: yyyy-MM-dd HH:mm:ss System.out.println ("YYYY-MM-DD =" + rightNow.format (DateTimeFormatter.ISO_DATE)); System.out.println ("YYYYMMDD =" + rightNow.format (DateTimeFormatter.BASIC_ISO_DATE)); System.out.println ("YYYY/MM/DD =" + rightNow.format (DateTimeFormatter.ofPattern ("yyyy/MM/dd") System.out.println ("YYYY-MM-DD HH:mm:ss =" + rightNow.format (DateTimeFormatter.ofPattern ("yyyy-MM-dd HH:mm:ss"));}

Console output:

YYYY-MM-DD = 2021-05-17

YYYYMMDD = 20210517

YYYY/MM/DD = 2021-05-17

YYYY-MM-DD HH:mm:ss = 2021-05-17 14:20:49

4. Date and time reverse analysis

@ Testpublic void testParseLocalDateTime () {/ * * 4. Time anti-parsing * / LocalDateTime localDateTime = LocalDateTime.parse ("2021 murmur05 murmur17 14:20:49", DateTimeFormatter.ofPattern ("yyyy--MM--dd HH:mm:ss")); System.out.println ("time after string anti-parsing:" + localDateTime);}

Console output:

The time after string inverse parsing is: 2021-05-17T14:20:49

5. Date formatting

@ Testpublic void testFormatLocalDate () {/ * * 5. Date format * / LocalDate localDate = LocalDate.now (); localDate.format (DateTimeFormatter.ISO_DATE); / / format: yyyy-MM-dd localDate.format (DateTimeFormatter.BASIC_ISO_DATE); / / format: yyyyMMdd localDate.format (DateTimeFormatter.ofPattern ("yyyy/MM/dd")); / / format: yyyy/MM/dd System.out.println ("yyyy-MM-dd =" + localDate.format (DateTimeFormatter.ISO_DATE)) System.out.println ("yyyMMdd =" + localDate.format (DateTimeFormatter.BASIC_ISO_DATE)); System.out.println ("yyyy/MM/dd =" + localDate.format (DateTimeFormatter.ofPattern ("yyyy/MM/dd");}

Running result:

Yyyy-MM-dd = 2021-05-18

YyyMMdd = 20210518

Yyyy/MM/dd = 2021-05-18

6. Time formatting

@ Testpublic void testFormatLocalTime () {/ * * 6. Time format * / LocalTime localTime = LocalTime.now (); localTime.format (DateTimeFormatter.ISO_TIME); / / format: yyyy-MM-dd localTime.format (DateTimeFormatter.ISO_LOCAL_TIME); / / format: yyyyMMdd localTime.format (DateTimeFormatter.ofPattern ("HH:mm:ss")); / / format: yyyy/MM/dd localTime.format (DateTimeFormatter.ofPattern ("HH/mm/ss")) / / format: yyyy/MM/dd System.out.println ("HH:mm:ss =" + localTime.format (DateTimeFormatter.ISO_TIME)); System.out.println ("HH:mm:ss =" + localTime.format (DateTimeFormatter.ISO_LOCAL_TIME)); System.out.println ("HH:mm:ss =" + localTime.format (DateTimeFormatter.ofPattern ("HH:mm:ss") System.out.println ("HH/mm/ss =" + localTime.format (DateTimeFormatter.ofPattern ("HH/mm/ss");} at this point, the study on "how to use the time interface LocalDateTime in Java8" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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