将 UTC 时间戳转换为本地日期
Converting UTC timestamp to local date
所以我现在尝试了大约几个小时将时间戳转换为本地日期 (CEST)。
Date date = new Date(stamp*1000);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("CEST"));
String myDate = simpleDateFormat.format(date);
无论我尝试过还是在 Internet 上查找,它都不起作用我总是返回 UTC 时间......
为了更好地理解:stamp 是一个类型为 long 的可变时间戳,我将从服务中接收到它
您的 TimeZone
id 可能不正确(好吧,Java 无法识别)。在这种情况下,似乎 TimeZone
被评估为 UTC 而不是抛出异常。
试试这个:
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("ECT"));
Here 是一个页面,提供一些关于 Java 的 TimeZone
和时区 ID 列表的信息。
tl;博士
String output = ZonedDateTime.ofInstant ( Instant.ofEpochSecond ( 1_468_015_200L ) , ZoneId.of ( "Europe/Paris" ) ).toString();
详情
几个问题:
- 您没有使用 proper time zone names。
- 专有名称采用
continent/region
格式。
CEST
等媒体中常见的 3-4 个字母缩写 不是 真正的时区。 避免它们。它们既不标准化也不独特(!)。
- 您使用的是旧的、过时的日期时间 classes,它们设计不佳且令人困惑。它们已被 java.time 框架取代。
如果 CEST
是指在夏季比 UTC 早 2 小时,那么让我们以 Europe/Paris
为例时区。你的问题缺少示例数据,所以我将弥补这个示例。
显然,您输入的是从 1970 年第一时刻开始的整秒计数(UTC)。那个值可以直接使用,不需要相乘。
ZoneId
class 代表时区。 Instant
是 UTC 时间轴上的一个点,分辨率最高为纳秒。一个ZonedDateTime
就是把Instant
调整成ZoneId
。
ZoneId zoneId = ZoneId.of ( "Europe/Paris" );
long input = 1_468_015_200L; // Whole seconds since start of 1970.
Instant instant = Instant.ofEpochSecond ( input );
ZonedDateTime zdt = ZonedDateTime.ofInstant ( instant , zoneId );
转储到控制台。
System.out.println ( "input: " + input + " | instant: " + instant + " | zdt: " + zdt );
input: 1468015200 | instant: 2016-07-08T22:00:00Z | zdt: 2016-07-09T00:00+02:00[Europe/Paris]
所以我现在尝试了大约几个小时将时间戳转换为本地日期 (CEST)。
Date date = new Date(stamp*1000);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("CEST"));
String myDate = simpleDateFormat.format(date);
无论我尝试过还是在 Internet 上查找,它都不起作用我总是返回 UTC 时间......
为了更好地理解:stamp 是一个类型为 long 的可变时间戳,我将从服务中接收到它
您的 TimeZone
id 可能不正确(好吧,Java 无法识别)。在这种情况下,似乎 TimeZone
被评估为 UTC 而不是抛出异常。
试试这个:
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("ECT"));
Here 是一个页面,提供一些关于 Java 的 TimeZone
和时区 ID 列表的信息。
tl;博士
String output = ZonedDateTime.ofInstant ( Instant.ofEpochSecond ( 1_468_015_200L ) , ZoneId.of ( "Europe/Paris" ) ).toString();
详情
几个问题:
- 您没有使用 proper time zone names。
- 专有名称采用
continent/region
格式。 CEST
等媒体中常见的 3-4 个字母缩写 不是 真正的时区。 避免它们。它们既不标准化也不独特(!)。
- 专有名称采用
- 您使用的是旧的、过时的日期时间 classes,它们设计不佳且令人困惑。它们已被 java.time 框架取代。
如果 CEST
是指在夏季比 UTC 早 2 小时,那么让我们以 Europe/Paris
为例时区。你的问题缺少示例数据,所以我将弥补这个示例。
显然,您输入的是从 1970 年第一时刻开始的整秒计数(UTC)。那个值可以直接使用,不需要相乘。
ZoneId
class 代表时区。 Instant
是 UTC 时间轴上的一个点,分辨率最高为纳秒。一个ZonedDateTime
就是把Instant
调整成ZoneId
。
ZoneId zoneId = ZoneId.of ( "Europe/Paris" );
long input = 1_468_015_200L; // Whole seconds since start of 1970.
Instant instant = Instant.ofEpochSecond ( input );
ZonedDateTime zdt = ZonedDateTime.ofInstant ( instant , zoneId );
转储到控制台。
System.out.println ( "input: " + input + " | instant: " + instant + " | zdt: " + zdt );
input: 1468015200 | instant: 2016-07-08T22:00:00Z | zdt: 2016-07-09T00:00+02:00[Europe/Paris]