简体   繁体   English

在Java中从毫秒转换为UTC时间

[英]Converting from Milliseconds to UTC Time in Java

I'm trying to convert a millisecond time (milliseconds since Jan 1 1970) to a time in UTC in Java. 我正在尝试将毫秒时间(自1970年1月1日以来的毫秒)转换为Java中的UTC时间。 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. 我已经看到很多其他问题利用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)); 因此,例如,如果我的初始时间值是1427723278405,我可以使用String date = new SimpleDateFormat("MMM dd hh:mm:ss z yyyy", Locale.ENGLISH).format(new Date (epoch));将其发送到美国东部时间3月30日09:48:45 String date = new SimpleDateFormat("MMM dd hh:mm:ss z yyyy", Locale.ENGLISH).format(new Date (epoch)); or Date d = new Date(epoch); 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. 但每当我尝试将其更改为SimpleDateFormat来执行此类操作时,我遇到问题,因为我不确定是否可以将Date或String转换为DateFormat并更改时区。

If anyone has a way to do this I would greatly appreciate the help, thanks! 如果有人有办法做到这一点,我将非常感谢帮助,谢谢!

java.time option java.time选项

You can use the new java.time package built into Java 8 and later. 您可以使用Java 8及更高版本中内置的新java.time包

You can create a ZonedDateTime corresponding to that instant in time in UTC timezone: 您可以在UTC时区中创建与该时刻对应的ZonedDateTime

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

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

    }

}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM