一张机,织梭光景去如飞:JDK时间变迁
夫天地者,万物之逆旅;光阴者,百代之过客。背景
阿里《java开发手册》中提到:
说明:如果是 JDK8 的应用,可以使用 Instant 代替 Date,LocalDateTime 代替 Calendar,
DateTimeFormatter 代替 SimpleDateFormat,官方给出的解释:simple beautiful strong immutable
thread-safe。JDK8中的时间函数与以前的版本有个根本性的改变,完全废弃了之前关于时间函数的设计。今天就聊聊这个jdk时间的设计变迁过程。这个变迁大致分为三个过程:最初的Date设计(JDK1.0),Date设计的补丁版Calendar(JDK1.1),重新设计(JDK1.8).
Date出世
先从一个实例开始吧!
public static void dateTest() {
Date date=new Date();
System.out.println(date.getYear()+" " + date.getMonth()+ " "+date.getDay());
}打印出的结果是:
122 6 5注意:当前的日期是 2022-07-01
是不是很惊喜?是不是很意外?
翻看Date的说明文档,让我们来看看原因:
/**
* Returns a value that is the result of subtracting 1900 from the
* year that contains or begins with the instant in time represented
* by this Date object, as interpreted in the local
* time zone.
*
* @return the year represented by this date, minus 1900.
* @see java.util.Calendar
* @deprecated As of JDK version 1.1,
* replaced by Calendar.get(Calendar.YEAR) - 1900.
*/
@Deprecated
public int getYear() {
return normalize().getYear() - 1900;
}
/**
* Returns a number representing the month that contains or begins
* with the instant in time represented by this Date object.
* The value returned is between 0 and 11,
* with the value 0 representing January.
*
* @return the month represented by this date.
* @see java.util.Calendar
* @deprecated As of JDK version 1.1,
* replaced by Calendar.get(Calendar.MONTH).
*/
@Deprecated
public int getMonth() {
return normalize().getMonth() - 1; // adjust 1-based to 0-based
}
/**
* Returns the day of the week represented by this date. The
* returned value (0 = Sunday, 1 = Monday,
* 2 = Tuesday, 3 = Wednesday, 4 =
* Thursday, 5 = Friday, 6 = Saturday)
* represents the day of the week that contains or begins with
* the instant in time represented by this Date object,
* as interpreted in the local time zone.
*
* @return the day of the week represented by this date.
* @see java.util.Calendar
* @deprecated As of JDK version 1.1,
* replaced by Calendar.get(Calendar.DAY_OF_WEEK).
*/
@Deprecated
public int getDay() {
return normalize().getDayOfWeek() - BaseCalendar.SUNDAY;
}自此,明白了:年=2022-1900=122,月=6+1=7,日=周五。
Calendar打补丁
在 1.1 版中,Calendar 类被添加到了 Java 平台中,以矫正 Date 的缺点,由此大部分的 Date 方法就都被弃用了。遗憾的是,这么做只能使情况更糟。
public static void calendarTest() {
Calendar cal = Calendar.getInstance();
cal.set(2018, 12, 31); // Year, Month, Day
System.out.print(cal.get(Calendar.YEAR) + " "+cal.get(Calendar.MONTH) + " "+cal.get(Calendar.DAY_OF_MONTH));
}结果显示:
2019 0 31从上面的理解中,月份是从 0 开始的即 0~11 代表 1 月…12 月
接着 day又是从 1 开始的,为什么同一个方法设计的如此怪异?
JDK1.8:破而后立
Java 8的日期和时间类包含Instant、LocalDate、LocalTime、LocalDateTime、Duration以及Period,这些类都包含在java.time包中。
(1)Instant
在 JDK8 中,针对统计时间 等场景,推荐使用 Instant 类来代替Date。如果想获取更加精确的纳秒级时间值,使用 System.nanoTime 的方式。其使用实例如下:
public static void instantTest() {
Instant instant=Instant.now();
System.out.println(instant.getEpochSecond());
//System.out.println(System.currentTimeMillis());
System.out.println(instant.getNano());
}显示结果:
1656896732
236000000(2)使用LocalDate、LocalTime、LocalDateTime来替换Calendar
public static void localDateTimeTest() {
LocalDate date=LocalDate.of(2022, 7, 4);
System.out.println(date.getYear()+" "+date.getMonthValue()+" "+date.getDayOfMonth());
LocalTime time=LocalTime.of(10, 05, 30);
System.out.println(time.getHour()+" "+time.getMinute()+" "+ time.getSecond());
LocalDateTime dtime=LocalDateTime.of(2022, 7, 4, 10, 5,30);
System.out.println(dtime.getYear()+" "+dtime.getMonthValue()+" "+dtime.getDayOfMonth()+
" "+dtime.getHour()+" "+dtime.getMinute());
}结果显示:
2022 7 4
10 5 30
2022 7 4 10 5(3)Duration
表示一个时间段,所以Duration类中不包含now()静态方法。可以通过Duration.between()方法创建Duration对象
public static void durationTest() {
LocalDateTime from = LocalDateTime.of(2022, 7, 1, 10, 7, 0);
LocalDateTime to = LocalDateTime.of(2022, 7, 4, 10, 7, 0);
Duration d = Duration.between(from, to);
System.out.println(d.toDays()+" "+ d.toHours()+" "+d.toMinutes() +" "+d.getSeconds()+" "+ d.getNano());
}结果显示:
3 72 4320 259200 0(4)Period
Period在概念上和Duration类似,区别在于Period是以年月日来衡量一个时间段,比如2年3个月6天
public static void periodTest() {
Period p = Period.between(
LocalDate.of(2022, 7, 1),
LocalDate.of(2022, 7, 4));
System.out.println(p.getYears()+" "+ p.getMonths()+" "+p.getDays());
}结果显示“
0 0 3时间转换
因历史原因,jdk1.8的时间api有时候还是需要转换为Date。
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
public class DateUtils {
//LocalDate 转Date
public static Date asDate(LocalDate localDate) {
return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
}
//LocalDateTime 转Date
public static Date asDate(LocalDateTime localDateTime) {
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
//Date 转LocalDate
public static LocalDate asLocalDate(Date date) {
return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
}
// Date 转LocalDateTime
public static LocalDateTime asLocalDateTime(Date date) {
return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
}
}
小结
JDK1.8的时间进行重新设计,使用Instant、LocalDate、LocalTime、LocalDateTime、Duration以及Period等来替代Date、Calendar设计。使用时要注意:
(1)不要在程序中写死一年为 365 天,避免在公历闰年时出现日期转换错误或程序逻辑错误。使用LocalDate.now().lengthOfYear()获取今年的天数。
(2)不允许在程序任何地方中使用:1)java.sql.Date 2)java.sql.Time 3)java.sql.Timestamp。
(3)DateTimeFormatter 代替 SimpleDateFormat.前者是线程安全的,后者线程不安全。