Current time: October 24, 2019 How many days until JDK 14 is released (March 17, 2020)?

// How many days until JDK 14 is released?
LocalDate jdk14 = LocalDate.of(2020.3.17);
LocalDate nowDate = LocalDate.now();
System.out.println("Until JDK 14 is released:"+nowDate.until(jdk14,ChronoUnit.DAYS)+"Day");
Copy the code

JDK 8 was officially available on March 18, 2014, more than five years later. In the past 5 years, many enterprises have also adopted JDK 8, and Jdk14 will arrive in March next year. Are you using the new FEATURES of JDK 8? I’m going to write a series of articles about the new features and their use starting with Jdk 8, and the Jdk will continue to be updated as well, so keep an eye on the blog or public account if you need to.

1. Time processing class

Jdk8 brings a new class of time processing tools to replace the previously flawed time processing. The new time processing is much simpler and easier to use than before.

Time dependent class introduce
LocalDateTime Time processing class, up to nanosecond accuracy
LocalDate Time processing class, maximum accuracy to day
DateTimeFormatter Time formatting
ZoneId Time Zone Setting class

2. Time acquisition

Different classes can be used to obtain different precision of time.

/** * time to get */
@Test
public void nowTimeTest(a) {
    // The current exact time
    LocalDateTime now = LocalDateTime.now();
    System.out.println("Current exact time:" + now);
    System.out.println("Current exact time:" + now.getYear() + "-" + now.getMonthValue() + "-" + now.getDayOfMonth() + "" + now.getHour() + "-" + now.getMinute() + "-" + now.getSecond());

    // Get the current date
    LocalDate localDate = LocalDate.now();
    System.out.println("Current date:" + localDate);
    System.out.println("Current date:" + localDate.getYear() + "-" + localDate.getMonthValue() + "-" + localDate.getDayOfMonth());

    // Obtain the time of the day
    LocalTime localTime = LocalTime.now();
    System.out.println("Time of day:" + localTime);
    System.out.println("Time of day:" + localTime.getHour() + ":" + localTime.getMinute() + ":" + localTime.getSecond());

    // The current exact time of the time zone
    ZonedDateTime nowZone = LocalDateTime.now().atZone(ZoneId.systemDefault());
    System.out.println(Current exact time (time zone) : + nowZone);
    System.out.println(Current exact time (time zone) : + nowZone.getYear() + "-" + nowZone.getMonthValue() + "-" + nowZone.getDayOfMonth() + "" + nowZone.getHour() + "-" + nowZone.getMinute() + "-" + nowZone.getSecond());
} 
Copy the code

Time acquired:

Cialis 10-24T00:26:41.724 cialis 10-240-26-41 cialis 10-240-26-41 Cialis 10-24t00 :26:41.724 Cialis 10-240-26-41 cialis 2019-10-24T00:26:41.725+08:00[GMT+08:00] Current exact time (time zone) : 2019-10-240-26-41Copy the code

3. Time creation

You can specify year, month, day, hour, minute, second to create a time class, or you can use a string to directly convert to time.

/** * Time to create */
@Test
public void createTime(a) {
    LocalDateTime ofTime = LocalDateTime.of(2019.10.1.8.8.8);
    System.out.println("Current exact time:" + ofTime);

    LocalDate localDate = LocalDate.of(2019.10.01);
    System.out.println("Current date:" + localDate);

    LocalTime localTime = LocalTime.of(12.01.01);
    System.out.println("Time of day:" + localTime);
}
Copy the code

Time of creation:

Current Precise time: 2019-10-01T08:08:08 Current date: 2019-10-01 Current time: 12:01:01Copy the code

4. Time switch

/** * Date conversion */
@Test
public void convertTimeTest(a) {
    LocalDateTime parseTime = LocalDateTime.parse("The 2019-10-01 T22: please. 222");
    System.out.println("String time conversion:" + parseTime);

    LocalDate formatted = LocalDate.parse("20190101", DateTimeFormatter.BASIC_ISO_DATE);
    System.out.println("String time conversion - specify format:" + formatted);

    // Convert Date to LocalDateTime
    Date date = new Date();
    ZoneId zoneId = ZoneId.systemDefault();
    System.out.println("Convert Date to LocalDateTime:" + LocalDateTime.ofInstant(date.toInstant(), zoneId));

    // Convert LocalDateTime to Date
    LocalDateTime localDateTime = LocalDateTime.now();
    Date toDate = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    System.out.println("Convert LocalDateTime to Date:"+ toDate); \// The current time is changed to the timestamp
    long epochMilli = LocalDateTime.now().toInstant(ZoneOffset.of("+ 8")).toEpochMilli();
    System.out.println("Current time to timestamp:" + epochMilli);
    // The timestamp is converted to time
    LocalDateTime epochMilliTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), ZoneId.systemDefault());
    System.out.println("Timestamp converted to time:" + epochMilliTime);
}
Copy the code

Conversion results:

2019-10-01T22:22:22.222 2019-10-01T22:22:22.222 2019-01-01 Date is converted to LocalDateTime: 2019-10-24T00:46:41.251 LocalDateTime: Thu Oct 24 00:46:41 GMT+08:00 2019 2019-10-24T00:46:41 GMT+08:00 2019 The 2019-10-24 T00:46:41. 258Copy the code

5. Time formatting

/** * Format the date */
@Test
public void formatTest(a) {
    LocalDateTime now = LocalDateTime.now();
    System.out.println("Current time:" + now);
    System.out.println(After formatting: + now.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
    System.out.println(After formatting: + now.format(DateTimeFormatter.ISO_LOCAL_DATE));
    System.out.println(After formatting: + now.format(DateTimeFormatter.ISO_LOCAL_TIME));
    System.out.println(After formatting: + now.format(DateTimeFormatter.ofPattern("YYYY-MM-dd hh:mm:ss")));
}
Copy the code

After formatting:

Current time: 2019-10-24T00:37:44.867 Format: 2019-10-24 Format: 00:37:44.867 Format: 2019-10-24 12:37:44Copy the code

6. Time comparison

/** * time comparison */
@Test
public void diffTest(a) {
    LocalDateTime now = LocalDateTime.now();
    LocalDateTime yestory = now.minusDays(1);
    System.out.println(now + "In" + yestory + "After?" + now.isAfter(yestory));
    System.out.println(now + "In" + yestory + "Before?" + now.isBefore(yestory));

    / / time
    long day = yestory.until(now, ChronoUnit.DAYS);
    long month = yestory.until(now, ChronoUnit.MONTHS);
    long hours = yestory.until(now, ChronoUnit.HOURS);
    long minutes = yestory.until(now, ChronoUnit.MINUTES);
    System.out.println("Month of difference" + month);
    System.out.println("Days of difference" + day);
    System.out.println("Within hours." + hours);
    System.out.println("Within minutes." + minutes);

    // How many days until JDK 14 is released?
    LocalDate jdk14 = LocalDate.of(2020.3.17);
    LocalDate nowDate = LocalDate.now();
    System.out.println("Until JDK 14 is released:" + nowDate.until(jdk14, ChronoUnit.DAYS) + "Day");
}
Copy the code

Comparison results:

Cialis 10-24T00:39:01.589 is it after cialis 10-23T00:39:01.589? True T00:2019-10-24 39:01. 589 in the 2019-10-23 T00:39:01. 589 before? False Offset month 0 offset day 1 offset hour 24 offset minute 1440Copy the code

7. Time plus or minus

/** * add/subtract */
@Test
public void calcTest(a) {
    LocalDateTime now = LocalDateTime.now();
    System.out.println("Current time:"+now);
    LocalDateTime plusTime = now.plusMonths(1).plusDays(1).plusHours(1).plusMinutes(1).plusSeconds(1);
    System.out.println("After adding 1 month 1 day 1 hour 1 minute 1 second:" + plusTime);
    LocalDateTime minusTime = now.minusMonths(2);
    System.out.println("After a reduction of 2 months:" + minusTime);
}
Copy the code

Operation result:

Current time: 2019-10-24 T00:41:08. 877 January 1 day for 1 hour after 1 minute 1 second time: the 2019-11-25 T01:42:09. 877 reduced after 2 months of time: the 2019-08-24 T00:41:08. 877Copy the code

8. Time extension method

/** *
@Test
public void timeFunctionTest(a) {
    LocalDateTime now = LocalDateTime.now();
    System.out.println("Current time:" + now);
    The first day / /
    LocalDateTime firstDay = now.withDayOfMonth(1);
    System.out.println("First day of the month:" + firstDay);
    // Last second of the day
    LocalDateTime lastSecondOfDay = now.withHour(23).withMinute(59).withSecond(59);
    System.out.println("Last second of the day:" + lastSecondOfDay);
    LocalDateTime lastDay = now.with(TemporalAdjusters.lastDayOfMonth());
    System.out.println("This month last day:" + lastDay);
    // Whether it is a leap year
    System.out.println("Is this a run year?" + Year.isLeap(now.getYear()));
  
}
Copy the code

Output results:

Current time:2019-10-24T00:43:28.296First day of the month:2019-10-01T00:43:28.296Last second of the day:2019-10-24T23:59:59.296
This month last day:2019-10-31T00:43:28.296Whether this is a run year:false
Copy the code

Jdk 8’s new time classes are much easier to use than before.

Jdk 8 also treats time as a separate package and uses different class names to distinguish between them. Instead of a different package with the same class name as before. It is also clearer to use.

Code has been uploaded to the lot (https://github.com/niumoo/jdk-feature).

< over > personal website: www.codingme.net if you like this article, you can follow the public account, grow together. Attention to the public number reply resources can be no routine to obtain the most popular Java core knowledge collation & interview core materials.