Java.util.Date: try to understand UTC and ET more

I live in North Carolina, by the way, which is located on the east side. So I compile and run this code, and it prints the same thing. The documentation says that java.util.date is trying to reflect UTC time.

Date utcTime = new Date(); Date estTime = new Date(utcTime.getTime() + TimeZone.getTimeZone("ET").getRawOffset()); DateFormat format = new SimpleDateFormat("dd/MM/yy h:mm a"); System.out.println("UTC: " + format.format(utcTime)); System.out.println("ET: " + format.format(estTime)); 

And this is what I get

 UTC: 11/05/11 11:14 AM ET: 11/05/11 11:14 AM 

But if I go to this website , which tries to reflect all different times, UTC and ET are different. What i did wrong here

+4
source share
5 answers

This is because getRawOffset() returns 0 - it does this for me and for "ET", but in fact TimeZone.getTimeZone("ET") basically returns GMT. I suspect that is not what you had in mind.

Olson’s best time zone name for North Carolina is America / New York, I think.

Note that you should not just add the raw timezone offset to UTC - you should instead set the formatting timezone. The Date value really does not know about the time zone ... it is always only milliseconds since January 1, 1970 UTC.

So you can use:

import java.text .; import java.util .;

 Date date = new Date(); DateFormat format = new SimpleDateFormat("dd/MM/yy h:mm a zzz"); format.setTimeZone(TimeZone.getTimeZone("America/New_York")); System.out.println("Eastern: " + format.format(date)); format.setTimeZone(TimeZone.getTimeZone("Etc/UTC")); System.out.println("UTC: " + format.format(date)); 

Output:

 Eastern: 11/05/11 11:30 AM EDT UTC: 11/05/11 3:30 PM UTC 

I also recommend that you learn Joda Time instead of the built-in libraries - this is a much better API.

+7
source

according to this post you can write TimeZone.getTimeZone("ETS") instead of TimeZone.getTimeZone("ET")

+1
source

TimeZone.getTimeZone ("ET"). getRawOffset () returns 0, so

+1
source

The time zone you are looking for is "EST" or "EDT" (for daytime), not "ET" . See http://mindprod.com/jgloss/timezone.html .

+1
source

The correct abbreviation for Eastern Standard Time is β€œEST”, not β€œET”. The getRawOffset() method getRawOffset() return 0 if an unknown time zone is passed to it.

 TimeZone.getTimeZone("EST").getRawOffset() 

In addition, when outputting the utcTime variable utcTime you do not utcTime UTC time. You indicate EST time because you live in this time zone. From what I understand, the Date class internally saves time in UTC ... but when you format it to display it as a human-readable string, it takes into account the current locale / time zone.

+1
source

All Articles