一、原来的日期时间
Java1.0中包含了一个Date类,但是它的大多数方法已经在Java 1.1引入Calendar类之后被弃用了。而Calendar并不比Date好多少。它们面临的问题是:
① 可变性:象日期和时间这样的类对象应该是不可变的。Calendar类中可以使用三种方法更改日历字段:set()、add() 和 roll()。
② 偏移性:Date中的年份是从1900开始的,而月份都是从0开始的。
③格式化:格式化只对Date有用,Calendar则不行。
④此外,它们也不是线程安全的,不能处理闰秒等。
二、新增日期时间类型
java 8中引入的java.time API 已经纠正了过去的缺陷,将来很长一段时间内它都会为我们服务。Java 8 吸收了 Joda-Time 的精华,以一个新的开始为 Java 创建优秀的 API。
java.time – 包含值对象的基础包
java.time.chrono – 提供对不同的日历系统的访问。
java.time.format – 格式化和解析时间和日期
java.time.temporal – 包括底层框架和扩展特性
java.time.zone – 包含时区支持的类
Java 8 吸收了 Joda-Time 的精华,以一个新的开始为 Java 创建优秀的 API。新的 java.time 中包含了所有关于时钟(Clock),本地日期(LocalDate)、本地时间(LocalTime)、本地日期时间(LocalDateTime)、时区(ZonedDateTime)和持续时间(Duration)的类。历史悠久的 Date 类新增了 toInstant() 方法,用于把 Date 转换成新的表示形式。这些新增的本地化时间日期 API 大大简化了了日期时间和本地化的管理。
三、java.time 类
1、本地日期(LocalDate)、本地时间(LocalTime)、本地日期时间(LocalDateTime)
LocalDate代表IOS格式(yyyy-MM-dd)的日期,可以存储“生日、纪念日”等日期。
LocalTime表示一个时间,而不是日期
LocalDateTime是用来表示日期和时间的,这是一个最常用的类之一。
方法名称
描述
now() / now(ZoneId zone)
静态方法,根据当前时间创建对象/指定时区的对象
of()
静态方法,根据指定日期/时间创建对象
getDayOfMonth()/getDayOfYear()
获得月份天数(1-31) /获得年份天数(1-366)
getDayOfWeek()
获得星期几(返回一个 DayOfWeek 枚举值)
getMonth()
获得月份, 返回一个 Month 枚举值
getMonthValue() / getYear()
获得月份(1-12) /获得年份
getHours()/getMinute()/getSecond()
获得当前对象对应的小时、分钟、秒
withDayOfMonth()/withDayOfYear()/withMonth()/withYear()
将月份天数、年份天数、月份、年份修改为指定的值并返回新的对象
with(TemporalAdjuster t)
将当前日期时间设置为校对器指定的日期时间
plusDays(), plusWeeks(), plusMonths(), plusYears(),plusHours()
向当前对象添加几天、几周、几个月、几年、几小时
minusMonths() / minusWeeks()/minusDays()/minusYears()/minusHours()
从当前对象减去几月、几周、几天、几年、几小时
plus(TemporalAmount t)/minus(TemporalAmount t)
添加或减少一个 Duration 或 Period
isBefore()/isAfter()
比较两个 LocalDate
isLeapYear()
判断是否是闰年(在LocalDate类中声明)
format(DateTimeFormatter t)
格式化本地日期、时间,返回一个字符串
parse(Charsequence text)
将指定格式的字符串解析为日期、时间
2、带时区的日期、时间的处理
ZonedDateTime:一个在ISO-8601日历系统时区的日期时间,如 2007-12-03T10:15:30+01:00 Europe/Paris。
其中每个时区都对应着ID,地区ID都为“{区域}/{城市}”的格式,例如:Asia/Shanghai等
now():使用系统时间获取当前的ZonedDateTime
now(ZoneId):返回指定时区的ZonedDateTime
ZoneId:该类中包含了所有的时区信息,一个时区的ID,如 Europe/Paris
getAvailableZoneIds():静态方法,可以获取所有时区信息
of(String id):静态方法,用指定的时区信息获取ZoneId对象
Clock:使用时区提供对当前即时、日期和时间的访问的时钟。
Demo:
1 public static voidmain(String[] args) {2 Set availableZoneIds =ZoneId.getAvailableZoneIds();3 for(String string : availableZoneIds) {4 System.out.println(string);5 }6
7 ZonedDateTime t =ZonedDateTime.now();8 System.out.println(t);9
10 ZonedDateTime t1 = ZonedDateTime.now(ZoneId.of("America/New_York"));11 System.out.println(t1);12
13 //Clock clock = Clock.systemDefaultZone();
14 Clock c = Clock.system(ZoneId.of("America/New_York"));15 System.out.println(c.getZone());16 System.out.println(c.instant());17 }
3、 java.time.format.DateTimeFormatter 类
该类提供了三种格式方法:
预定义的标准格式。如:ISO_DATE_TIME;ISO_DATE
本地化相关的格式。如:ofLocalizedDate(FormatStyle.MEDIUM)
自定义的格式。如:ofPattern(“yyyy-MM-dd hh:mm:ss”)
Demo:
1 @Test2 public voidtest12(){3 LocalDateTime now =LocalDateTime.now();4
5 DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒 SSS毫秒 E 是这一年的D天");6 String string =df.format(now);7 System.out.println(string);8
9 //预定义模式
10 DateTimeFormatter df2 =DateTimeFormatter.ISO_DATE_TIME;11 System.out.println(df2.format(now));12
13 DateTimeFormatter df3 =DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);14 System.out.println(df3.format(now));15
16 DateTimeFormatter df4 =DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);17 System.out.println(df4.format(now));18
19 DateTimeFormatter df5 =DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);20 System.out.println(df5.format(now));21 }