laitimes

What APIs are available in Java that can be used for dates and times?

author:Dark horse programmer

Starting from Java 8, the java.time package provides a new date and time API, the new API strictly distinguishes between time, local date, local time, and makes it easier to calculate dates and times. The main types involved are the following:

LocalDate: A date that does not contain a specific time.

LocalTime: The time without a date.

LocalDateTime: Contains the date and time.

Instant: stands for timestamp.

DateTimeFormatter is used to format and parse time

Duration: Used to calculate the two "time" intervals

Period: Used to calculate two Date intervals

The types of the new API are almost all unchanging (similar to the use of String), so you can use it without worrying about being modified. where LocalDate, LocalTime, and LocalDateTime, respectively, represent date, time, and datetime objects, and instances of their classes are immutable objects. All three build objects and APIs are common.

Here's how to build the object:

What APIs are available in Java that can be used for dates and times?

LocalDate, LocalTime, LocalDateTime APIs for obtaining information.

What APIs are available in Java that can be used for dates and times?

Modify the relevant API

LocalDateTime combines the methods in LocalDate and LocalTime, so only LocalDate and LocalTime are used as examples. These methods return a new instance reference because LocalDateTime, LocalDate, and LocalTime are all immutable.

What APIs are available in Java that can be used for dates and times?

Instant timestamp

JDK8 is particularly easy to get timestamps and more feature-rich. The Instant class consists of a static factory method now() that can return the current timestamp.

Instant instant = Instant.now();
System.out.println("当前时间戳是:" + instant);

Date date = Date.from(instant);
System.out.println("当前时间戳是:" + date);

instant = date.toInstant();
System.out.println(instant);           

The timestamp contains the date and time, much like java.util.Date, in fact Instant is similar to the date before JDK8. The two classes, Instant and Date, can be converted.

DateTimeFormatter

In JDK8, a new date and time formatter has been introduced, DateTimeFormatter. Both the format method can be called.

LocalDateTime ldt = LocalDateTime.now();
System.out.println(ldt);//2021-03-01T15:09:17.444190900

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String ldtStr = ldt.format(dtf);
System.out.println(ldtStr);//2021-03-01 15:09:17

String ldtStr1 = dtf.format(ldt);
System.out.println(ldtStr1);//2021-03-01 15:09:17           

Period

In Java8, we can calculate the date interval difference using the following class: java.time.Period. Mainly the Period class methods getYears(), getMonths() and getDays() are calculated, which can only be accurate to the year, month and day. Used for comparisons between LocalDates.

LocalDate today = LocalDate.now();
System.out.println(today);     // 2021-03-01
LocalDate birthDate = LocalDate.of(1995, 1, 11);
System.out.println(birthDate); // 1995-01-11
Period period = Period.between(birthDate, today);
System.out.printf("年龄 : %d 年 %d 月 %d 日", period.getYears(), period.getMonths(),
 period.getDays());           

Duration

In Java8, we can use the following classes to calculate the time interval difference: java.time.Duration, a time-based value, a method for measuring the amount of time. Used for comparisons between LocalDateTime. It can also be used for comparisons between Instant.

LocalDateTime today = LocalDateTime.now();
System.out.println(today);
LocalDateTime birthDate = LocalDateTime.of(1990,10,1,10,50,30);
System.out.println(birthDate);

Duration duration = Duration.between(birthDate, today);//第二个参数减第一个参数
System.out.println(duration.toDays());//两个时间差的天数
System.out.println(duration.toHours());//两个时间差的小时数
System.out.println(duration.toMinutes());//两个时间差的分钟数
System.out.println(duration.toMillis());//两个时间差的毫秒数
System.out.println(duration.toNanos());//两个时间差的纳秒数           

Duration is used to calculate two "time" intervals. Period is used to calculate two "date" intervals.

Java.time.temporal.ChronoUnit

The ChronoUnit class can be used to measure a period of time in a single unit of time, and this tool class is the most complete and can be used to compare all units of time.

LocalDateTime today = LocalDateTime.now(); 
System.out.println(today); 
LocalDateTime birthDate = LocalDateTime.of(1990,10,1,10,50,30); 
System.out.println(birthDate); 

System.out.println("相差的年数:" + ChronoUnit.YEARS.between(birthDate, today)); 
System.out.println("相差的月数:" + ChronoUnit.MONTHS.between(birthDate, today)); 
System.out.println("相差的周数:" + ChronoUnit.WEEKS.between(birthDate, today)); 
System.out.println("相差的天数:" + ChronoUnit.DAYS.between(birthDate, today)); 
System.out.println("相差的时数:" + ChronoUnit.HOURS.between(birthDate, today)); 
System.out.println("相差的分数:" + ChronoUnit.MINUTES.between(birthDate, today)); 
System.out.println("相差的秒数:" + ChronoUnit.SECONDS.between(birthDate, today)); 
System.out.println("相差的毫秒数:" + ChronoUnit.MILLIS.between(birthDate, today)); 
System.out.println("相差的微秒数:" + ChronoUnit.MICROS.between(birthDate, today)); 
System.out.println("相差的纳秒数:" + ChronoUnit.NANOS.between(birthDate, today)); 

System.out.println("相差的半天数:" + ChronoUnit.HALF_DAYS.between(birthDate, today)); 
System.out.println("相差的十年数:" + ChronoUnit.DECADES.between(birthDate, today)); 
System.out.println("相差的世纪(百年)数:" + ChronoUnit.CENTURIES.between(birthDate, today)); 
System.out.println("相差的千年数:" + ChronoUnit.MILLENNIA.between(birthDate, today)); 
System.out.println("相差的纪元数:" + ChronoUnit.ERAS.between(birthDate, today));