I’ve got an Access database with a column of dates and times and a timezone like so:

So, first I’m creating a TimeZone object and a SimpleDateFormat object like so
TimeZone timeZone = TimeZone.getTimeZone(parseNull(rs.getString("TimeZone")));
DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormatter.setTimeZone(timeZone);
Then, I’m reading in the date like so:
String startDateString = rs.getDate("Start_Date") + " " + rs.getString("Start_Time").substring(11);
startDate = dateFormatter.parse(startDateString);
However, if I then print out the Date like so:
System.out.println("Start Actual: " + rs.getDate("Start_Date") + " " + rs.getString("Start_Time").substring(11));
System.out.println("Start Formatted: " + startDate);
I get the following output:
Start Actual: 2011-11-10 18:00:00
Start Formatted: Thu Nov 10 02:00:00 EST 2011
Okay….why? I find this very confusing. Why won’t my Date object correctly display in the Australia/Sydney time zone and, better yet, how can I get this to correctly work so that I can set the date in my application correctly?
Thanks in advance for any help!
A
java.util.Datedoesn’t have a time zone. It’s a universal instant in time. When you display a date in human readable format, then you choose in which timezone you want to display this date, and you do it by setting the time zone on the SimpleDateFormat used to transform the date into a string.If you just do
System.out.println("Start Formatted: " + startDate), thetoString()method of the date is used, and thistoString()method uses the default timezone.