JAVA TimeZone issue EDT Vs EST

2019-02-16 16:42发布

I a newbie to java and hence haven't been able figure this out since quite some time.

I am using Windows XP and the machine is set to TimeZone: Eastern Time (US & Canada).

I have a Java application, which takes the current system time and timezone info and writes a string like: 20101012 15:56:00 EST, to a file.

The last piece of Date above, i.e.: the timezone, changes from EST to EDT as i change my system date.

Being precise: From November(eg: Nov2009) to March (Mar 2010), it is EST, otherwise EDT.

EST is what I want ALWAYS and not EDT.

Is there any particular class / function, by which I can always read it as EST?

Awaiting for response.


Thanks for your replies. Well, I forgot to mention a few things.

  1. I want my machine to be set to: Eastern Time (US & Canada) in the windows time zone settings.

  2. In simple terms, What i want to do is: get my machine time, and write it to a text file

  3. I am aware of the daylight saving which happens from March to Nov.

But the problem is, when I write my machine time to the file, it is written as 2010 01 12 15:56:00 EST if daylight saving (DST) is not present and as 20101012 15:56:00 EDT, if DST is present. My concern is, whether it is DST or not, I want to write EST always.

6条回答
SAY GOODBYE
2楼-- · 2019-02-16 17:12

Your question is still not clear.

I didn't undestand if you simply want to force 'EST' chars, even if your machine is set to automatically chage DST, or what you want is to get the actual time on EST.

So you have two options:

  1. Your machine time is set to 2:15pm and DST in on effect, and you want to write 2:15pm EST (which is not the correct actual time) you should use SimpleDateFormat. This way, you will be lying about the time. But, anyway, you know what best fits for you.

  2. Your machine time is set to 2:15pm and DST in on effect, and you want to write 1:15pm EST, you should use: TimeZone.setDefault(TimeZone.getTimeZone("EST"))

查看更多
家丑人穷心不美
3楼-- · 2019-02-16 17:23

The solution depends on why you're writing this time out and what you want to do with it when you read it back in (what it's going to be used to do, not "what you want"). Always writing it out as EST is a misrepresentation of fact. When EDT is in effect it is not, in fact, EST. If you're trying to track some "absolute" point in time I recommend working in GMT instead of any timezone.

查看更多
爷、活的狠高调
4楼-- · 2019-02-16 17:26

the machine is set to TimeZone: Eastern Time (US & Canada).

That's not a real time zone (neither are EDT and EST). A time zone (as understood by Java) is something like "America/Phoenix", which is an ID for an administrative timezone that has both a base offset and (optionally) switches to daylight savings time at specific dates. Both of these can change, and it's necessary to take such changes into account when interpreting historical dates.

Thus, if you don't want to have DST switches, choose a timezone that does not observe DST. It is possible that there is no such timezone and trying to act like there is would introduce impossible dates.

查看更多
贪生不怕死
5楼-- · 2019-02-16 17:31

I don't think you should do what you are suggesting.

You are saying that regardless of what your system timezone is currently (Eastern Time is NOT your time zone, but UTC+5 or +4 is), you want it to display EST. This is just plainly wrong. Suppose it's in the summer, and your computer thinks it's 2010/6/15 15:00 locally. You print the current time and you get:

The time I print this out at: 2010 06 15 15:00:00 EDT

For whatever reason, you think that the EDT is unpleasing, and so you change it to EST:

I print this out at: 2010 06 15 15:00:00 EST

however, if you then send that snippet to someone else within the next hour, they'll be forced to think you traveled from the future! Since 15:00:00 EST is 16:00:00 EDT.

查看更多
戒情不戒烟
6楼-- · 2019-02-16 17:31

It should be noted that during the Summer months of the Eastern timezone, pretty much every major center uses Daylight Savings Time, so you are correctly being shown the time that people in that timezone care about.

If you want it to be a specific timezone, rather than the default/system timezone, then you can force the TimeZone to EST via something like this:

TimeZone est = TimeZone.getTimeZone("EST");

Although as Michael mentioned above, the documentation recommends using a local full name such as "America/New York" rather than the general timezone abbreviation.

If you want to claim that it really is EST even though you know it to be EDT, then I suppose you could do the following: use a SimpleDateFormat instance with a custom pattern that doesn't include the timezone information, then tack "EST" onto the end of the String you write out.

查看更多
一纸荒年 Trace。
7楼-- · 2019-02-16 17:36

I would create a custom zone:

TimeZone alwaysEst = TimeZone.getTimeZone("EST+5");

That will report as EST and will always be 5 hours ahead of UTC. In particular, do not choose an existing timezone or you will eventually get burned when a zone update changes the definition.

Do be aware that by forcing EST the dates you log will only match the time displayed by the system for 5 months out of the year. The other 7 months you'll be an hour off. It may simplify parsing the file, but it will confuse users.

查看更多
登录 后发表回答