新的日期和时间API

LocalDate,LocalTime,Instant,Duration,Period

  • 使用LocalDate和LocalTime
    localdate是一个不可变的对象,只能提供简单的日期,并不包含当天的时间信息,也不附带任何与时区相关的信息。
//日期 LocalDate localDate = LocalDate.now(); System.out.println(localDate); //创建实例 LocalDate localDate1 = LocalDate.of(2018, 06, 9); System.out.println( localDate1.getYear() + "-" + localDate1.getMonthValue() + "-" + localDate1.getDayOfMonth()); System.out.println(localDate1.get(ChronoField.YEAR)); System.out.println(localDate1.get(ChronoField.MONTH_OF_YEAR)); System.out.println(localDate1.get(ChronoField.DAY_OF_MONTH)); LocalDate localDate2 = LocalDate.parse("2018-06-09", DateTimeFormatter.ofPattern("yyyy-MM-dd")); System.out.println("localDate2:" + localDate2); boolean before = localDate2.isBefore(LocalDate.now()); System.out.println(before);

【新的日期和时间API】localtime只提供当天的时间信息。
//时间 LocalTime localTime = LocalTime.now(); System.out.println(localTime); LocalTime localTime1 = LocalTime.of(12, 12, 12); System.out.println(localTime1.getHour() + ":" + localTime1.getMinute() + ":" + localTime1.getSecond()); LocalTime localTime2 = LocalTime.parse("07:23:02", DateTimeFormatter.ofPattern("HH:mm:ss")); System.out.println(localTime2);

合并日期和时间,LocalDateTime,是LocalDate和LocalTime的合体。它同时表示日期和时间,但不带有时区信息。
//日期+ 时间 LocalDateTime localDateTime = LocalDateTime.now(); System.out.println(localDateTime); LocalDateTime localDateTime1 = LocalDateTime.of(2018,06,9,12,12,12); System.out.println(localDateTime1); System.out.println(localDateTime1.toLocalDate()); System.out.println(localDateTime1.toLocalTime());

  • 机器的日期和时间格式
    对于人比较习惯的时间,然后对于计算机确是很不好理解,因此有了instant类对时间建模的方式。
//instant Instant instant = Instant.now(); System.out.println("instant:"+instant); System.out.println(instant.getEpochSecond()); System.out.println(instant.getNano()); Instant instant1 = Instant.ofEpochMilli(1); System.out.println(instant1); Instant instant2 = Instant.ofEpochSecond(2,1_000_000_000); System.out.println(instant2);

  • 定义duration和period
    duration主要用于处理以秒和纳秒衡量时间的长短,它不能只传递日期作为参数。
//duration + period Duration duration = Duration.between(localTime1,localTime2); System.out.println(duration.getSeconds()); System.out.println(duration.getNano()); System.out.println(duration.isZero()); Period period = Period.between(localDate1,localDate2); System.out.println(period); System.out.println(period.getDays()); System.out.println(period.getMonths()); System.out.println(period.plusDays(1).getDays());

操作,解析和格式和日期 因为LocalDate是不可变的对象,要想修改它最好的当时是创建这个对象的一个副本,然后修改这个对象的副本,它不会影响原来的对象。修改的方式是采用withAttribute。
//withAttribute LocalDate localDate3 = LocalDate.now(); LocalDate localDate4 = localDate3.withYear(2019); localDate3.with(ChronoField.YEAR, 2019); System.out.println("localDate3:" + localDate3 + "localDate4:" + localDate4); LocalDate localDate5 = localDate3.plusDays(1); System.out.println(localDate5); LocalDateTime localDateTime2 = LocalDateTime.now(); System.out.println(localDateTime2); LocalDateTime localDateTime3 = localDateTime2.withDayOfMonth(2); System.out.println(localDateTime3); LocalDateTime localDateTime4 = localDateTime2.plusDays(10); System.out.println(localDateTime4);

  • 使用TemporalAdjuster
    TemporalAdjuster提供了十分丰富的更加复杂的日期操作,而这些方法的名称也十分的直观。可以通过工厂TemporalAdjusters来操作。
//TemporalAdjuster TemporalAdjuster temporalAdjuster = TemporalAdjusters.firstDayOfMonth(); System.out.println(temporalAdjuster.adjustInto(localDate3)); TemporalAdjuster temporalAdjuster1 = TemporalAdjusters.firstDayOfNextMonth(); System.out.println(temporalAdjuster1.adjustInto(localDate3)); TemporalAdjuster temporalAdjuster2 = TemporalAdjusters.lastDayOfMonth(); System.out.println(temporalAdjuster2.adjustInto(localDate3)); TemporalAdjuster temporalAdjuster3 = TemporalAdjusters.dayOfWeekInMonth(localDate3.getMonthValue(),localDate3.getDayOfWeek()); System.out.println(temporalAdjuster3.adjustInto(localDate3));

  • 打印输出以及解析日期
    DateTimeFormatter是常用的日期解析格式,它预定义了像Basic_ISO_DATE和ISO_LOCAL_DATE这样的常量。和老的dateformatter相比,DateTimeFormatter是线程安全的。
//DateTimeFormatter LocalDateTime localDateTime5 = LocalDateTime.now(); String format = localDateTime5.format(DateTimeFormatter.BASIC_ISO_DATE); System.out.println(format); String format1 = localDateTime5.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME); System.out.println(format1); String format2 = localDateTime5.format(DateTimeFormatter.ISO_DATE); System.out.println(format2);

处理不同的时区和历法 时区的设置为:“{区域}/{城市}”。
//zoneId ZoneId zoneId = ZoneId.of("Europe/Rome"); System.out.println(zoneId); String id = ZoneId.systemDefault().getId(); System.out.println(id); ZoneRules rules = ZoneId.systemDefault().getRules(); System.out.println(rules);

当获取到zoneId之后可以和localdate,localdatetime,instant对象整合起来,构造为一个zonedatetime实例。
//zoneId ZoneId zoneId = ZoneId.of("Europe/Rome"); System.out.println(zoneId); String id = ZoneId.systemDefault().getId(); System.out.println(id); ZoneRules rules = ZoneId.systemDefault().getRules(); System.out.println(rules); LocalDateTime localDateTime6 = LocalDateTime.now(); ZonedDateTime zonedDateTime = localDateTime6.atZone(ZoneId.of("Europe/Rome")); System.out.println(zonedDateTime.getYear()+"-"+zonedDateTime.getMonthValue()+"-"+zonedDateTime.getDayOfMonth());

    推荐阅读