Java8 class time

1 Introduction

Before Java8, date and time API has been criticized by developers, including: java.util.Date is mutable, SimpleDateFormat non-thread-safety issues. Therefore, Java8 introduced a new date and time processing API, a new API based on the ISO standard calendar system.

2, nouns

Timestamp (time): time stamp refers GMT January 1, 1970 00 hours 00 minutes 00 seconds to the current total number of seconds (milliseconds), can be understood as an absolute time, it has nothing to do with time zones, different time zones on the same Interpretation of the timestamp is not the same
time zone: same time (time stamp), the world each region is probably not the same, the specific time related to the time zone by longitude total is divided into 24 time zones, Greenwich, England is 0:00 zone, Beijing, China East area 8

Two, Java8 time

2.1 Java8 time
when Java1.0 versions support only time java.util.Date class that represents the starting point from 1970-01-01 00:00:00 time accurate to milliseconds. When Java1.1, indicate the date java.util.Calendar class is added in, but Calendar class there are many problems, such as: Date, Calendar class are variable, but such as time, date class should not be change; Date of the month is starting from zero; Date format only entered into force, does not take effect for the Calendar. Around 2001, Joda-Time library project start date and time is dedicated to providing a high quality, ease of use because it will soon be widely used. Java8 learn a lot Joda-Time feature, we launched a new toolkit time

Java8 Time Class In java.time package path, a main class time is as follows:
the Instant timestamp (time)
independent LocalDate time zone date
LocalTime regardless of the time zone
date and time independent LocalDateTime the time zone
ZonedDateTime associated with the time zone the date and time
when ZoneId zone
ZoneOffset time offset relative to GMT, for example: +08: 00

Time Output Format Type:

Output Type Description
2019-06-10T03: 48: 20.847Z
world standard time, T: the date and time division, Z: UTC
2019-06-10T11: 51: 48.872
time excluding the time zone information
2019-06-10T11: 55: 04.421 + 08: 00 [ Asia / Shanghai]
contains information on the time zone time, + 08: 00 with respect to 0:00 zone plus eight hours, [Asia / Shanghai]: time zone
3, Java8 time-use
configuration Instant:

    public static void main(String ... args) {
        // 获取当前时间戳
        Instant instant = Instant.now();
        System.out.println(instant);
        // 指定系统时间戳
        Instant instant1 = Instant.ofEpochMilli(System.currentTimeMillis());
        System.out.println(instant1);
        // 解析指定时间戳
        Instant instant2 = Instant.parse("2019-06-10T03:42:39Z");
        System.out.println(instant2);
    }
--- 
2020-03-07T03:35:32.062Z
2020-03-07T03:35:32.063Z
2019-06-10T03:42:39Z

Construction LocalDateTime:

     public static void main(String ... args) {
        // 获取当前时间
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);
		// 指定时间
        LocalDateTime localDateTime1 = LocalDateTime.of(2019, 06, 10, 10, 30,30);
        System.out.println(localDateTime1);
		// 解析时间
        LocalDateTime localDateTime2 = LocalDateTime.parse("2019-06-10 11:55:04", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println(localDateTime2);
    }
---
2020-03-07T11:38:57.870
2019-06-10T10:30:30
2020-03-07T11:55:04

Construction ZonedDateTime:

    public static void main(String ... args) {
		// 获取当前时区当前时间
        ZonedDateTime zonedDateTime = ZonedDateTime.now();
        System.out.println(zonedDateTime);
		// 指定时间及时区
        ZonedDateTime zonedDateTime1 = ZonedDateTime.of(2019, 06, 10, 10, 30,30, 00,  ZoneId.of("UTC"));
        System.out.println(zonedDateTime1);
		// 解析指定时间
        ZonedDateTime zonedDateTime2 = ZonedDateTime.parse("2019-06-10T12:03:19.367+08:00[Asia/Shanghai]", DateTimeFormatter.ISO_ZONED_DATE_TIME);
        System.out.println(zonedDateTime2);
    }
---
2019-06-10T12:08:44.405+08:00[Asia/Shanghai]
2019-06-10T10:30:30Z[UTC]
2019-06-10T12:03:19.367+08:00[Asia/Shanghai]

Time adjustment (TemporalAdjuster)

    public static void main(String ... args) {
        LocalDateTime localDateTime = LocalDateTime.now();
        // 本年本月最后一天
        System.out.println(localDateTime.with(TemporalAdjusters.lastDayOfMonth()));
        // 本年本月第一天
        System.out.println(localDateTime.with(TemporalAdjusters.firstDayOfMonth()));
        // 本年下一月第一天
        System.out.println(localDateTime.with(TemporalAdjusters.firstDayOfNextMonth()));
        // 下一年第一天
        System.out.println(localDateTime.with(TemporalAdjusters.firstDayOfNextYear()));
        // 本年最后一天
        System.out.println(localDateTime.with(TemporalAdjusters.lastDayOfYear()));
        // 下一个周五
        System.out.println(localDateTime.with(TemporalAdjusters.next(DayOfWeek.FRIDAY)));
        // 本月第一个周五
        System.out.println(localDateTime.with(TemporalAdjusters.firstInMonth(DayOfWeek.FRIDAY)));
        // 本月最后一个周五
        System.out.println(localDateTime.with(TemporalAdjusters.lastInMonth(DayOfWeek.FRIDAY)));
        // 下一个周五,如果当前是周五则返回当前时间
        System.out.println(localDateTime.with(TemporalAdjusters.nextOrSame(DayOfWeek.FRIDAY)));
        // 前一个周五
        System.out.println(localDateTime.with(TemporalAdjusters.previous(DayOfWeek.FRIDAY)));
        // 前一个周五,如果当前是周五则返回当前时间
        System.out.println(localDateTime.with(TemporalAdjusters.previousOrSame(DayOfWeek.FRIDAY)));
        // 当前时间+100天,plusYeas/plusMonths/plusWeeks/plusHours/plusMinutes/plusSeconds形式相同,同于System.out.println(localDateTime.plus(100, ChronoUnit.DAYS));
        System.out.println(localDateTime.plusDays(100));
        // 当前时间-100天,minusYeas/minusMonths/minusWeeks/minusHours/minusMinutes/minusSeconds形式相同,同于System.out.println(localDateTime.minus(100, ChronoUnit.DAYS));
        System.out.println(localDateTime.minusDays(100));
    }
---
2019-07-31T20:19:49.250
2019-07-01T20:19:49.250
2019-08-01T20:19:49.250
2020-01-01T20:19:49.250
2019-12-31T20:19:49.250
2019-07-19T20:19:49.250
2019-07-05T20:19:49.250
2019-07-26T20:19:49.250
2019-07-19T20:19:49.250
2019-07-12T20:19:49.250
2019-07-12T20:19:49.250
2019-10-22T20:19:49.250
2019-04-05T20:19:49.250

Reference article: the Java in time and time zone
Java8日期/ Time Use
java8日期屌炸天特性以及实战

Published 80 original articles · won praise 140 · views 640 000 +

Guess you like

Origin blog.csdn.net/linjpg/article/details/104711870