Java当前日期/时间显示原始时间前1小时 [英] Java Current Date/Time displays 1 hour ahead that original time

查看:508
本文介绍了Java当前日期/时间显示原始时间前1小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用日历实例打印当前的日期和时间,我得到的结果比实际时间提前1个小时。

When I try to print the current date and time using Calender instance, the result I get is 1 hour ahead from the actual time.

我正在使用与EST时区一起运行的远程机器。下面是我试过的,但没有任何作用。

I am working in remote machine which runs with EST time zone. Below is what I tried, but nothing works.

Calendar cal = Calendar.getInstance(TimeZone.getDefault());
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("EST"));
Calendar cal = Calendar.getInstance(Locale.ENGLISH);

System.out.println("Current Date & Time: " +calendar.getTime());

o/p:
Time: Sat Dec 28 11:55:10 UTC 2013

But expected o/p: 
Time: Sat Dec 28 10:55:10 UTC 2013

所有3种类型都提供相同的结果。我不明白我错过了什么,以获得确切的日期&时间。这个问题与日光节约时间有关吗?

All the 3 types give same result. I couldn't understand what I miss out to get the exact date & time. Is this problem related to daylight time saving ?

有人可以帮我解决这个问题。感谢Advance。

Could someone help me to overcome this problem. Thanks in Advance.

推荐答案

再次这是java.util.Date的老陷阱:它的toString() - 方法,你间接使用时打印 calendar.getTime()使用您的默认时区,而不是您的日历实例的时区(您设置为EST)。

It is again this old pitfall with java.util.Date: Its toString()-method which you indirectly use when printing calendar.getTime() uses your default time zone, not the time zone of your calendar instance (which you set to 'EST').

解决方案:

Date currentTime = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
System.out.println("Current Date & Time: " + sdf.format(currentTime));

说明:

a)在第一行,不需要日历实例,因为您只对当前全局时间感兴趣(与时区无关的物理时间相同)。 Calendar.getInstance()也是更耗资源。最后,两个表达式 new Date() Calendar.getInstance(...)。getTime()没有时间当它是关于内部状态时的区域引用。只有juDate的 toString() -method使用默认时区。

a) In first line no calendar instance is necessary because you are just interested in current global time (the same physical time independent from timezone). Calendar.getInstance() is also more consuming resources. Finally, both expressions new Date() and Calendar.getInstance(...).getTime() have no time zone reference when it is about the internal state. Only the toString()-method of j.u.Date uses default time zone.

b)您需要定义一个输出格式在第2行中给出。由您自行更改。只需研究 java.text.SimpleDateFormat 的模式文档。

b) You need to define an output format which is given in line 2. It is up to you to change it. Just study the pattern documentation of java.text.SimpleDateFormat.

c)您还需要定义时区以您的输出格式帮助格式对象将全局Date-instance转换为时区感知表示。顺便说一句,我选择了美国/纽约标识符,而不是EST,因为后一种形式有时候会变得模糊。您应该选择第一个表单(IANA或Olson时区标识符)或格式GMT +/- HH:mm。

c) You also need to define the time zone in your output format to help the format object to translate the global Date-instance into a timezone-aware representation. By the way, I had choosen the identifier 'America/New_York', not 'EST' because latter form can be sometimes ambigous. You should either choose the first form (IANA- or Olson time zone identifier) or the form 'GMT+/-HH:mm'.

d)输出本身完成与 sdf.format(currentTime)不仅仅是 currentTime (没有隐式调用 toString( )

d) The output itself is done with sdf.format(currentTime), not just currentTime (no implicit call of toString()).

e)回答你的问题这个问题是否与夏令时相关?:不,时区EST(美国/纽约)从未在十二月份的DST。

e) To answer your question 'Is this problem related to daylight time saving ?': No, the time in time zone EST (America/New_York) is never in DST in december.

结论:

如果可以,你应该尽量避免juDate和juCalendar,因为有太多的陷阱。现在JodaTime是一个更好的选择,虽然不是没有问题。

If you can, you should try to avoid j.u.Date and j.u.Calendar because there are too many pitfalls. At the moment JodaTime is a better alternative, although not without issues.

这篇关于Java当前日期/时间显示原始时间前1小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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