Hello.
I would like to use java.text.SimpleDateFormat in order to parse a String containing a date, time, and timezone without needing to specify a different timezone depending on whether I'm using daylight savings time or not. That is, consider the following program:
import java.text.*;
public class TZ
{
public static void main(String[] args) throws Exception
{
DateFormat format = new SimpleDateFormat("yyyyMMdd HHmmss z");
System.out.println(format.parse(args[0]));
}
}
Now take a look at the following output (the program was run on a computer in the US/Eastern timezone, though that probably isn't relevant):
java TZ "20070310 120000 EST"
Sat Mar 10 12:00:00 EST 2007
The above example looks fine. The time in question was during standard time, and we print out a Date that seems to be the same moment in time.
java TZ "20070312 120000 EST"
Mon Mar 12 1
3:00:00 E
DT 2007
In this example, we've moved forward two days into the daylight savings time period. It appears that SimpleDateFormat, upon seeing the "EST" in the string to parse, is assuming that I really mean standard time here, despite the fact that the day should be on daylight savings time. This is a little weird, but could be reasonable, since the String is using "EST" explicitly.
java TZ "20070312 120000 EDT"
Mon Mar 12 12:00:00 EDT 2007
In the above example, we switched the String to use EDT explicitly, and this did what we wanted. However, it's unfortunate that we had to modify the String in order to get the Date we wanted. So let's try again below.
java TZ "20070312 120000 EST5EDT"
Mon Mar 12 1
3:00:00 EDT 2007
This last example is really where I was disappointed. I was hoping that by using "EST5EDT" here, the SimpleDateFormat would realize that I wanted it to parse the String using whatever DST behavior was correct at that particular moment. Unfortunately, it seems to treat "EST5EDT" identically to "EST" here.
The only way I can see to fix this is to avoid using a TimeZone format parameter in the SimpleDateFormat entirely, and instead parse the TimeZone independently. That is, consider this program:
import java.text.*;
import java.util.TimeZone;
public class TZFixed
{
public static void main(String[] args) throws Exception
{
DateFormat format = new SimpleDateFormat("yyyyMMdd HHmmss");
format.setTimeZone(TimeZone.getTimeZone(args[1]));
System.out.println(format.parse(args[0]));
}
}
Now look at these runs:
java TZFixed "20070310 120000" EST5EDT
Sat Mar 10 12:00:00 EST 2007
java TZFixed "20070312 120000" EST5EDT
Mon Mar 12 12:00:00 EDT 2007
Perfect! The code parses the time using the correct DST behavior both before and after the DST switch. However, the code is uglier than simply using SimpleDateFormat. And this difference seems somewhat inconsistent. Why would SimpleDateFormat behave differently if the TimeZone is set programmatically versus being set by the "z" format character?
Can anyone shed any insight on this? Any way to get this to work just using the SimpleDateFormat String?
Thanks,
-Neil