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 time API in java8

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article is about how to use the Java 8 Time API. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

Why is it necessary to introduce a new date and time library in Java 8? In java 1.0, support for time and date relies only on the java.util.date class, which cannot represent dates and can only represent time to millisecond precision, and is less user-friendly. Date date = new Date(114,2,18);

//Print results

Tue Mar 18 00:00:00 CST 2014

In java 1.1, both Date and Calendar classes exist, increasing programmer choice confusion, and the month starts from 0. The DateForm class only supports the Date class LocalDate: provides a simple date, does not contain time information//creates an immutable date object

LocalDate date = LocalDate.now (); //Returns the current date

LocalDate date = LocalDate.of(2014,3,18); //Returns a specified date

LocalDate = LocalDate.parse("2014-03-18"); //Parses the string to generate a LocalDate object

...

//How to get date and time

int year = date.getYear(); //getYear

getDayOfMonth(); //Get the day of the month

getMonth(); //get Month

getDayOfWeek(); //get week

lengthOfMonth(); //Gets the number of days in the month

isLeapYear(); //whether it is an anniversary

...

//operation LocalDate method (part)

withYear(int year); //set year

withDayOfMonth(int day); //set day

with(ChronoFiled.MONTH_OF_YEAR,int month); //set month

plusWeek(int week); //add weeks

minusYears(int year); //subtract year year

plus(int month,ChronoUnit.MONTHS); //modify month

...

//Method of comparison

isEqual(); //is equal

isBefore(); //whether before

isAfter(); //whether after

LocalTime: provides a simple time without date information//creates a time object

LocalTime time = LocalTime.now (); //current time

LocalTime time = LocalTime.of(13,23,46); //custom time

LocalTime time = LocalTime.parse("13:23:46"); //parse time

...

//How to get time

getHour(); //getHour

getMinute(); //min

getSecond(); //seconds

...

//method of operating time

plusHour(); //hour

plusMinute(); //operation minutes

plusSecond(); //seconds

...

//Method of comparison

isEqual(); //is equal

isBefore(); //whether before

isAfter(); //whether after

``

LocalDateTime: Integration of LocalDate and LocalTime//Create datetime object

LocalDateTime dateTime = LocalDateTime.now (); //current ah dateTime

LocalDateTime dateTime = LocalDateTime.of(LocalDate.now, LocalTime.now); //Specify dateTime

LocalDateTime dateTime = LocalDateTime.parse("2018-08-08 08:08:08"); //parse time

//How to get date and time

getYear();

getDayOfMonth(); //Get the day of the month

getMonth(); //get Month

getDayOfWeek(); //get week

getHour(); //getHour

getMinute(); //min

getSecond(); //seconds

...

//How to operate date and time

withYear(int year); //set year

withDayOfMonth(int day); //set day

with(ChronoFiled.MONTH_OF_YEAR,int month); //set month

plusHour(); //hour

plusMinute(); //operation minutes

plusSecond(); //seconds

...

//Method of comparison

isEqual(); //is equal

isBefore(); //whether before

isAfter(); //whether after

``

Duration: Peroid is used to calculate the difference between two LocalDates Duration = Duration.between(LocalTime.of(12,12,12),LocalTime.now());

long second = duration.getSeconds(); //time difference

Peroid peroid = Peroid.between(LocalDate.now(),LocalDate.now());

int month = peroid.getMonth(); //getDay(),getYear() time difference

Timestamp: InstanceInstant now = Instant.now ();

Thank you for reading! About "java8 time API how to use" this article is shared here, I hope the above content can have some help for everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!

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