Can anybody tell about three things:
- How to retrieve TimeZone from java.util.Date instance?
- 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) - How to convert EST time to EDT or vice versa?
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 theDate
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.Ideally, use Joda Time instead of
java.util.Date
/Calendar
/TimeZone
, but withinTimeZone
you can useTimeZone.getOffset(long)
to find the offset from UTC, orTimeZone.inDaylightTime(Date)
to just give you a yes/no answer.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.
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".
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.
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:
To (crudely) determine the difference between two TimeZones:
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)