一 我们为什么要学习 java.timeAPI 原先的Date and Calendar 类的api比较复杂,不易于理解,应用起来不是很灵活。 Calendar 是个线程不安全的类会导致SimpleDateFormat线程不安全。 java.time是JSR 310: Date and Time API.规范所开发,其所有类都是线程安全的或者是枚举类型的类 java.time 的API 使用简单,能够灵活计算时间,矫正时间。
二 LocalDate LocalDate 是 日期,在java.time 中 日期和时间是可以分开和组合的。
2.1 创建date的方式 // 创建date的方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public void LocalDateTest () { LocalDate now = LocalDate.now(); System.out.println(now); LocalDate ofDate = LocalDate.of(2018 , 8 , 8 ); System.out.println(ofDate); Clock clock = Clock.systemDefaultZone(); LocalDate date = LocalDate.now(clock); System.out.println(date); LocalDate localDate = LocalDate.ofYearDay(2018 , 256 ); System.out.println(localDate); }
2.2 使用LocalDate读取date
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 public void LocalDateTest2 () { LocalDate date = LocalDate.of(2019 ,10 ,27 ); date.getYear(); System.out.println(date.getYear()); date.getDayOfMonth(); System.out.println(date.getDayOfMonth()); date.getDayOfWeek(); System.out.println(date.getDayOfWeek()); date.getDayOfYear(); System.out.println(date.getDayOfYear()); date.getMonthValue(); System.out.println(date.getMonthValue()); date.lengthOfMonth(); System.out.println(date.lengthOfMonth()); date.isLeapYear(); System.out.println(date.isLeapYear()); }
2.3 TemporalField 读取 LocalDate 的值 ChronoField 是个枚举其实现了TemporalField接口,除了2.2的方式读取date我们还可以使用ChronoField方式读取date.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public void LocalDateTest3 () { LocalDate date = LocalDate.of(2019 , 10 , 27 ); date.get(ChronoField.YEAR); System.out.println(date.get(ChronoField.YEAR)); date.get(ChronoField.MONTH_OF_YEAR); System.out.println(date.get(ChronoField.MONTH_OF_YEAR)); date.get(ChronoField.DAY_OF_MONTH); System.out.println(date.get(ChronoField.DAY_OF_MONTH)); date.get(ChronoField.DAY_OF_WEEK); System.out.println(date.get(ChronoField.DAY_OF_WEEK)); }
2.4 解析LocalDate
1 2 3 4 5 6 7 8 9 10 public void LocalDateParse () { String dateStr = "2019-10-27" ; LocalDate parse = LocalDate.parse(dateStr); System.out.println(parse); DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd" ); LocalDate date = LocalDate.parse("2019/10/27" , dateTimeFormatter); System.out.println(date); }
2.5 使用 Period 操纵 date 获得连个日期之间的差值,可以获得年,月,日,判断是否非0等等。
1 2 3 4 5 6 7 @Test public void LocalDatePor () { LocalDate date1 = LocalDate.of(2019 , 10 , 27 ); LocalDate date2 = LocalDate.of(2019 , 10 , 25 ); Period between = Period.between(date2, date1); System.out.println(between.getDays()); }
2.6 修改date 通过withAttribute修改不会改变原来的date,会在原来date的基础上形成新的LocalDate副本。
1 2 3 4 5 6 7 8 9 10 public void LocalDateWith () { LocalDate date1 = LocalDate.of(2019 , 10 , 27 ); LocalDate date2 = date1.withMonth(9 ); System.out.println(date2); LocalDate date3 = date2.withYear(2018 ); System.out.println(date3); System.out.println(date1); }
2.7 使用 TemporalAdjuster 修改日期 TemporalAdjuster 时间矫正器修改时间也是不会改变原来的date,会新生成LocalDate 副本,相比于withAttribute,其API更加丰富,提供大量的静态工厂方法,能满足我们日常开发需求。
1 2 3 4 5 6 7 8 9 10 11 12 13 public void LocalDateTemporalAdjuster () { LocalDate date1 = LocalDate.of(2019 , 10 , 27 ); LocalDate date2 = date1.with(TemporalAdjusters.firstDayOfMonth()); System.out.println(date2); LocalDate date3 = date1.with(TemporalAdjusters.firstDayOfYear()); System.out.println(date3); LocalDate date4 = date1.with(TemporalAdjusters.lastDayOfYear()); System.out.println(date4); }
三LocalTime 如果你已经掌握了上述的LocalDate的基本用法,那么学习LocalTime也是十分简单,原因是LocalTime和LocalDate的API基本差不多。
3.1 创建LocalTime
1 2 3 4 5 6 7 8 9 public void localTimeTest1 () { LocalTime now = LocalTime.now(); System.out.println(now); LocalTime of = LocalTime.of(22 , 47 ); System.out.println(of); }
3.2 读取时间
1 2 3 4 5 6 7 8 9 10 11 12 public void localTimeRead () { LocalTime tiem = LocalTime.of(22 , 50 ); int hour = tiem.getHour(); int minute = tiem.getMinute(); int second = tiem.getSecond(); int nano = tiem.getNano(); }
3.3 时间解析
1 2 3 4 5 6 7 8 9 10 public void localTimeParse () { LocalTime parse = LocalTime.parse("22:50:00" ); System.out.println(parse); LocalTime time = LocalTime.parse("22:50:00" , DateTimeFormatter.ISO_TIME); System.out.println(time); }
3.4 时间修改
1 2 3 4 5 6 7 8 public void localTime () { LocalTime time = LocalTime.of(22 , 50 ); LocalTime time1 = time.withHour(2 ); System.out.println(time1); LocalTime time2 = time.withMinute(10 ); System.out.println(time2); }
3.5 使用Duration获得时间差值
1 2 3 4 5 6 7 8 9 10 11 public void localTime () { LocalTime time1 = LocalTime.of(22 , 50 ,20 ,20 ); LocalTime time2 = LocalTime.of(23 , 10 ); Duration duration = Duration.between(time1, time2); long seconds = duration.getSeconds(); int nano = duration.getNano(); System.out.println(seconds); System.out.println(nano); }
四 LocalDate 和 LocalTime 的相互合并和转换 LocalDate 和 LocalTime 能相互合并成 LocalDateTime ,LocalDateTime 也可以转为 LocalDate 或者 LocalTime。 LocalDateTime其他的API 跟 LocalTime,LocalDate 差不多,在次不赘述。
1 2 3 4 5 6 7 8 9 10 11 12 13 public void LocalDateTimeTest () { LocalDate date = LocalDate.of(2019 , 10 , 27 ); LocalTime time = LocalTime.of(23 , 20 , 00 ); LocalDateTime localDateTime = LocalDateTime.of(date, time); System.out.println(localDateTime); LocalDate localDate = localDateTime.toLocalDate(); System.out.println(localDate); LocalTime localTime = localDateTime.toLocalTime(); System.out.println(localTime); }
五 ZoneId java8中 java.time.ZoneId代替了老版本java.util.TimeZone 。
5.1 时区偏移 默认是当前时区和UTC /格林威治的固定偏差值
1 2 3 4 5 6 7 8 9 10 public void TimeZoneId () { ZoneId shanghai = ZoneId.of("Asia/Shanghai" ); LocalDate date = LocalDate.of(2019 , 10 , 27 ); ZonedDateTime zonedDateTime = date.atStartOfDay(shanghai); ZoneOffset offset = zonedDateTime.getOffset(); System.out.println(offset); }
5.2 TimeZone 转 ZoneId
1 2 3 4 5 public void TimeZoneId2 () { ZoneId zoneId = TimeZone.getDefault().toZoneId(); String id = zoneId.getId(); System.out.println(id); }
5.3 时区时间计算
1 2 3 4 5 6 7 8 9 10 11 12 13 public void TimeZoneId3 () { ZoneId zoneId = ZoneId.of("America/Chicago" ); Instant instant = Instant.now(); System.out.println(LocalDateTime.ofInstant(instant,ZoneId.of("Asia/Shanghai" ))); ZonedDateTime zonedDateTime = instant.atZone(zoneId); ZoneOffset offset = zonedDateTime.getOffset(); System.out.println(offset); LocalDateTime localDateTime = zonedDateTime.toLocalDateTime(); System.out.println(localDateTime); }