The date and time the operation class Java8

The date and time the operation class Java8

Java8 time class has two important features

  • Thread Safety
  • Immutable classes, new objects are returned
    Obviously, this feature solves the problem with the original java.util.Date class SimpleDateFormat thread safe. Meanwhile Java8 time class provides many built-in methods to facilitate the operation of the corresponding time.

image.png
The picture shows the coverage Java8 time class
associated with a class

  • LocalDate
  • LocalTime
  • localdateti to
  • ZoneId
  • ZonedDateTime
  • Instant

Instant class

Instant class used to represent Greenwich Mean Time (UTC) time point, the initial time is 1970-01-01T00: 00: 00Z. That is, from January 1, 1970 start time, get the seconds and even nanosecond value. The timestamp can be converted with the date and time. It can represent the world's most complete human time. This class java.util.Date class compared to the original, accurate to the nanosecond level.

Get the current value and the second value nanoseconds

Instant instant = Instant.now();
System.out.println(instant);
System.out.println(instant.getEpochSecond());
System.out.println(instant.getNano());

结果如下
2019-08-28T07:59:54.979Z
1566979194
979000000

The specified seconds value into Instant. Instant.ofEpochSecond () method.

Instant instant1 = Instant.ofEpochSecond(1566981233L);
System.out.println(instant1);

LOCALDATE, localtime, localdateti me, my zoneddateti

Java8 use LocalDate, LocalTime, LocalDateTime, ZonedDateTime each operation date, time, date, and time.
The four classes use the system default time zone
get today's date and time

LocalDate today = LocalDate.now();
System.out.println(today);

LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime);

LocalTime localTime = LocalTime.now();
System.out.println(localTime);

ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println(zonedDateTime);

ZoneId zoneId = ZoneId.systemDefault();
System.out.println(zoneId);

The results are as follows

2019-08-28
2019-08-28T17:42:01.964
17:42:01.965
2019-08-28T17:42:01.965+08:00[Asia/Shanghai]
Asia/Shanghai

And by a specific date 2019-09-30 isBefore () to determine whether the specified date before today

LocalDate future = LocalDate.of(2019, 9, 30);
boolean before = today.isBefore(future);
System.out.println(before);

LocalDateTime turn String conversion format specified by DateTimeFormatter

String formatStr = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss"));
System.out.println(formatStr);

String into LocalDate

String str = "2019-01-02";
LocalDate localDate2 = LocalDate.parse(str, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
System.out.println(localDate2);

Instant LocalDateTime and mutual conversion

Gets seconds and milliseconds of the day. Instant LocalDateTime turn obtain a timestamp. Since LocalDateTime does not contain a time zone, into Instant when you need to specify the district. Beijing also is the East eight districts ZoneOffset.of ( "+ 8")

long milliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
System.out.println(milliSecond);

long second = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
System.out.println(second);

Instant timestamp turn LocalDateTime. Use LocalDateTime.ofInstant method, you need to specify which time zone converted to a time of

LocalDateTime localDateTime2 = LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault()); //使用系统默认时间
System.out.println(localDateTime2);

结果如下
2019-08-28T16:33:53.639

Reference article

https://blog.csdn.net/u013066244/article/details/96443952

Guess you like

Origin www.cnblogs.com/helloDuo/p/11427655.html