java8-03-全新的日期时间API

来源:互联网 发布:c语言词汇 编辑:程序博客网 时间:2024/05/24 00:46

  • 概览
  • 本地日期时间简单操作
  • 时间调整
  • 格式化解析
  • 和传统日期互转
    • 旧转新
    • 新转旧

除了难用难理解之外,旧的日期时间API都是可变(mutable)的,即非线程安全的。

jdk8引入的一些操作日期时间的类大都是 不可变immutable 的。

0 概览

date

包含时间? 包含日期? 包含时区? java.time.LocalDate N Y java.time.LocalTime Y N java.time.LocalDateTime Y Y java.time.ZonedDateTime Y Y

- java.time.Instant : 时间戳
- java.time.Duration : 时间间隔
- java.time.Period : 日期间隔

1 本地日期时间简单操作

以上三者都是 Immutable 的。

@Testpublic void testLocalDateTime() {    // 当前系统时间    LocalDateTime now = LocalDateTime.now();    System.out.println(now);    // 构造指定日期时间    LocalDateTime dateTime = LocalDateTime.of(2017, 7, 7, 12, 22, 35);    System.out.println(dateTime);    // 向后推两天    System.out.println(dateTime.plusDays(2));    // 向后推六个月    System.out.println(dateTime.plusMonths(6));    // 向前推两小时    System.out.println(dateTime.minusHours(2));    System.out.println(dateTime.getHour());    // 时间比较    LocalDateTime date1 = LocalDateTime.of(2017, 7, 20, 16, 17);    LocalDateTime date2 = LocalDateTime.of(2017, 6, 20, 16, 17);    System.out.println(date1.isAfter(date2));}@Testpublic void testInstant() {    // UTC 时区    Instant now = Instant.now();    System.out.println(now);    // 毫秒数    System.out.println(now.toEpochMilli());    OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8));    System.out.println(offsetDateTime);}@Testpublic void testDuration() {    // 时间间隔    Instant now = Instant.now();    Instant last = Instant.ofEpochMilli(0);    Duration duration = Duration.between(now, last);    System.out.println(duration);    System.out.println(duration.toDays());    LocalTime prev = LocalTime.of(12, 22);    LocalTime curr = LocalTime.of(14, 23);    Duration duration2 = Duration.between(prev, curr);    System.out.println(duration2);    System.out.println(duration2.toMinutes());}@Testpublic void testPeriod() {    // 日期间隔    LocalDate prev = LocalDate.of(2017, 07, 07);    LocalDate curr = LocalDate.of(2017, 07, 10);    Period period = Period.between(prev, curr);    System.out.println(period);    System.out.println(period.getDays());}@Testpublic void test3() {    YearMonth yearMonth = YearMonth.of(2017, 7);    System.out.println(yearMonth + " 有 " + yearMonth.lengthOfMonth() + " 天");    System.out.println(yearMonth.getYear() + "年有" + yearMonth.lengthOfYear() + "天");    System.out.println(yearMonth.getYear() + " isLeapYear: " + yearMonth.isLeapYear());    yearMonth = YearMonth.of(2017, 2);    // 测试29号在2017-02是不是合法的一天    System.out.println(yearMonth.isValidDay(29));}

2 时间调整

方法 java.time.LocalDateTime.with(TemporalAdjuster) 可以用来调整时间,方便的获取诸如“下一个周日”等特殊的日期。 参数 TemporalAdjuster 可以指定规则,同时他也是一个函数式接口。

就和 java.util.stream.Collector<? super T, A, R>java.util.stream.Collectors 的关系一样,jdk也为TemporalAdjuster 提供了一些工具函数,都定义在 java.time.temporal.TemporalAdjusters 里。

LocalDateTime dateTime = LocalDateTime.now();// 当月第七天LocalDateTime date1 = dateTime.withDayOfMonth(7);System.out.println(date1);// 当年第七天LocalDateTime date2 = dateTime.withDayOfYear(7);System.out.println(date2);// 下周一LocalDateTime date3 = dateTime.with(TemporalAdjusters.next(DayOfWeek.MONDAY));System.out.println(date3);// 上周一LocalDateTime date4 = dateTime.with(TemporalAdjusters.previous(DayOfWeek.MONDAY));System.out.println(date4);// 本月最后一个周日LocalDateTime date5 = dateTime.with(TemporalAdjusters.lastInMonth(DayOfWeek.SUNDAY));System.out.println(date5);// 本月第一个周日LocalDateTime date6 = dateTime.with(TemporalAdjusters.firstInMonth(DayOfWeek.SUNDAY));System.out.println(date6);// -------------------------------LocalDateTime now = LocalDateTime.now();// 下周的今天LocalDateTime nextWeekToday = now.plus(1, ChronoUnit.WEEKS);System.out.println(nextWeekToday);// 上周的今天LocalDateTime prevWeekToday = now.minus(1, ChronoUnit.WEEKS);System.out.println(prevWeekToday);

3 格式化/解析

LocalDateTime dateTime = LocalDateTime.now();// 内置ISO日期时间格式DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;// 2017-07-21T11:09:52.424System.out.println(formatter.format(dateTime));// 自定义日期时间格式formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");// 2017-07-21 11:09:52System.out.println(formatter.format(dateTime));// 解析日期TemporalAccessor parse = formatter.parse("2015-02-12 23:25:12");System.out.println(parse);

4 和传统日期互转

传统的 java.util.Date 表示的是时间轴上的一个 时间点, 是距离计算机时间原点(1970-01-01 00:00:00:000 UTC)的毫秒数, 并没有时间范围的概念。

同时 java.util.date 也没有时区的概念。

Date date = new Date();System.out.println(date.toString());// Fri Jul 21 10:10:57 CST 2017

但是以上代码输出中却带着时区信息 CST :

  • CST Central Standard Time (USA) UT-6:00
  • CST Central Standard Time (Australia) UT 9:30
  • CST China Standard Time UT 8:00
  • CST Cuba Standard Time UT-4:00

此处的时区信息并不属于 java.util.Date , 实际上是使用了java默认的时区。

和其对应的, java.time.Instant 中也不包含时区信息,只不过相比于 java.util.Date 其精度可以达到了纳秒级。

转换过程有点麻烦:

LocalDateTime <====> Instant <====> java.util.Date

旧转新

Date date = new Date();LocalDateTime localDateTime = date.toInstant()// 转为Instant                                  .atZone(ZoneId.systemDefault())// Instant 没有时区的概念,因此指定时区                                  .toLocalDateTime();System.out.println(localDateTime);// ----------------------------LocalDateTime localDateTime2 = Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.of("Asia/Shanghai")).toLocalDateTime();System.out.println(localDateTime2);

新转旧

LocalDateTime localDateTime = LocalDateTime.now();Date date = Date.from(ZonedDateTime.of(localDateTime, ZoneId.systemDefault()).toInstant());System.out.println(date);   
原创粉丝点击