preface

Java8’s date and time API provides us with great convenience. How to better familiar with the use of time API is also a very important knowledge of learning java8, let’s learn to learn.

This article is more code, can be used as a tool, when you need to use, come back to see.

directory

1. How to convert a common Date time to LocalDateTime?

The above is the procedure for converting a common Date object to a Java8 time. You need to pay special attention to the time zone.

// Set the time zone // ZoneId defaultZoneId = zoneid.systemdefault (); ZoneId defaultZoneId = ZoneId.of("Asia/Shanghai"); System.out.println("set TimeZone : " + defaultZoneId); // Date: Fri Jan 17 16:52:59 CST 2020 date date = new date (); System.out.println("date : " + date); //1. Instant: 2020-01-17T08:52:59.235z Instant = date.toinstant (); System.out.println("instant : " + instant); //Zone: default UTC+0 //2. LocalDate: 2020-01-17 localDate localDate = instant. AtZone (defaultZoneId).tolocalDate (); System.out.println("localDate : " + localDate); //3. LocalDateTime: 2020-01-17T16:52:59.235 LocalDateTime LocalDateTime = instant. AtZone (defaultZoneId).tolocalDateTime (); System.out.println("localDateTime : " + localDateTime); //4. ZonedDateTime: 2020-01-17T16:52:59.235+08:00[Asia/Shanghai] ZonedDateTime = instant. AtZone (defaultZoneId); System.out.println("zonedDateTime : " + zonedDateTime);Copy the code

2. How to convert the Java8 time to a common Date?

The conversion process is reversed with the following code:

ZoneId defaultZoneId = zoneid. of("Asia/Shanghai"); System.out.println(" Default TimeZone : " + defaultZoneId); LocalDate localDate = LocalDate.of(2020, 01, 17); Date date = Date.from(localDate.atStartOfDay(defaultZoneId).toInstant()); System.out.println("\n1. LocalDate -> Date"); System.out.println("localDate : " + localDate); System.out.println("date : " + date); LocalDateTime LocalDateTime = LocalDateTime. Of,01,17,17,46,31 (2020); Date date2 = Date.from(localDateTime.atZone(defaultZoneId).toInstant()); System.out.println("\n2. LocalDateTime -> Date"); System.out.println("localDateTime : " + localDateTime); System.out.println("date2 : " + date2); ZonedDateTime zonedDateTime = localDateTime.atZone(defaultZoneId); Date date3 = Date.from(zonedDateTime.toInstant()); System.out.println("\n3. ZonedDateTime -> Date"); System.out.println("zonedDateTime : " + zonedDateTime); System.out.println("date3 : " + date3);Copy the code

3. How do you compare time?

  • Both Date and localDate and localDateTime in java8 can be compared using compareTo methods.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date date1 = sdf.parse("2020-01-17"); Date date2 = sdf.parse("2020-01-16"); System.out.println(" + date1.compareTo(date2));Copy the code

LocalDate and LocalDateTime provide three methods, isAfter(),isBefore(), and isEqual().

Before java8, we used after(), before(), equals. The method name is different, but the function is similar.

4. Instant in java8

Instant represents an Instant point-in-time value object.

It is calculated from 1970-01-01t00:00:00 Z in milliseconds, immutable, and thread-safe.

5. Get the current timestamp mode?

New Timestamp(system.currentTimemillis ());

New Date().getTime();

Method 3: Instant.

Calendar.getinstance ()

Localdatetime.now () localDatetime.now ()

Localdate.now ()

Timestamp timestamp = new Timestamp(System.currentTimeMillis()); / / the 2020-01-17 17:04:53. 346 System. Out. Println (timestamp); //return 1579251953703 System.out.println(timestamp.getTime()); // Instant 2020-01-17t09:05:53.703z instant instant = timestamp.toinstant (); System.out.println(instant); //return 1579251953703 System.out.println(instant.toEpochMilli()); // instant to timestamp 1579251893346 Timestamp tsFromInstant = Timestamp.from(instant); System.out.println(tsFromInstant.getTime());Copy the code

6. Time to String

The SimpleDateFormat API can be used to convert time and string values to threadsafe values.

LocalDateTime now = localDatetime.now (); System.out.println("Before : " + now); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String formatDateTime = now.format(formatter); System.out.println("After : " + formatDateTime);Copy the code

7. Turn time String

String now = "2020-01-17 17:30";

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

LocalDateTime formatDateTime = LocalDateTime.parse(now, formatter);

System.out.println("Before : " + now);

System.out.println("After : " + formatDateTime);

System.out.println("After : " + formatDateTime.format(formatter));Copy the code

8.Instant č½¬LocalDateTime

Instant instant = Instant.now(); System.out.println("Instant : " + instant); LDT = localDatetime. ofInstant(instant, zoneid. of("Asia/Shanghai")); System.out.println("LocalDateTime : " + ldt);Copy the code


9. LocalDateTime Instant

LocalDateTime dateTime = LocalDateTime.of(2020, 01, 17, 6, 17, 10); //LocalDateTime : 2020-01-17T06:17:10 System.out.println("LocalDateTime : " + dateTime); // +hh:mm if the time zone is set to GMT + 8, the time is 8 hours later than the original time. 2020-01-16T22:17:10Z Instant instant = dateTime.toInstant(ZoneOffset.of("+08:00")); System.out.println("Instant : " + instant);Copy the code

10. Why do localDate and localDateTime appear in Java8?

Prior to java8, time was represented by the Date class and so on, which included some poor API design. For example, the year java.util.Date starts at 1900, the months start at 1, and the days start at 0, which is not intuitive. Third-party date and Time libraries make up for this somewhat in popularity, such as Joda-time.

To address these issues and provide better support in the JDK kernel, a new date and time API has been designed for Java SE 8 that does not have these issues.

Java SE 8 comes with the new date and time API, java.time, which provides developers with much improved security and functionality. The new API models the domain well and provides a large number of classes for modeling a variety of developer use cases.

Welcome to the public account [Xia Dream development notes], reply dry goods, receive a selection of learning video