简体   繁体   中英

Converting EST to EDT or vice versa in Java

Can anybody tell about three things:

  1. How to retrieve TimeZone from java.util.Date instance?
  2. How to know whether Daylight savings is applicable?
    I suppose I can know it by doing timeZone.getDSTSavings, but problem I am facing is that even if I make my system's date as Feb 1 2012, still I am getting the value as positive (I guess 3600000)
  3. How to convert EST time to EDT or vice versa?

How to retrieve TimeZone from java.util.Date instance?

There's no such thing. A Date just represents a number of milliseconds since the Unix epoch, which was midnight on January 1st 1970 UTC. It's not associated with a particular calendar system or time zone. To put it another way, if a friend and I are on the phone together (with a zero latency ;) and I click my fingers, we would both agree on the Date at which that click too place - even if I'm using the Gregorian calendar and he's using the Julian calendar, and even if I'm in London and he's in New York. It's the same instant in time .

How to know whether Daylight savings is applicable?, I suppose I can know it by doing timeZone.getDSTSavings, but problem I am facing is that even if I make my system's date as Feb 1 2012, still I am getting the value as positive (I guess 3600000)

Ideally, use Joda Time instead of java.util.Date / Calendar / TimeZone , but within TimeZone you can use TimeZone.getOffset(long) to find the offset from UTC, or TimeZone.inDaylightTime(Date) to just give you a yes/no answer.

How to convert EST time to EDT or vice versa?

Usually that's an invalid question - because at any one instance in time, either EST or EDT applies. You normally convert from one time zone to another, and "EDT" and "EST" aren't different time zones - they're different offsets within the same time zone. The fact that you're asking for this suggests that you may be modelling your data incorrectly to start with (which is unfortunately easy to do with date/time values). Please give us more information and we may be able to help you more.

  1. How to retrieve TimeZone from java.util.Date instance?

You can view the Time Zone of an instantiated Date object by viewing it in toString() format. Such as "Wed May 01 12:00:00 EDT 2013".

How to know whether Daylight savings is applicable?, I suppose I can know it by doing timeZone.getDSTSavings, but problem I am facing is that even if I make my system's date as Feb 1 2012, still I am getting the value as positive (I guess 3600000) 3. How to convert EST time to EDT or vice versa? This is critical question I have

The Date object will account for the EDT/EST automatically. For instance, if you have an instantiated Date object for November 28, then the toString() will include EST rather than EDT.

The problem I originally ran into was that I was trying to use EST for a calendar date where Daylight Savings Time was active, and the Date object was automatically correcting my mistake by adding an hour to my time and displaying EDT.

I ran into a similar problem. The following solution is in Matlab but calling Java, and my changes are just comments because I'm working with Matlab datenum values. You wouldn't really be able to convert timezone aware values, since they are the same timezone, but this may help with raw values that are not timezone aware.

%c1 GregorianCalendar of now
%c2 GregorianCalendar of date in question
tz1 = c1.getTimeZone; %Note t2 would be the same

in1 = tz1.inDaylightTime(c1.getTime);
in2 = tz1.inDaylightTime(c2.getTime);

if (in1 == in2)
    %do nothing
elseif in1
    %c1 EDT, c2 is EST
    %Subtract 1 hour to c2
else
    %c1 EST, c2 EDT
    %Add 1 hour to c2
end

Date stores the time in milliseconds since the 1970 epoch, so doesn't contain timezone information. Your JVM has a locale set which defines how this date is then displayed.

If you want to find out what TimeZone your JVM has set, call 'TimeZone.getDefault()'

Once you have a TimeZone object, you can call a number of functions to determine if for that timezone, daylight savings applies to a give date:

  • inDaylightTime(Date)

To (crudely) determine the difference between two TimeZones:

long edtOffset = TimeZone.getTimeZone("EDT").getOffset(date.getTime());
long estOffset = TimeZone.getTimeZone("EST").getOffset(date.getTime());
long diff = estOffset - edtOffset;

Although I'm not sure how valid this is as EST and EDT are mutually exclusive, and whether the above code will even give you the result you're interested in (I've not tested it)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM