如何在 Java 中将 UTC 时间转换为本地时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13315734/
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-10-31 12:15:52  来源:igfitidea点击:

How to convert UTC time into local time in Java?

javatime

提问by ChanChow

I have time coming from gpslocation service in 1352437114052 format. Can some one tell me how to convert this into local time either in Java or Matlab or Excel.

我有时间来自 1352437114052 格式的 gpslocation 服务。有人可以告诉我如何在 Java 或 Matlab 或 Excel 中将其转换为本地时间。

采纳答案by Steve Kuo

Create a new Datefrom your milliseconds since epoch. Then use a DateFormatto format it in your desired timezone.

Date从纪元以来的毫秒数中创建一个新的。然后使用 aDateFormat将其格式化为您所需的时区。

Date date = new Date(1352437114052L);
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
format.setTimeZone(TimeZone.getTimeZone("PST"));
System.out.println(format.format(date));

回答by Tomasz Nurkiewicz

This is an epoch timeand it represents Fri, 09 Nov 2012 04:58:34 GMT. This numeric value is an absolute point in time, irrespective to time zone.

这是一个纪元时间,它代表格林威治标准时间 2012 年 11 月 9 日星期五 04:58:34。该数值是一个绝对的时间点,与时区无关。

If you want to see that point in time in different time zone, use GregorianCalendar:

如果您想查看不同时区的那个时间点,请使用GregorianCalendar

Calendar c = new GregorianCalendar(TimeZone.getTimeZone("EST"));
c.setTimeInMillis(1352437114052L);
c.get(Calendar.HOUR_OF_DAY); //20:58 the day before

回答by Ole V.V.

The modern Java answer using the JVM's time zone setting (typically the same as your computer's time zone):

使用 JVM 的时区设置(通常与您计算机的时区相同)的现代 Java 答案:

        long time = 1_352_437_114_052L;
        ZonedDateTime dateTime = Instant.ofEpochMilli(time).atZone(ZoneId.systemDefault());
        System.out.println(dateTime);

Running on my computer I get

在我的电脑上运行我得到

2012-11-09T05:58:34.052+01:00[Europe/Copenhagen]

2012-11-09T05:58:34.052+01:00[欧洲/哥本哈根]

To specify a time zone:

要指定时区:

        ZonedDateTime dateTime = Instant.ofEpochMilli(time).atZone(ZoneId.of("Asia/Almaty"));

2012-11-09T10:58:34.052+06:00[Asia/Almaty]

2012-11-09T10:58:34.052+06:00[亚洲/阿拉木图]

Question: Will that work on Android too?

问题:这也适用于 Android 吗?

To answer tinker's comment here: Yes. I am using java.time, the modern Java date and time API, and it works nicely on older and newer Android devices. It just requires at least Java 6.

这里回答修补匠的评论:是的。我使用的java.time是现代 Java 日期和时间 API,它在新旧 Android 设备上都能很好地工作。它只需要至少 Java 6

  • In Java 8 and later and on newer Android devices (from API level 26, I'm told) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It's called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bpwith subpackages.
  • 在 Java 8 及更高版本以及更新的 Android 设备上(我被告知从 API 级别 26),现代 API 是内置的。
  • 在 Java 6 和 7 中获得 ThreeTen Backport,新类的 backport(ThreeTen for JSR 310;请参阅底部的链接)。
  • 在(较旧的)Android 上使用 ThreeTen Backport 的 Android 版本。它被称为 ThreeTenABP。并确保从org.threeten.bp子包中导入日期和时间类。

Links

链接

回答by DSlomer64

@Steve Kuo answered the question directly, almost. Here's a more general solution for machine's local time, including daylight saving time, where ais of type BasicFileAttributesas reported from Windows directory entry in public FileVisitResult visitFile(Path f, BasicFileAttributes a)during Files.walkFileTree:

@Steve Kuo 几乎直接回答了这个问题。这是机器本地时间的更通用解决方案,包括夏令时,其中a的类型BasicFileAttributespublic FileVisitResult visitFile(Path f, BasicFileAttributes a)期间的Windows 目录条目报告的类型相同Files.walkFileTree

  String modifyDate;
  Date date = new Date(a.lastModifiedTime().to(TimeUnit.MILLISECONDS));
  DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
  format.setTimeZone(TimeZone.getDefault());
  modifyDate = (format.format(date)).substring(0,10);

回答by tinker

long timeStamp = System.currentTimeMillis();
System.out.println(timeStamp+"");

Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getDefault());
calendar.setTimeInMillis(timeStamp);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a z");
String dateString = sdf.format(calendar.getTime());
System.out.println(dateString);


Output :

输出 :

timestamp : 1528860439258

dateformat from sdf : 2018-06-12 08:27:19 PM PDT