1.旧版日期时间的问题
在旧版本中JDK对于日期和时间这块的时间是非常差的。
/**
* 旧版日期时间设计的问题
*/
@Test
public void test01() throws Exception{
// 1.设计不合理
Date date = new Date(2021,05,05);
System.out.println(date);
// 2.时间格式化和解析操作是线程不安全的
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
for (int i = 0; i < 50; i++) {
new Thread(()->{
// System.out.println(sdf.format(date));
try {
System.out.println(sdf.parse("2021-05-06"));
} catch (ParseException e) {
e.printStackTrace();
}
}).start();
}
}
- 设计不合理,在java.util和java.sql的包中都有日期类,java.util.Date同时包含日期和时间的,而java.sql.Date仅仅包含日期,此外用于格式化和解析的类在java.text包下。
- 非线程安全,java.util.Date是非线程安全的,所有的日期类都是可变的,这是java日期类最大的问题之一。
- 时区处理麻烦,日期类并不提供国际化,没有时区支持。
2. 新日期时间API介绍
JDK 8中增加了一套全新的日期时间API,这套API设计合理,是线程安全的。新的日期及时间API位于 java.time 包
中,下面是一些关键类。
Java 8增加了一个Clock时钟类用于获取当时的时间戳,或当前时区下的日期时间信息。以前用到System.currentTimeInMillis()和TimeZone.getDefault()的地方都可用Clock替换。
- LocalDate :表示日期,包含年月日,格式为 2019-10-16
- LocalTime :表示时间,包含时分秒,格式为 16:38:54.158549300
- LocalDateTime :表示日期时间,包含年月日,时分秒,格式为 2018-09-06T15:33:56.750
- DateTimeFormatter :日期时间格式化类。
- Instant:时间戳,表示一个特定的时间瞬间。
- Duration:用于计算2个时间(LocalTime,时分秒)的距离;持续时间,时间差
- Period:用于计算2个日期(LocalDate,年月日)的距离
- ZonedDateTime :包含时区的时间
- ZoneOffset :时区偏移量,比如:+8:00
- Clock :时钟,比如获取目前美国纽约的时间。
Java中使用的历法是ISO 8601日历系统,它是世界民用历法,也就是我们所说的公历。平年有365天,闰年是366
天。此外Java 8还提供了4套其他历法,分别是:
- ThaiBuddhistDate:泰国佛教历
- MinguoDate:中华民国历
- JapaneseDate:日本历
- HijrahDate:伊斯兰历
国际标准化组织的国际标准ISO 8601是日期和时间的表示方法,全称为《数据存储和交换形式·信息交换·日期和时间的表示方法》
原文如下:
日期和时间的组合表示法编辑
合并表示时,要在时间前面加一大写字母T,如要表示北京时间2004年5月3日下午5点30分8秒,可以写成2004-05-03T17:30:08+08:00或20040503T173008+08。
所以这个T date和time合并表示时,中间加个T。
2.1 日期时间的常见操作
LocalDate,LocalTime以及LocalDateTime的操作。
/**
* JDK8 日期时间操作
*/
@Test
public void test01(){
// 1.创建指定的日期
LocalDate date1 = LocalDate.of(2021, 05, 06);
System.out.println("date1 = "+date1);//2021-05-06
// 2.得到当前的日期
LocalDate now = LocalDate.now();
System.out.println("now = "+now);//2021-05-27
// 3.根据LocalDate对象获取对应的日期信息
System.out.println("年:" + now.getYear());//2021
System.out.println("月:" + now.getMonth().getValue());//5
System.out.println("日:" + now.getDayOfMonth());//27
System.out.println("星期:" + now.getDayOfWeek().getValue());//4
LocalDate today = LocalDate.now();
if(today.isLeapYear()){
System.out.println("This year is 闰年");
}else {
System.out.println("2018 is not a 闰年");
}
}
/**
* 时间操作
*/
@Test
public void test02(){
// 1.得到指定的时间
LocalTime time = LocalTime.of(5,26,33,23145);
System.out.println(time);//05:26:33.23145
// 2.获取当前的时间
LocalTime now = LocalTime.now();
System.out.println(now);//15:27:09.617
// 3.获取时间信息
System.out.println(now.getHour());//15
System.out.println(now.getMinute());//27
System.out.println(now.getSecond());//9
System.out.println(now.getNano());//617000000
}
/**
* 日期时间类型 LocalDateTime
*/
@Test
public void test03(){
// 获取指定的日期时间
LocalDateTime dateTime =
LocalDateTime.of(2020
, 06
, 01
, 12
, 12
, 33
, 213);
System.out.println(dateTime);//2020-06-01T12:12:33.000000213
// 获取当前的日期时间
LocalDateTime now = LocalDateTime.now();
System.out.println(now);//2022-06-13T10:06:52.443
// 获取日期时间信息
System.out.println(now.getYear());//2022
System.out.println(now.getMonth().getValue());//6
System.out.println(now.getDayOfMonth());//13
System.out.println(now.getDayOfWeek().getValue());//1
System.out.println(now.getHour());//10
System.out.println(now.getMinute());//6
System.out.println(now.getSecond());//52
System.out.println(now.getNano());//443000000
}
/**
* Clock时钟类
*/
@Test
public void test04(){
// Returns the current time based on your system clock and set to UTC.
Clock clock = Clock.systemUTC();
System.out.println("Clock : " + clock.millis());
// Returns time based on system clock zone
Clock defaultClock = Clock.systemDefaultZone();
System.out.println("Clock : " + defaultClock.millis());
}
2.2 日期时间的修改和比较
/**
* 日期时间的修改
*/
@Test
public void test01(){
LocalDateTime now = LocalDateTime.now();
System.out.println("now = "+now);//now = 2022-06-13T10:25:44.169
// 修改日期时间 对日期时间的修改,对已存在的LocalDate对象,创建了它模板
// 并不会修改原来的信息
LocalDateTime localDateTime = now.withYear(1998);
System.out.println("now :"+now);//now :2022-06-13T10:25:44.169
System.out.println("修改后的:" + localDateTime);//1998-06-13T10:25:44.169
System.out.println("月份:" + now.withMonth(10));//2022-10-13T10:25:44.169
System.out.println("天:" + now.withDayOfMonth(6));//2022-06-06T10:25:44.169
System.out.println("小时:" + now.withHour(8));//2022-06-13T08:25:44.169
System.out.println("分钟:" + now.withMinute(15));//2022-06-13T10:15:44.169
// 在当前日期时间的基础上 加上或者减去指定的时间
System.out.println("两天后:" + now.plusDays(2));//2022-06-15T10:25:44.169
System.out.println("10年后:"+now.plusYears(10));//2032-06-13T10:25:44.169
System.out.println("6个月后 = " + now.plusMonths(6));//2022-12-13T10:25:44.169
System.out.println("10年前 = " + now.minusYears(10));//2012-06-13T10:25:44.169
System.out.println("半年前 = " + now.minusMonths(6));//2021-12-13T10:25:44.169
System.out.println("一周前 = " + now.minusDays(7));//2022-06-06T10:25:44.169
LocalTime time = LocalTime.now();
LocalTime newTime = time.plusHours(3);
System.out.println("三个小时后的时间为:"+newTime);
LocalDate today = LocalDate.now();
System.out.println("今天的日期为:"+today);
LocalDate nextWeek = today.plus(1, ChronoUnit.WEEKS);
System.out.println("一周后的日期为:"+nextWeek);
LocalDate today = LocalDate.now();
LocalDate previousYear = today.minus(1, ChronoUnit.YEARS);
System.out.println("一年前的日期 : " + previousYear);
LocalDate nextYear = today.plus(1, ChronoUnit.YEARS);
System.out.println("一年后的日期:"+nextYear);
}
/**
* 日期时间的比较
*/
@Test
public void test02(){
LocalDate now = LocalDate.now();
LocalDate date = LocalDate.of(2020, 1, 3);
// 在JDK8中要实现 日期的比较 isAfter isBefore isEqual 通过这几个方法来直接比较
System.out.println(now.isAfter(date)); // true
System.out.println(now.isBefore(date)); // false
System.out.println(now.isEqual(date)); // false
// 检查像生日这种周期性事件
LocalDate date1 = LocalDate.now();
LocalDate date2 = LocalDate.of(2018,2,6);
MonthDay birthday = MonthDay.of(date2.getMonth(),date2.getDayOfMonth());
MonthDay currentMonthDay = MonthDay.from(date1);
if(currentMonthDay.equals(birthday)){
System.out.println("是你的生日");
}else{
System.out.println("你的生日还没有到");
}
}
/**
* 如何表示信用卡到期这类固定日期,答案就在YearMonth
* 与 MonthDay检查重复事件的例子相似,YearMonth是另一个组合类,用于表示信用卡到期日、FD到期日、期货期权到 * 期日等。还可以用这个类得到 当月共有多少天,YearMonth实例的lengthOfMonth()方法可以返回当月的天数,在判 * 断2月有28天还是29天时非常有用。
*/
@Test
public void test03(){
YearMonth currentYearMonth = YearMonth.now();
System.out.printf("Days in month year %s: %d%n", currentYearMonth, currentYearMonth.lengthOfMonth()); //2022-07: 31
YearMonth creditCardExpiry = YearMonth.of(2019, Month.FEBRUARY);
System.out.printf("Your credit card expires on %s %n", creditCardExpiry); //2019-02
}
注意:在进行日期时间修改的时候,原来的LocalDate对象是不会被修改,每次操作都是返回了一个新的LocalDate对象,所以在多线程场景下是数据安全的。
2.3 格式化和解析操作
在JDK8中我们可以通过java.time.format.DateTimeFormatter
类可以进行日期的解析和格式化操作
/**
* 日期时间格式化
*/
@Test
public void test01(){
LocalDateTime now = LocalDateTime.now();
// 指定格式 使用系统默认的格式 2021-05-27T16:16:38.139
DateTimeFormatter isoLocalDateTime = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
// 将日期时间转换为字符串
String format = now.format(isoLocalDateTime);
System.out.println("format = " + format);//2022-06-13T11:00:50.967
// 通过 ofPattern 方法来指定特定的格式
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format1 = now.format(dateTimeFormatter);
System.out.println("format1 = " + format1);// 2021-05-27 16:16:38
// 将字符串解析为一个 日期时间类型
LocalDateTime parse = LocalDateTime.parse("1997-05-06 22:45:16", dateTimeFormatter); System.out.println("parse = " + parse);// parse = 1997-05-06T22:45:16
}
/**
* 日期格式化
*/
@Test
public void test01(){
LocalDateTime now = LocalDateTime.now();
// 通过 ofPattern 方法来指定特定的格式
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format1 = now.format(dateTimeFormatter);
System.out.println("format1 = " + format1);// 2021-05-27 16:16:38
// 将字符串解析为一个 日期时间类型
LocalDateTime parse = LocalDateTime.parse("1997-05-06 22:45:16", dateTimeFormatter);
System.out.println("parse = " + parse);// parse = 1997-05-06T22:45:16
LocalDate parse2 = LocalDate.parse("1997-05-06 22:45:16", dateTimeFormatter);
System.out.println("parse2 = " + parse2);// parse2 = 1997-05-06
LocalDate formatted = LocalDate.parse("20180205", DateTimeFormatter.BASIC_ISO_DATE);
System.out.println("格式化后的日期为: "+formatted); //2018-02-05
}
2.4 Instant类
在JDK8中给我们新增一个Instant类(时间戳/时间线),内部保存了从1970年1月1日 00:00:00以来的秒和纳秒。
时间戳信息里同时包含了日期和时间,这和java.util.Date很像。实际上Instant类确实等同于 Java 8之前的Date类,你可以使用Date类和Instant类各自的转换方法互相转换,例如:Date.from(Instant) 将Instant转换成java.util.Date,Date.toInstant()则是将Date类转换成Instant类。
/**
* Instant 时间戳
* 可以用来统计时间消耗
*/
@Test
public void test01() throws Exception{
Instant now = Instant.now();
System.out.println("now = " + now);//now = 2022-06-13T03:03:49.632Z
// 获取从1970年一月一日 00:00:00 到现在的 纳秒
System.out.println(now.getNano());
Thread.sleep(5);
Instant now1 = Instant.now();
System.out.println("耗时:" + (now1.getNano() - now.getNano()));
}
2.5 计算日期时间差
JDK8中提供了两个工具类Duration/Period:计算日期时间差
- Duration:用来计算两个时间差(LocalTime)
- Period:用来计算两个日期差(LocalDate)
Duration:期间、Period:时期
/**
* 计算日期时间差
*/
@Test
public void test01(){
// 计算时间差
LocalTime now = LocalTime.now();
LocalTime time = LocalTime.of(22, 48, 59);
System.out.println("now = " + now);
// 通过Duration来计算时间差
Duration duration = Duration.between(now, time);
System.out.println(duration.toDays()); // 0
System.out.println(duration.toHours()); // 6
System.out.println(duration.toMinutes()); // 368
System.out.println(duration.toMillis()); // 22124240
// 计算日期差
LocalDate nowDate = LocalDate.now();// 2022-7-22
LocalDate date = LocalDate.of(2010, 3, 5);
//相差了几个年 零 几个月 零 几个日
Period period = Period.between(date, nowDate);
System.out.println(period.getYears()); // 12
System.out.println(period.getMonths()); // 4
System.out.println(period.getDays()); // 17
}
2.6 时间校正器
有时候我们可以需要如下调整:将日期调整到"下个月的第一天"等操作。这时我们通过时间校正器效果可能会更好。
- TemporalAdjuster:时间校正器
- TemporalAdjusters:通过该类静态方法提供了大量的常用TemporalAdjuster的实现。
/**
* 时间校正器
*/
@Test
public void test02(){
LocalDateTime now = LocalDateTime.now();
// 将当前的日期调整到下个月的一号
TemporalAdjuster adJuster = (temporal)->{
LocalDateTime dateTime = (LocalDateTime) temporal;
LocalDateTime nextMonth = dateTime.plusMonths(1).withDayOfMonth(1);
System.out.println("nextMonth = " + nextMonth);
return nextMonth;
};
// 我们可以通过TemporalAdjusters 来实现
// LocalDateTime nextMonth = now.with(adJuster);
LocalDateTime nextMonth = now.with(TemporalAdjusters.firstDayOfNextMonth());
System.out.println("nextMonth = " + nextMonth);
}
2.7 日期时间的时区
Java8 中加入了对时区的支持,LocalDate、LocalTime、LocalDateTime是不带时区的,带时区的日期时间类分别为:ZonedDate、ZonedTime、ZonedDateTime。
其中每个时区都对应着 ID,ID的格式为 “区域/城市” 。例如 :Asia/Shanghai 等。
ZoneId:该类中包含了所有的时区信息
/**
* 时区操作
*/
@Test
public void test01(){
// 1.获取所有的时区id
// ZoneId.getAvailableZoneIds().forEach(System.out::println);
// 获取当前时间 中国使用的 东八区的时区,比标准时间早8个小时
LocalDateTime now = LocalDateTime.now();
System.out.println("now = " + now); // 2022-07-22T14:16:27.434
// 获取标准时间
ZonedDateTime bz = ZonedDateTime.now(Clock.systemUTC());
System.out.println("bz = " + bz); // 2022-07-22T06:16:27.436Z
// 使用计算机默认的时区,创建日期时间
ZonedDateTime now1 = ZonedDateTime.now();
System.out.println("now1 = " + now1); //2022-07-22T14:16:27.436+08:00[Asia/Shanghai]
// 使用指定的时区创建日期时间
ZonedDateTime now2 = ZonedDateTime.now(ZoneId.of("America/Marigot"));
System.out.println("now2 = " + now2);//2022-07-22T02:16:27.436-04:00[America/Marigot]
// 把本时区的时间转换成另一个时区的时间。
ZoneId america = ZoneId.of("America/New_York");
LocalDateTime localtDateAndTime = LocalDateTime.now();
ZonedDateTime dateAndTimeInNewYork = ZonedDateTime.of(localtDateAndTime, america );
System.out.println("Current date and time in a particular timezone : " + dateAndTimeInNewYork);
}
JDK新的日期和时间API的优势:
- 新版日期时间API中,日期和时间对象是不可变,操作日期不会影响原来的值,而是生成一个新的实例
- 提供不同的两种方式,有效的区分了人和机器的操作
- TemporalAdjuster可以更精确的操作日期,还可以自定义日期调整期
- 线程安全