Java 8 之 java.time 包

来源:互联网 发布:淘宝联盟ios历史版本 编辑:程序博客网 时间:2024/06/03 09:07

原文:http://coderbee.net/index.php/java/20131015/503

 

包概述

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

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

  • Year:表示年份。

  • Month:表示月份。

  • YearMonth:表示年月。

  • MonthDay:表示月日。

  • DayOfWeek:存储星期的一天。

类之间转换的示例:

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获取LocalDateLocalDate localDate1 = year.atDay(59);System.out.println("localDate1 :" + localDate1);//  把LocalTime关联到一个LocalDate得到一个LocalDateTimeLocalDateTime localDateTime1 = localtime.atDate(localDate1);System.out.println("localDateTime1 :" + localDateTime1);//  用指定的年获取一个YearYear year1 = Year.of(2012);System.out.println("year1 :" + year1);//  从Year获取YearMonthYearMonth yearMonth = year1.atMonth(2);System.out.println("yearMonth :" + yearMonth);//  YearMonth指定日得到 LocalDateLocalDate localDate2 = yearMonth.atDay(29);System.out.println("localDate2 :" + localDate2);//  判断是否是闰年System.out.println("isLeapYear :" + localDate2.isLeapYear());//自动处理闰年的2月日期//创建一个 MonthDayMonthDay 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.489localDate :2013-10-15localtime :15:11:57.489year :2013localDate1 :2013-02-28localDateTime1 :2013-02-28T15:11:57.489year1 :2012yearMonth :2012-02localDate2 :2012-02-29isLeapYear :trueleapYear :2012-02-29nonLeapYear :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个SimpleDateFormatDateTimeFormatter 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 :MONDAYbetween :-594date formatter :2011 02 28temporalAccessor :2013-01-15instant :2013-10-15T07:55:30.964Zwith :2011-02-01with1 :2013-10-07with2 :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)
原创粉丝点击