【问题标题】:Convert UTC Date to local Date将 UTC 日期转换为本地日期
【发布时间】:2015-07-13 23:58:53
【问题描述】:

我正在从纪元时间(UTC)转换为如下所示的格式。现在我尝试了不同的 SO 答案来将 UTCDateUTC 转换为本地时间。但我没有得到当地时间。

任何帮助将不胜感激。

String epochTime = "1436831775043";

Date UTCDate = new Date(Long.parseLong(epochTime));
Date localDate; // How to get this?

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("h:mm a");
String result = simpleDateFormat.format(UTCDate);

此外,转换必须在没有任何外部库的帮助下完成。

【问题讨论】:

标签: java date datetime simpledateformat


【解决方案1】:

Java 8

String epochTime = "1436831775043";

Instant utcInstant = new Date(Long.parseLong(epochTime)).toInstant();
ZonedDateTime there = ZonedDateTime.ofInstant(utcInstant, ZoneId.of("UTC"));

System.out.println(utcInstant);
LocalDateTime here = there.withZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime();
System.out.println(here);

哪些输出:

2015-07-13T23:56:15.043Z
2015-07-14T09:56:15.043

经过深思熟虑...

我认为你在追你的尾巴。 Date 只是一个容器,用于记录自纪元(1970 年 1 月 1 日,格林威治标准时间 00:00:00)以来的毫秒数。它在内部不携带时区 (AFAIK) 的表示。

例如...

String epochTime = "1436831775043";
Date UTCDate = new Date(Long.parseLong(epochTime));
// Prints the "representation" of the Date
System.out.println(UTCDate);

// Local date/time format...
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy h:mm:ss a");
try {
    System.out.println("local format: " + simpleDateFormat.format(UTCDate));
    System.out.println("local Date: " + simpleDateFormat.parse(simpleDateFormat.format(UTCDate)));
} catch (ParseException ex) {
    Logger.getLogger(JavaApplication203.class.getName()).log(Level.SEVERE, null, ex);
}

// UTC date/time format
try {
    simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    System.out.println("utc format: " + simpleDateFormat.format(UTCDate));
    System.out.println("utc date: " + simpleDateFormat.parse(simpleDateFormat.format(UTCDate)));
} catch (ParseException ex) {
    Logger.getLogger(JavaApplication203.class.getName()).log(Level.SEVERE, null, ex);
}

哪些输出...

Tue Jul 14 09:56:15 EST 2015
local format: 14/07/2015 9:56:15 AM
local Date: Tue Jul 14 09:56:15 EST 2015
utc format: 13/07/2015 11:56:15 PM
utc date: Tue Jul 14 09:56:15 EST 2015

如果您查看local Dateutc date,它们是相同的,即使local formatutc format 的格式正确。

因此,与其追着你的故事试图让Date“代表”你想要的值,不如使用 Java 8 的 Time API 或 JodaTime 来管理时区信息,或者简单地将 Date 格式化为时区你想要...

此外,如果我们做类似...

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy h:mm:ss a");
Date localDate = simpleDateFormat.parse(simpleDateFormat.format(UTCDate));
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date utcDate = simpleDateFormat.parse(simpleDateFormat.format(UTCDate));

System.out.println(localDate.getTime());
System.out.println(utcDate.getTime());

System.out.println(localDate.equals(utcDate));

它将打印...

1436831775000
1436831775000
true

【讨论】:

  • 感谢您提供有用的额外信息。但另一个答案更简单。 :)
  • 一切都归结为你想要达到的目标,关键是你只能影响Date对象的格式;)
  • 小提示:java.time 在ZoneIdZoneOffset 的子类上有一个UTC 时区常量。所以,不要使用ZoneId.of("UTC"),而是使用ZoneOffset.UTC
【解决方案2】:

您可以在格式化程序中设置您的时区:

simpleDateFormat.setTimeZone(TimeZone.getDefault());

【讨论】:

  • 糟糕,它确实有效!我的模拟器的时区不同。
猜你喜欢
  • 2020-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-07
  • 2018-09-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多