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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31395441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:33:06  来源:igfitidea点击:

Convert UTC Date to local Date

javadatedatetimesimpledateformat

提问by Kevin Richards

I am converting from epoch time (which is in UTC) to a format as shown below. Now I tried different SO answers to convert UTCDatefrom UTCto local time. But I am not getting the local time.

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

Any help would be appreciated.

任何帮助,将不胜感激。

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);

Also, the conversion has to be done without the help of any external library.

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

采纳答案by Nitesh Patel

You can set your time zone in the formatter:

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

simpleDateFormat.setTimeZone(TimeZone.getDefault());

回答by MadProgrammer

Java 8

爪哇 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);

Which outputs:

哪些输出:

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

After thoughts...

思后...

I think you're chasing your tail. Dateis just a container for the number of milliseconds since the epoch (January 1, 1970, 00:00:00 GMT). It doesn't internally carry a representation of a time zone (AFAIK).

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

For example...

例如...

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);
}

Which outputs...

哪个输出...

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

If you have a look at local Dateand utc datethey are the same thing, even though the local formatand utc formatare formatted correctly.

如果你看看local Dateutc date他们是一样的东西,即使local formatutc format格式正确无误。

So, instead of chasing your tale trying to get Dateto "represent" a value you want, either use Java 8's Time API or JodaTime to manage the Time Zone information or simply format the Dateinto the Time Zone you want...

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

Further, if we do something like...

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

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));

It will print...

它会打印...

1436831775000
1436831775000
true