Dates and Java suck
Or there must be something I don’t understand. Update: Dates and Java don’t suck.
I need to do some date arithmetic (add an hour to the current time) and submit that as a Date Object to an API method (that as far as I can tell totally neglects the Date Object’s time zone component—and that I have no control over) that sends that data to a server on the east coast.
Here’s my test program:
import java.util.*;
public class MyTest
{
public static void main(String[] args)
{
TimeZone tz = TimeZone.getTimeZone("America/New_York");
Calendar current_date_cal = Calendar.getInstance(tz);
Date start = current_date_cal.getTime();
System.out.println(current_date_cal.get(Calendar.HOUR_OF_DAY));
System.out.println(start.toString());
}
}
Running this at 5:00PM PDT (GMT -7) yields the following output:
jwatt@jwatt:~$ java MyTest 20 Thu May 31 16:00:09 GMT-08:00 2007
The Calendar object returns the EST (GMT -5) hour when I’d really rather it be EDT (GMT -4), and the Calendar returns a date object in a different timezone than the Timezone I specified—my current time, sort of, PST, instead of PDT. Of course when a time of 16:00 (5PM pacific time) gets to NY, they say “Silly Wabbit, 5PM already happened!”


Consider:
import java.text.DateFormat; import java.util.Date; import java.util.TimeZone; public class MyTest { public static void main(String[] args) { TimeZone tz = TimeZone.getTimeZone("America/New_York"); Date start = new Date(); DateFormat df = DateFormat.getDateTimeInstance(); df.setTimeZone(tz); System.out.println(df.format(start)); } }Running this at 9:02PM CDT (GMT -5) yields:
May 31, 2007 10:02:30 PM