java.time 包是在JDK8新引入的,提供了用于日期、时间、实例和周期的主要API。html
java.time包定义的类表示了日期-时间概念的规则,包括instants, durations, dates, times, time-zones and periods。这些都是基于ISO日历系统,它又是遵循 Gregorian规则的。java
全部类都是不可变的、线程安全的。api
LocalDateTime:存储了日期和时间,如:2013-10-15T14:43:14.539
。安全
LocalDate:存储了日期,如:2013-10-15
。spa
LocalTime:存储了时间,如:14:43:14.539
。.net
上面的类能够由下面的类组合来:线程
类之间转换的示例:
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
DateTimeFormatter:在日期对象与字符串之间进行转换。
ChronoUnit:计算出两个时间点之间的时间距离,可按多种时间单位计算。
TemporalAdjuster:各类日期计算功能。
续前面的代码:
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
转换可经过下面的方法进行。
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,妈妈不再用担忧我处理日期了。