在 Java 中从毫秒转换为 UTC 时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31292032/
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
Converting from Milliseconds to UTC Time in Java
提问by kpb6756
I'm trying to convert a millisecond time (milliseconds since Jan 1 1970) to a time in UTC in Java. I've seen a lot of other questions that utilize SimpleDateFormat to change the timezone, but I'm not sure how to get the time into a SimpleDateFormat, so far I've only figured out how to get it into a string or a date.
我正在尝试将毫秒时间(自 1970 年 1 月 1 日以来的毫秒数)转换为 Java 中的 UTC 时间。我已经看到很多其他问题利用 SimpleDateFormat 来更改时区,但我不确定如何将时间放入 SimpleDateFormat,到目前为止我只知道如何将其放入字符串或日期.
So for instance if my initial time value is 1427723278405, I can get that to Mon Mar 30 09:48:45 EDT using either String date = new SimpleDateFormat("MMM dd hh:mm:ss z yyyy", Locale.ENGLISH).format(new Date (epoch));
or Date d = new Date(epoch);
but whenever I try to change it to a SimpleDateFormat to do something like thisI encounter issues because I'm not sure of a way to convert the Date or String to a DateFormat and change the timezone.
因此,举例来说,如果我的初始时间值是1427723278405,我可以得到那个周一3月30日美国东部时间九点48分45秒或者使用String date = new SimpleDateFormat("MMM dd hh:mm:ss z yyyy", Locale.ENGLISH).format(new Date (epoch));
或Date d = new Date(epoch);
,但每当我试图将其更改为一个SimpleDateFormat的做一些像这样我遇到的问题,因为我不确定将日期或字符串转换为 DateFormat 并更改时区的方法。
If anyone has a way to do this I would greatly appreciate the help, thanks!
如果有人有办法做到这一点,我将不胜感激,谢谢!
采纳答案by Pavan Kumar K
Try below..
下面试试..
package com.example;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class TestClient {
/**
* @param args
*/
public static void main(String[] args) {
long time = 1427723278405L;
SimpleDateFormat sdf = new SimpleDateFormat();
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(sdf.format(new Date(time)));
}
}
回答by assylias
java.time option
java.time 选项
You can use the new java.time packagebuilt into Java 8 and later.
您可以使用Java 8 及更高版本中内置的新java.time 包。
You can create a ZonedDateTime
corresponding to that instant in time in UTC timezone:
您可以ZonedDateTime
在 UTC 时区中创建与该时刻对应的时间:
ZonedDateTime utc = Instant.ofEpochMilli(1427723278405L).atZone(ZoneOffset.UTC);
System.out.println(utc);
You can also use a DateTimeFormatter if you need a different format, for example:
如果您需要不同的格式,也可以使用 DateTimeFormatter,例如:
System.out.println( DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss").format(utc));
回答by Tariq
You May check this..
你可以检查这个..
Calendar calendar = new GregorianCalendar();
calendar.setTimeInMillis(1427723278405L);
DateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");
formatter.setCalendar(calendar);
System.out.println(formatter.format(calendar.getTime()));
formatter.setTimeZone(TimeZone.getTimeZone("America/New_York"));
System.out.println(formatter.format(calendar.getTime()));