Java 8 之 java.time 包

包概述

java.time 包是在JDK8新引入的,提供了用于日期、时间、实例和周期的主要API。

java.time包定义的类表示了日期-时间概念的规则,包括instants, durations, dates, times, time-zones and periods。这些都是基于ISO日历系统,它又是遵循 Gregorian规则的。

所有类都是不可变的、线程安全的。

一些类的介绍

  • LocalDateTime:存储了日期和时间,如:2013-10-15T14:43:14.539

  • LocalDate:存储了日期,如:2013-10-15

  • LocalTime:存储了时间,如:14:43:14.539

上面的类可以由下面的类组合来:

类之间转换的示例:

LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("localDateTime :" + localDateTime);

LocalDate localDate = LocalDate.now();
System.out.println("localDate :" + localDate);

LocalTime localtime = LocalTime.now();
System.out.println("localtime :" + localtime);

//  获取当前年份
Year year = Year.now();
System.out.println("year :" + year);
//   从Year获取LocalDate
LocalDate localDate1 = year.atDay(59);
System.out.println("localDate1 :" + localDate1);
//  把LocalTime关联到一个LocalDate得到一个LocalDateTime
LocalDateTime localDateTime1 = localtime.atDate(localDate1);
System.out.println("localDateTime1 :" + localDateTime1);

//  用指定的年获取一个Year
Year year1 = Year.of(2012);
System.out.println("year1 :" + year1);

//  从Year获取YearMonth
YearMonth yearMonth = year1.atMonth(2);
System.out.println("yearMonth :" + yearMonth);
//  YearMonth指定日得到 LocalDate
LocalDate localDate2 = yearMonth.atDay(29);
System.out.println("localDate2 :" + localDate2);
//  判断是否是闰年
System.out.println("isLeapYear :" + localDate2.isLeapYear());

//自动处理闰年的2月日期
//创建一个 MonthDay
MonthDay monthDay = MonthDay.of(2, 29);
LocalDate leapYear = monthDay.atYear(2012);
System.out.println("leapYear :" + leapYear);

//同一个 MonthDay 关联到另一个年份上
LocalDate nonLeapYear = monthDay.atYear(2011);
System.out.println("nonLeapYear :" + nonLeapYear);

上面代码的输出结果为:

localDateTime :2013-10-15T15:11:57.489
localDate :2013-10-15
localtime :15:11:57.489
year :2013
localDate1 :2013-02-28
localDateTime1 :2013-02-28T15:11:57.489
year1 :2012
yearMonth :2012-02
localDate2 :2012-02-29
isLeapYear :true
leapYear :2012-02-29
nonLeapYear :2011-02-28

格式化与时间计算

续前面的代码:

DayOfWeek dayOfWeek = DayOfWeek.of(1);
System.out.println("dayOfWeek :" + dayOfWeek);

//计算两个日期之间的天数,还可以按其他时间单位计算两个时间点之间的间隔。
long between = ChronoUnit.DAYS.between(localDate, leapYear);
System.out.println("between :" + between);

// 线程安全的格式化类,不用每次都new个SimpleDateFormat
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("uuuu MM dd");

//  把日期时间转换为字符串标识
System.out.println("date formatter :" + dateTimeFormatter.format(nonLeapYear));

//  解析字符串形式的日期时间
TemporalAccessor temporalAccessor = dateTimeFormatter.parse("2013 01 15");
System.out.println("temporalAccessor :" + LocalDate.from(temporalAccessor));

Instant instant = Instant.now(); //  时间戳
System.out.println("instant :" + instant);

//计算某月的第一天的日期
LocalDate with = nonLeapYear.with(TemporalAdjuster.firstDayOfMonth());
System.out.println("with :" + with);

// 计算某月的第一个星期一的日期
TemporalAdjuster temporalAdjuster = TemporalAdjuster.firstInMonth(DayOfWeek.MONDAY);
LocalDate with1 = localDate.with(temporalAdjuster);
System.out.println("with1 :" + with1);

// 计算localDate的下一个星期一的日期
LocalDate with2 = localDate.with(TemporalAdjuster.next(DayOfWeek.MONDAY));
System.out.println("with2 :" + with2);

输出:

dayOfWeek :MONDAY
between :-594
date formatter :2011 02 28
temporalAccessor :2013-01-15
instant :2013-10-15T07:55:30.964Z
with :2011-02-01
with1 :2013-10-07
with2 :2013-10-21

java.util.Date到新库类的转换

转换可通过下面的方法进行。

Date.toInstant()
Date.from(Instant)
Calendar.toInstant()

方法概览

该包的API提供了大量相关的方法,这些方法一般有一致的方法前缀:

  • of:静态工厂方法。
  • parse:静态工厂方法,关注于解析。
  • get:获取某些东西的值。
  • is:检查某些东西的是否是true。
  • with:不可变的setter等价物。
  • plus:加一些量到某个对象。
  • minus:从某个对象减去一些量。
  • to:转换到另一个类型。
  • at:把这个对象与另一个对象组合起来,例如:date.atTime(time)

想起以前做报表都是在存储过程里处理日期的,因为Java的日期操作确实太弱了。有了Java 8,妈妈再也不用担心我处理日期了。


欢迎关注我的微信公众号: coderbee笔记,可以更及时回复你的讨论。

Java 8 之 java.time 包》有一个想法

  1. TemporalAdjuster.firstDayOfMonth() 这里是报错。改成
    TemporalAdjusters.firstDayOfMonth() 就可以了。
    看了下API TemporalAdjuster 是接口

回复 小小名 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据