Java 时间类型转换

前言

Java中表示时间有三种类型:

1
2
3
String dateStr = "2020-01-01 15:00:11"; // String类型
Date date = new Date(); // Date类型
long time = date.getTime(); // long类型

  这三种类型的不同在于,String类型不带时区信息,Date和long类型会带时区信息。开发项目时,接口参数的时间字段最好用long类型表示,这样发送方和接收方收到的时间会一致。用String类型也能一致,但双方可能都要做格式转换。用Date类型做接口参数时,接收方和收到的值可能与发送方的不一致,不适合验签。

Date

Date转String

看一下电脑上的时间,得知当前时间是8时区的2020-02-25 15:41:21(系统时区是8时区)。
按照yyyy-MM-dd HH:mm:ss的格式打印当前时间:

1
2
3
4
Date d = new Date(); // 当前时间
String format = "yyyy-MM-dd HH:mm:ss"; // 注意,月份M大写,分钟m小写
SimpleDateFormat sdf = new SimpleDateFormat(format);
System.out.println(sdf.format(d)); // 2020-02-25 15:41:21

打印结果和电脑上显示的一致。

转换时考虑时区

若想知道当前时间对应的GMT0时间是多少,就要在转换时加上时区:

1
2
3
4
5
6
Date d = new Date(); // 当前时间
String format = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
String timezone = "GMT+0"; // 时区
sdf.setTimeZone(TimeZone.getTimeZone(timezone));
System.out.println(sdf.format(d)); // 2020-02-25 07:41:21

打印结果2020-02-25 07:41:21(GMT0)和当前时间2020-02-25 15:41:21(GMT8)可以对应。

Date转long

1
2
Date date = new Date();
long time = date.getTime();

Long

long转Date

1
2
long currentTimeMillis = System.currentTimeMillis();
Date d = new Date(currentTimeMillis);

long转String

先long转Date,再Date转String。

String

String转Date

若已知时间字符串”2020-02-25 10:44:05”是8时区,系统也是8时区,那么转换时可以简单的写:

1
2
3
4
5
6
7
8
9
10
try {
String timeStr = "2020-02-25 10:44:05";
String format = "yyyy-MM-dd HH:mm:ss"; // 时间字符串的格式
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date = sdf.parse(timeStr);
System.out.println(date); // Tue Feb 25 10:44:05 CST 2020

} catch (ParseException e) {
e.printStackTrace();
}

打印出的CST时间10:44:05和时间字符串2020-02-25 10:44:05(GMT8)一致。

CST时间:China Standard Time,中国标准时间,等于GMT8

转换时考虑时区

但如果已知”2020-02-25 10:44:05”这个字符串是0时区,那么就要指定时区转换:

1
2
3
4
5
6
7
8
9
10
11
12
try {
String timeStr = "2020-02-25 10:44:05";
String format = "yyyy-MM-dd HH:mm:ss"; // 时间字符串的格式
SimpleDateFormat sdf = new SimpleDateFormat(format);
String timezone = "GMT+0"; // 时间字符串的时区
sdf.setTimeZone(TimeZone.getTimeZone(timezone));
Date date = sdf.parse(timeStr);
System.out.println(date); // Tue Feb 25 18:44:05 CST 2020

} catch (ParseException e) {
e.printStackTrace();
}

打印出的CST时间18:44:05和时间字符串2020-02-25 10:44:05(GMT0)一致。

复杂格式的时间字符串转换

处理复杂的时间字符串,如PDT时间

PDT时间:Pacific Daylight Time,太平洋夏季时间,等于UTC-7,UTC=GMT0,因此PDT=GMT8-15
PST时间:Pacific Standard Timee,太平洋标准时间,等于PDT-1,因此PST=GMT8-16

1
2
3
4
5
6
7
8
9
10
try {
String timeStr = "03:00:14 Jun 13, 2019 PDT"; // pdt时间2019-06-13 03:00:14
String format = "HH:mm:ss MMM dd, yyyy z"; // z表示时区
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.ENGLISH); // 当字符串中有英文单词时要加上Locale参数
Date date = sdf.parse(timeStr);
System.out.println(date); // Thu Jun 13 18:00:14 CST 2019

} catch (ParseException e) {
e.printStackTrace();
}

打印出的CST时间18:00:14和时间字符串2019-06-13 03:00:14(PDT)一致。

处理UTC时间(UTC=GMT0):

1
2
3
4
5
6
7
8
9
10
11
12
try {
String timeStr = "2019-04-24T02:30:00";
String format = "yyyy-MM-dd'T'HH:mm:ss"; // 时间格式,注意T带引号
SimpleDateFormat sdf = new SimpleDateFormat(format);
String timezone = "GMT+0"; // 时间字符串的时区
sdf.setTimeZone(TimeZone.getTimeZone(timezone));
Date date = sdf.parse(timeStr);
System.out.println(date); // Wed Apr 24 10:30:00 CST 2019

} catch (ParseException e) {
e.printStackTrace();
}

打印出的CST时间10:30:00和时间字符串2019-04-24 02:30:00(UTC)一致。

String转long

先String转Date,再Date转long。