日期与时间
标准库 API
Java 标准库有两套处理日期和时间的 API
- 一套定义在 java.util 这个包里面,主要包括 Date 、Calendar 和 TimeZone 这几个类
- 一套新的 API 是在 Java 8 引入的,定义在 java.time 这个包里面,主要包括 LocalDateTime 、ZonedDateTime 、ZoneId 等
旧 API
以十六进制形式和以美元货币格式打印 123400
package org.example;
import java.text.NumberFormat;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
int n = 123400;
System.out.println(Integer.toHexString(n)); // 1e208
System.out.println(NumberFormat.getCurrencyInstance(Locale.US).format(n)); // $123,400.00
}
}
在 Java 程序中,时间戳(Epoch Time)通常是用 long 表示的毫秒数,要获取当前时间戳,可以使用System.currentTimeMillis() ,这是 Java 程序获取时间戳最常用的方法。
Date
java.util.Date 是用于表示一个日期和时间的对象,注意与 java.sql.Date 区分,后者用在数据库中。如果观察 Date 的源码,可以发现它实际上存储了一个 long 类型的以毫秒表示的时间戳:
public class Date implements Serializable, Cloneable, Comparable<Date> {
private transient long fastTime;
...
}
Date 的基本用法
package org.example;
import java.util.Date;
public class Main {
public static void main(String[] args) {
// 获取当前时间:
Date date = new Date();
System.out.println(date.getYear() + 1900); // 必须加上 1900 ,已弃用
System.out.println(date.getMonth() + 1); // 0 ~ 11 ,必须加上 1 ,已弃用
System.out.println(date.getDate()); // 1 ~ 31 ,不能加 1 ,已弃用
// 转换为String:
System.out.println(date.toString());
// 转换为GMT时区:
System.out.println(date.toGMTString()); // 已弃用
// 转换为本地时区:
System.out.println(date.toLocaleString()); // 已弃用
}
}
打印本地时区表示的日期和时间时,不同的计算机可能会有不同的结果。如果我们想要针对用户的偏好精确地控制日期和时间的格式,就可以使用 SimpleDateFormat 对一个 Date 进行转换。它用预定义的字符串表示格式化:
- yyyy:年
- MM:月
- dd: 日
- HH: 小时
- mm: 分钟
- ss: 秒
我们来看如何以自定义的格式输出
package org.example;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
// 获取当前时间:
Date date = new Date();
var sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(date));
}
}
Java 的格式化预定义了许多不同的格式,我们以 MMM 和 E 为例
package org.example;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
// 获取当前时间:
Date date = new Date();
var sdf = new SimpleDateFormat("E MMM dd, yyyy");
System.out.println(sdf.format(date));
}
}
上述代码在不同的语言环境会打印出类似 Sun Sep 15, 2019 这样的日期。可以从 JDK 文档查看详细的格式说明。一般来说,字母越长,输出越长。以 M 为例,假设当前月份是 9 月:
- M:输出9
- MM:输出09
- MMM:输出Sep
- MMMM:输出September
Date 对象有几个严重的问题:它不能转换时区,除了 toGMTString() 可以按 GMT+0:00 输出外,Date 总是以当前计算机系统的默认时区为基础进行输出。此外,我们也很难对日期和时间进行加减,计算两个日期相差多少天,计算某个月第一个星期一的日期等。
Calendar
Calendar 可以用于获取并设置年、月、日、时、分、秒,它和 Date 比,主要多了一个可以做简单的日期和时间运算的功能。来看 Calendar 的基本用法
package org.example;
import java.util.Calendar;
public class Main {
public static void main(String[] args) {
// 获取当前时间:
Calendar c = Calendar.getInstance();
int y = c.get(Calendar.YEAR);
int m = 1 + c.get(Calendar.MONTH);
int d = c.get(Calendar.DAY_OF_MONTH);
int w = c.get(Calendar.DAY_OF_WEEK);
int hh = c.get(Calendar.HOUR_OF_DAY);
int mm = c.get(Calendar.MINUTE);
int ss = c.get(Calendar.SECOND);
int ms = c.get(Calendar.MILLISECOND);
System.out.println(y + "-" + m + "-" + d + " " + w + " " + hh + ":" + mm + ":" + ss + "." + ms);
}
}
注意到 Calendar 获取年月日这些信息变成了 get(int field) ,返回的年份不必转换,返回的月份仍然要加 1 ,返回的星期要特别注意,1 ~ 7 分别表示周日,周一,……,周六。
Calendar 只有一种方式获取,即 Calendar.getInstance() ,而且一获取到就是当前时间。如果我们想给它设置成特定的一个日期和时间,就必须先清除所有字段
package org.example;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Main {
public static void main(String[] args) {
// 当前时间:
Calendar c = Calendar.getInstance();
// 清除所有:
c.clear();
// 设置2019年:
c.set(Calendar.YEAR, 2019);
// 设置9月:注意8表示9月:
c.set(Calendar.MONTH, 8);
// 设置2日:
c.set(Calendar.DATE, 2);
// 设置时间:
c.set(Calendar.HOUR_OF_DAY, 21);
c.set(Calendar.MINUTE, 22);
c.set(Calendar.SECOND, 23);
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(c.getTime()));
// 2019-09-02 21:22:23
}
}
利用 Calendar.getTime() 可以将一个 Calendar 对象转换成 Date 对象,然后就可以用 SimpleDateFormat 进行格式化了。
TimeZone
Calendar 和 Date 相比,它提供了时区转换的功能。时区用 TimeZone 对象表示
package org.example;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) {
TimeZone tzDefault = TimeZone.getDefault(); // 当前时区
TimeZone tzGMT9 = TimeZone.getTimeZone("GMT+09:00"); // GMT+9:00时区
TimeZone tzNY = TimeZone.getTimeZone("America/New_York"); // 纽约时区
System.out.println(tzDefault.getID()); // Asia/Shanghai
System.out.println(tzGMT9.getID()); // GMT+09:00
System.out.println(tzNY.getID()); // America/New_York
}
}
时区的唯一标识是以字符串表示的 ID ,我们获取指定 TimeZone 对象也是以这个 ID 为参数获取,GMT+09:00 、Asia/Shanghai 都是有效的时区 ID 。要列出系统支持的所有 ID ,请使用 TimeZone.getAvailableIDs() 。
有了时区,我们就可以对指定时间进行转换。例如,下面的例子演示了如何将北京时间 2019-11-20 8:15:00 转换为纽约时间
package org.example;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) {
// 当前时间:
Calendar c = Calendar.getInstance();
// 清除所有:
c.clear();
// 设置为北京时区:
c.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
// 设置年月日时分秒:
c.set(2019, 10 /* 11月 */, 20, 8, 15, 0);
// 显示时间:
var sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
System.out.println(sdf.format(c.getTime()));
// 2019-11-19 19:15:00
}
}
可见,利用 Calendar 进行时区转换的步骤是:
- 清除所有字段
- 设定指定时区;
- 设定日期和时间
- 创建 SimpleDateFormat 并设定目标时区
- 格式化获取的 Date 对象(注意 Date 对象无时区信息,时区信息存储在 SimpleDateFormat 中)
因此,本质上时区转换只能通过 SimpleDateFormat 在显示的时候完成。
Calendar 也可以对日期和时间进行简单的加减
package org.example;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Main {
public static void main(String[] args) {
// 当前时间:
Calendar c = Calendar.getInstance();
// 清除所有:
c.clear();
// 设置年月日时分秒:
c.set(2019, 10 /* 11月 */, 20, 8, 15, 0);
// 加5天并减去2小时:
c.add(Calendar.DAY_OF_MONTH, 5);
c.add(Calendar.HOUR_OF_DAY, -2);
// 显示时间:
var sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = c.getTime();
System.out.println(sdf.format(d));
// 2019-11-25 6:15:00
}
}
新 API
从 Java 8 开始,java.time 包提供了新的日期和时间 API ,主要涉及的类型有:
- 本地日期和时间:LocalDateTime ,LocalDate ,LocalTime
- 带时区的日期和时间:ZonedDateTime
- 时刻:Instant
- 时区:ZoneId,ZoneOffset
- 时间间隔:Duration
以及一套新的用于取代 SimpleDateFormat 的格式化类型 DateTimeFormatter 。
和旧的 API 相比,新 API 严格区分了时刻、本地日期、本地时间和带时区的日期时间,并且,对日期和时间进行运算更加方便。
此外,新 API 修正了旧 API 不合理的常量设计:
- Month 的范围用 1~12 表示1月到12月
- Week 的范围用 1~7 表示周一到周日
最后,新 API 的类型几乎全部是不变类型(和 String 类似),可以放心使用不必担心被修改。
LocalDateTime
最常用的 LocalDateTime ,它表示一个本地日期和时间
package org.example;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class Main {
public static void main(String[] args) {
LocalDate d = LocalDate.now(); // 当前日期
LocalTime t = LocalTime.now(); // 当前时间
LocalDateTime dt = LocalDateTime.now(); // 当前日期和时间
System.out.println(d); // 严格按照ISO 8601格式打印
System.out.println(t); // 严格按照ISO 8601格式打印
System.out.println(dt); // 严格按照ISO 8601格式打印
}
}
本地日期和时间通过 now() 获取到的总是以当前默认时区返回的,和旧 API 不同,LocalDateTime 、LocalDate 和 LocalTime 默认严格按照 ISO 8601 规定的日期和时间格式进行打印。
上述代码其实有一个小问题,在获取 3 个类型的时候,由于执行一行代码总会消耗一点时间,因此,3 个类型的日期和时间很可能对不上(时间的毫秒数基本上不同)。为了保证获取到同一时刻的日期和时间,可以改写如下:
LocalDateTime dt = LocalDateTime.now(); // 当前日期和时间
LocalDate d = dt.toLocalDate(); // 转换到当前日期
LocalTime t = dt.toLocalTime(); // 转换到当前时间
反过来,通过指定的日期和时间创建 LocalDateTime 可以通过 of() 方法:
// 指定日期和时间:
LocalDate d2 = LocalDate.of(2019, 11, 30); // 2019-11-30, 注意11=11月
LocalTime t2 = LocalTime.of(15, 16, 17); // 15:16:17
LocalDateTime dt2 = LocalDateTime.of(2019, 11, 30, 15, 16, 17);
LocalDateTime dt3 = LocalDateTime.of(d2, t2);
因为严格按照 ISO 8601 的格式,因此,将字符串转换为 LocalDateTime 就可以传入标准格式:
LocalDateTime dt = LocalDateTime.parse("2019-11-19T15:16:17");
LocalDate d = LocalDate.parse("2019-11-19");
LocalTime t = LocalTime.parse("15:16:17");
注意 ISO 8601 规定的日期和时间分隔符是T。标准格式如下:
- 日期:yyyy-MM-dd
- 时间:HH:mm:ss
- 带毫秒的时间:HH:mm:ss.SSS
- 日期和时间:yyyy-MM-dd'T'HH:mm:ss
- 带毫秒的日期和时间:yyyy-MM-dd'T'HH:mm:ss.SSS
LocalDateTime 提供了对日期和时间进行加减的非常简单的链式调用
package org.example;
import java.time.LocalDateTime;
public class Main {
public static void main(String[] args) {
LocalDateTime dt = LocalDateTime.of(2019, 10, 26, 20, 30, 59);
System.out.println(dt);
// 加5天减3小时:
LocalDateTime dt2 = dt.plusDays(5).minusHours(3);
System.out.println(dt2); // 2019-10-31T17:30:59
// 减1月:
LocalDateTime dt3 = dt2.minusMonths(1);
System.out.println(dt3); // 2019-09-30T17:30:59
}
}
注意到月份加减会自动调整日期,例如从 2019-10-31 减去 1 个月得到的结果是 2019-09-30 ,因为9月没有31日。
对日期和时间进行调整则使用 withXxx() 方法,例如:withHour(15) 会把 10:11:12 变为15:11:12 :
- 调整年:withYear()
- 调整月:withMonth()
- 调整日:withDayOfMonth()
- 调整时:withHour()
- 调整分:withMinute()
- 调整秒:withSecond()
示例代码如下
package org.example;
import java.time.LocalDateTime;
public class Main {
public static void main(String[] args) {
LocalDateTime dt = LocalDateTime.of(2019, 10, 26, 20, 30, 59);
System.out.println(dt);
// 日期变为31日:
LocalDateTime dt2 = dt.withDayOfMonth(31);
System.out.println(dt2); // 2019-10-31T20:30:59
// 月份变为9:
LocalDateTime dt3 = dt2.withMonth(9);
System.out.println(dt3); // 2019-09-30T20:30:59
}
}
同样注意到调整月份时,会相应地调整日期,即把 2019-10-31 的月份调整为 9 时,日期也自动变为 30 。
实际上,LocalDateTime 还有一个通用的 with() 方法允许我们做更复杂的运算。例如
package org.example;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
public class Main {
public static void main(String[] args) {
// 本月第一天0:00时刻:
LocalDateTime firstDay = LocalDate.now().withDayOfMonth(1).atStartOfDay();
System.out.println(firstDay);
// 本月最后1天:
LocalDate lastDay = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());
System.out.println(lastDay);
// 下月第1天:
LocalDate nextMonthFirstDay = LocalDate.now().with(TemporalAdjusters.firstDayOfNextMonth());
System.out.println(nextMonthFirstDay);
// 本月第1个周一:
LocalDate firstWeekday = LocalDate.now().with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
System.out.println(firstWeekday);
}
}
对于计算某个月第 1 个周日这样的问题,新的 API 可以轻松完成。
要判断两个 LocalDateTime 的先后,可以使用 isBefore() 、isAfter() 方法,对于 LocalDate 和 LocalTime 类似
package org.example;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class Main {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime target = LocalDateTime.of(2019, 11, 19, 8, 15, 0);
System.out.println(now.isBefore(target));
System.out.println(LocalDate.now().isBefore(LocalDate.of(2019, 11, 19)));
System.out.println(LocalTime.now().isAfter(LocalTime.parse("08:15:00")));
}
}
注意到 LocalDateTime 无法与时间戳进行转换,因为 LocalDateTime 没有时区,无法确定某一时刻。后面我们要介绍的 ZonedDateTime 相当于 LocalDateTime 加时区的组合,它具有时区,可以与 long 表示的时间戳进行转换。
Duration 和 Period
Duration 表示两个时刻之间的时间间隔。另一个类似的 Period 表示两个日期之间的天数
package org.example;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
public class Main {
public static void main(String[] args) {
LocalDateTime start = LocalDateTime.of(2019, 11, 19, 8, 15, 0);
LocalDateTime end = LocalDateTime.of(2020, 1, 9, 19, 25, 30);
Duration d = Duration.between(start, end);
System.out.println(d); // PT1235H10M30S
Period p = LocalDate.of(2019, 11, 19).until(LocalDate.of(2020, 1, 9));
System.out.println(p); // P1M21D
}
}
注意到两个 LocalDateTime 之间的差值使用 Duration 表示,类似 PT1235H10M30S ,表示1235小时10分钟30秒。而两个 LocalDate 之间的差值用 Period 表示,类似 P1M21D ,表示1个月21天。
Duration 和 Period 的表示方法也符合 ISO 8601 的格式,它以 P...T... 的形式表示,P...T 之间表示日期间隔,T 后面表示时间间隔。如果是 PT... 的格式表示仅有时间间隔。利用 ofXxx() 或者 parse() 方法也可以直接创建 Duration :
Duration d1 = Duration.ofHours(10); // 10 hours
Duration d2 = Duration.parse("P1DT2H3M"); // 1 day, 2 hours, 3 minutes
有的童鞋可能发现 Java 8 引入的 java.timeAPI ,怎么和一个开源的 Joda Time 很像?难道 JDK 也开始抄袭开源了?其实正是因为开源的 Joda Time 设计很好,应用广泛,所以 JDK 团队邀请 Joda Time 的作者 Stephen Colebourne 共同设计了 java.time API 。
ZonedDateTime
LocalDateTime 总是表示本地日期和时间,要表示一个带时区的日期和时间,我们就需要 ZonedDateTime 。
可以简单地把 ZonedDateTime 理解成 LocalDateTime 加 ZoneId 。ZoneId 是 java.time 引入的新的时区类,注意和旧的 java.util.TimeZone 区别。
要创建一个 ZonedDateTime 对象,有以下几种方法,一种是通过 now() 方法返回当前时间
package org.example;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Main {
public static void main(String[] args) {
ZonedDateTime zbj = ZonedDateTime.now(); // 默认时区
ZonedDateTime zny = ZonedDateTime.now(ZoneId.of("America/New_York")); // 用指定时区获取当前时间
System.out.println(zbj);
System.out.println(zny);
}
}
观察打印的两个 ZonedDateTime ,发现它们时区不同,但表示的时间都是同一时刻(毫秒数不同是执行语句时的时间差):
2019-09-15T20:58:18.786182+08:00[Asia/Shanghai]
2019-09-15T08:58:18.788860-04:00[America/New_York]
另一种方式是通过给一个 LocalDateTime 附加一个 ZoneId ,就可以变成 ZonedDateTime
package org.example;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Main {
public static void main(String[] args) {
LocalDateTime ldt = LocalDateTime.of(2019, 9, 15, 15, 16, 17);
ZonedDateTime zbj = ldt.atZone(ZoneId.systemDefault());
ZonedDateTime zny = ldt.atZone(ZoneId.of("America/New_York"));
System.out.println(zbj);
System.out.println(zny);
}
}
以这种方式创建的 ZonedDateTime ,它的日期和时间与 LocalDateTime 相同,但附加的时区不同,因此是两个不同的时刻:
2019-09-15T15:16:17+08:00[Asia/Shanghai]
2019-09-15T15:16:17-04:00[America/New_York]
时区转换
要转换时区,首先我们需要有一个 ZonedDateTime 对象,然后,通过 withZoneSameInstant() 将关联时区转换到另一个时区,转换后日期和时间都会相应调整。下面的代码演示了如何将北京时间转换为纽约时间
package org.example;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Main {
public static void main(String[] args) {
// 以中国时区获取当前时间:
ZonedDateTime zbj = ZonedDateTime.now(ZoneId.of("Asia/Shanghai"));
// 转换为纽约时间:
ZonedDateTime zny = zbj.withZoneSameInstant(ZoneId.of("America/New_York"));
System.out.println(zbj);
System.out.println(zny);
}
}
要特别注意,时区转换的时候,由于夏令时的存在,不同的日期转换的结果很可能是不同的。这是北京时间9月15日的转换结果:
2019-09-15T21:05:50.187697+08:00[Asia/Shanghai]
2019-09-15T09:05:50.187697-04:00[America/New_York]
这是北京时间11月15日的转换结果:
2019-11-15T21:05:50.187697+08:00[Asia/Shanghai]
2019-11-15T08:05:50.187697-05:00[America/New_York]
两次转换后的纽约时间有1小时的夏令时时差。
涉及到时区时,千万不要自己计算时差,否则难以正确处理夏令时。
有了 ZonedDateTime ,将其转换为本地时间就非常简单:
ZonedDateTime zdt = ...
LocalDateTime ldt = zdt.toLocalDateTime();
转换为 LocalDateTime 时,直接丢弃了时区信息。
DateTimeFormatter
如果要自定义输出的格式,或者要把一个非 ISO 8601 格式的字符串解析成 LocalDateTime ,可以使用新的 DateTimeFormatter
package org.example;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
// 自定义格式化:
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
System.out.println(dtf.format(LocalDateTime.now()));
// 用自定义格式解析:
LocalDateTime dt2 = LocalDateTime.parse("2019/11/30 15:16:17", dtf);
System.out.println(dt2);
}
}
使用旧的 Date 对象时,我们用 SimpleDateFormat 进行格式化显示。使用新的 LocalDateTime 或 ZonedDateTime 时,我们要进行格式化显示,就要使用 DateTimeFormatter 。
和 SimpleDateFormat 不同的是,DateTimeFormatter 不但是不变对象,它还是线程安全的。线程的概念我们会在后面涉及到。现在我们只需要记住:因为 SimpleDateFormat 不是线程安全的,使用的时候,只能在方法内部创建新的局部变量。而 DateTimeFormatter 可以只创建一个实例,到处引用。
创建 DateTimeFormatter 时,我们仍然通过传入格式化字符串实现:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
格式化字符串的使用方式与 SimpleDateFormat 完全一致。
另一种创建 DateTimeFormatter 的方法是,传入格式化字符串时,同时指定 Locale :
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E, yyyy-MMMM-dd HH:mm", Locale.US);
这种方式可以按照 Locale 默认习惯格式化。我们来看实际效果
package org.example;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
ZonedDateTime zdt = ZonedDateTime.now();
var formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm ZZZZ");
System.out.println(formatter.format(zdt));
var zhFormatter = DateTimeFormatter.ofPattern("yyyy MMM dd EE HH:mm", Locale.CHINA);
System.out.println(zhFormatter.format(zdt));
var usFormatter = DateTimeFormatter.ofPattern("E, MMMM/dd/yyyy HH:mm", Locale.US);
System.out.println(usFormatter.format(zdt));
}
}
在格式化字符串中,如果需要输出固定字符,可以用'xxx'表示。
运行上述代码,分别以默认方式、中国地区和美国地区对当前时间进行显示,结果如下:
2019-09-15T23:16 GMT+08:00
2019 9月 15 周日 23:16
Sun, September/15/2019 23:16
当我们直接调用 System.out.println() 对一个 ZonedDateTime 或者 LocalDateTime 实例进行打印的时候,实际上,调用的是它们的 toString() 方法,默认的 toString() 方法显示的字符串就是按照 ISO 8601 格式显示的,我们可以通过 DateTimeFormatter 预定义的几个静态变量来引用:
var ldt = LocalDateTime.now();
System.out.println(DateTimeFormatter.ISO_DATE.format(ldt));
System.out.println(DateTimeFormatter.ISO_DATE_TIME.format(ldt));
得到的输出和 toString() 类似:
2019-09-15
2019-09-15T23:16:51.56217
Instant
我们已经讲过,计算机存储的当前时间,本质上只是一个不断递增的整数。Java 提供的 System.currentTimeMillis() 返回的就是以毫秒表示的当前时间戳。
这个当前时间戳在 java.time 中以 Instant 类型表示,我们用 Instant.now() 获取当前时间戳,效果和 System.currentTimeMillis() 类似
package org.example;
import java.time.Instant;
public class Main {
public static void main(String[] args) {
Instant now = Instant.now();
System.out.println(now.getEpochSecond()); // 秒
System.out.println(now.toEpochMilli()); // 毫秒
}
}
打印的结果类似:
1568568760
1568568760316
实际上,Instant 内部只有两个核心字段:
public final class Instant implements ... {
private final long seconds;
private final int nanos;
}
一个是以秒为单位的时间戳,一个是更精确的纳秒精度。它和 System.currentTimeMillis() 返回的 long 相比,只是多了更高精度的纳秒。
既然 Instant 就是时间戳,那么,给它附加上一个时区,就可以创建出 ZonedDateTime :
// 以指定时间戳创建Instant:
Instant ins = Instant.ofEpochSecond(1568568760);
ZonedDateTime zdt = ins.atZone(ZoneId.systemDefault());
System.out.println(zdt); // 2019-09-16T01:32:40+08:00[Asia/Shanghai]
可见,对于某一个时间戳,给它关联上指定的 ZoneId ,就得到了 ZonedDateTime ,继而可以获得了对应时区的 LocalDateTime 。
所以,LocalDateTime ,ZoneId ,Instant ,ZonedDateTime 和 long 都可以互相转换:

转换的时候,只需要留意 long 类型以毫秒还是秒为单位即可。
最佳实践
由于 Java 提供了新旧两套日期和时间的 API ,除非涉及到遗留代码,否则我们应该坚持使用新的 API 。如果需要与遗留代码打交道,如何在新旧 API 之间互相转换呢?
旧 API 转新 API
如果要把旧式的 Date 或 Calendar 转换为新 API 对象,可以通过 toInstant() 方法转换为 Instant 对象,再继续转换为 ZonedDateTime :
// Date -> Instant:
Instant ins1 = new Date().toInstant();
// Calendar -> Instant -> ZonedDateTime:
Calendar calendar = Calendar.getInstance();
Instant ins2 = calendar.toInstant();
ZonedDateTime zdt = ins2.atZone(calendar.getTimeZone().toZoneId());
从上面的代码还可以看到,旧的 TimeZone 提供了一个 toZoneId() ,可以把自己变成新的 ZoneId 。
新 API 转旧 API
如果要把新的 ZonedDateTime 转换为旧的 API 对象,只能借助 long 型时间戳做一个“中转”:
// ZonedDateTime -> long:
ZonedDateTime zdt = ZonedDateTime.now();
long ts = zdt.toEpochSecond() * 1000;
// long -> Date:
Date date = new Date(ts);
// long -> Calendar:
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.setTimeZone(TimeZone.getTimeZone(zdt.getZone().getId()));
calendar.setTimeInMillis(zdt.toEpochSecond() * 1000);
从上面的代码还可以看到,新的 ZoneId 转换为旧的 TimeZone ,需要借助 ZoneId.getId() 返回的 String 完成。
在数据库中存储日期和时间
除了旧式的 java.util.Date ,我们还可以找到另一个 java.sql.Date ,它继承自 java.util.Date ,但会自动忽略所有时间相关信息。这个奇葩的设计原因要追溯到数据库的日期与时间类型。
在数据库中,也存在几种日期和时间类型:
- DATETIME:表示日期和时间
- DATE:仅表示日期
- TIME:仅表示时间
- TIMESTAMP:和 DATETIME 类似,但是数据库会在创建或者更新记录的时候同时修改 TIMESTAMP
在使用 Java 程序操作数据库时,我们需要把数据库类型与 Java 类型映射起来。下表是数据库类型与 Java 新旧 API 的映射关系:

实际上,在数据库中,我们需要存储的最常用的是时刻(Instant),因为有了时刻信息,就可以根据用户自己选择的时区,显示出正确的本地时间。所以,最好的方法是直接用长整数 long 表示,在数据库中存储为 BIGINT 类型。
通过存储一个 long 型时间戳,我们可以编写一个 timestampToString() 的方法,非常简单地为不同用户以不同的偏好来显示不同的本地时间:
package org.example;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
long ts = 1574208900000L;
System.out.println(timestampToString(ts, Locale.CHINA, "Asia/Shanghai"));
System.out.println(timestampToString(ts, Locale.US, "America/New_York"));
}
static String timestampToString(long epochMilli, Locale lo, String zoneId) {
Instant ins = Instant.ofEpochMilli(epochMilli);
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.SHORT);
return f.withLocale(lo).format(ZonedDateTime.ofInstant(ins, ZoneId.of(zoneId)));
}
}
对上述方法进行调用,结果如下:
2019年11月20日 上午8:15
Nov 19, 2019, 7:15 PM

浙公网安备 33010602011771号