在Java中从毫秒转换为UTC时间 [英] Converting from Milliseconds to UTC Time in Java
问题描述
我正在尝试将毫秒时间(自1970年1月1日以来的毫秒数)转换为Java中的UTC时间。我已经看到很多其他问题,这些问题利用SimpleDateFormat来更改时区,但是我不确定如何将时间转换为SimpleDateFormat,到目前为止,我仅想出了如何将其转换为字符串或日期。
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.
因此,例如,如果我的初始时间值为1427723278405,则可以使用 String将其设置为美国东部时间3月30日09:48:45 date = new SimpleDateFormat( MMM dd hh:mm:ss z yyyy,Locale.ENGLISH).format(new Date(epoch));
或 Date d = new Date (epoch);
,但是每当我尝试将其更改为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 this I encounter issues because I'm not sure of a way to convert the Date or String to a DateFormat and change the timezone.
如果有人可以做到这一点,我将非常感谢您的帮助,谢谢!
If anyone has a way to do this I would greatly appreciate the help, thanks!
推荐答案
尝试下面的方法。
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)));
}
}
这篇关于在Java中从毫秒转换为UTC时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!