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

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

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

0 概览

date

包含时间?包含日期?包含时区?
java.time.LocalDateNY
java.time.LocalTimeYN
java.time.LocalDateTimeYY
java.time.ZonedDateTimeYY

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

1 本地日期时间简单操作

以上三者都是 Immutable 的。

@Test
public 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));
}

@Test
public 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);
}

@Test
public 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());
}

@Test
public 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());
}

@Test
public 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.424
System.out.println(formatter.format(dateTime));

// 自定义日期时间格式
formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// 2017-07-21 11:09:52
System.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);   
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值