java - 将 UTC 日期转换为本地日期

标签 java date datetime simpledateformat

我正在将纪元时间(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 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 format utc 格式 格式正确。

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

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

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

关于java - 将 UTC 日期转换为本地日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31395441/

相关文章:

php - 从给定日期时间获取每月的第一个星期一不会保留时间组件

java - 线程“kafka-生产者-网络-线程”中未捕获的异常 |生产者1

java - 使用java限制文件大小创建

sql - 在 ms sql server 的 char 列中存储的日期或整数值上使用关系运算符是否有效?

javascript - 如何将 JS 日期对象传递给云函数以将其存储在 Firestore 上

c# - DateTime.TryParseExact 返回 false

c# - 如何在不同的系统文化中转换 DateTime?

java - Jenkins 会吞下 MojoFailureException 吗?

java - 可作为方法引用运行

javascript - 有效检测数组中缺失的日期并注入(inject) null(highcharts 和 jquery)