如何从Unix纪元时间毫秒获得GMT日期?

时间:2021-12-31 16:00:38

I trying convert unix milliseconds to gmt date, I need only hours and minutes, but results are incorrect according to online converters.

我尝试将unix毫秒转换为gmt日期,我只需要几小时和几分钟,但根据在线转换器,结果不正确。

What I need

我需要的

如何从Unix纪元时间毫秒获得GMT日期?

Here is my code:

这是我的代码:

 public static void main(String[] args) {
    long time = 1438050023;
   // TimeZone timeZone = TimeZone.getTimeZone("UTC");
    Calendar calendar = Calendar.getInstance();

    calendar.setTimeInMillis(time / 1000);

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm:ss dd MM yyyy");
    simpleDateFormat.setTimeZone(calendar.getTimeZone());

    System.out.println(simpleDateFormat.format(calendar.getTime()));
}

Result:

结果:

03:23:58 01 01 1970

2 个解决方案

#1


4  

Change calendar.setTimeInMillis(time / 1000) to calendar.setTimeInMillis(time * 1000)

将calendar.setTimeInMillis(time / 1000)更改为calendar.setTimeInMillis(time * 1000)

The number of milliseconds is 1000 times the number of seconds; not 1/1000 the number.

毫秒数是秒数的1000倍;数字不是1/1000。

#2


1  

 public static String ConvertMillistoDatetime(long millis) {
    long second = (millis / 1000) % 60;
    long minute = (millis / (1000 * 60)) % 60;
    long hour = (millis / (1000 * 60 * 60)) % 24;

    return String.format("%02d:%02d:%02d", hour, minute, second);
}

Try this you can keep seconds optional here

试试这个你可以在这里保持秒可选

#1


4  

Change calendar.setTimeInMillis(time / 1000) to calendar.setTimeInMillis(time * 1000)

将calendar.setTimeInMillis(time / 1000)更改为calendar.setTimeInMillis(time * 1000)

The number of milliseconds is 1000 times the number of seconds; not 1/1000 the number.

毫秒数是秒数的1000倍;数字不是1/1000。

#2


1  

 public static String ConvertMillistoDatetime(long millis) {
    long second = (millis / 1000) % 60;
    long minute = (millis / (1000 * 60)) % 60;
    long hour = (millis / (1000 * 60 * 60)) % 24;

    return String.format("%02d:%02d:%02d", hour, minute, second);
}

Try this you can keep seconds optional here

试试这个你可以在这里保持秒可选