Java SimpleDateFormat:一小时错误 [英] Java SimpleDateFormat: an hour wrong

查看:118
本文介绍了Java SimpleDateFormat:一小时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不需要一个完整的故事来澄清我的问题,所以我只是显示代码(这只是一个例子)。我的结果有什么区别?

I don't need a whole story to clarify my question, so I'll just show the code (which is a mere example). How come there is a difference in my result?

代码

long millis = 2305293L;
System.out.println(
    millis + "ms = " + 
    (millis / 1000) + "s = " + 
    (millis / 1000 / 60) + "m");
System.out.println(
    new SimpleDateFormat("HH:mm:ss").
    format(
        new Date(millis)
        )
    );

输出

2305293ms = 2305s = 38m
01:38:25


推荐答案

如果您在伦敦或巴黎,那么1970年1月1日的时区为GMT + 1。

If you are in London, or Paris, the timezone was GMT+1 on 1 Jan 1970.

原因@ARC在评论中解释说,英国在1968年2月18日至1971年10月31日期间使用GMT + 1或UTC + 1。

For reasons @ARC explains in the comments, the UK used GMT+1 or UTC+1 from 18 Feb 1968 to 31 Oct 1971


是可能为我转换一个长而没有任何时区干扰?

is it possible for me to convert a long without any timezones interfering?

将TimeZone设置为GMT。

Set the TimeZone to be GMT.

long millis = 2305293L;
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(sdf.format(new Date(millis)));

打印

00:38:25.293

这篇关于Java SimpleDateFormat:一小时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆