I have a piece of code that generates timestamps based on US TimeZones such as Pacific, Central and Eastern. I get correct results when I run this code locally (EST) but get results an hour off when I run the same code on a server running in US central time. See the results below and the code that I run.
Results from running the code on Dev Server:
ts: 2007-11-05 10:39:03.19 (Default time)
tsEST: 2007-11-05 10:39:03.019 (EST based on TimeZone
String America/New_York, should be 11:39)
tsCST: 2007-11-05 09:39:03.019 (Central time based on TimeZone String US/Central, should be 10:39)
tsPST: 2007-11-05 07:39:03.019 (Pacific time based on TimeZone String America/Los_Angeles, should be 8:39)
Results from running the same code on Local machine:
tsEST: 2007-11-05 11:39:01.272 (Eastern Time based on TimeZone String America/New_York)
tsCST: 2007-11-05 10:39:01.272 (Central time based on TimeZone String US/Central)
tsPST: 2007-11-05 08:39:01.272 (Pacific time based on TimeZone String America/Los_Angeles)
Below is the code that I ran.
Timestamp ts = new Timestamp(Calendar.getInstance().getTime().getTime());
DateFormat df1 = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss.SS" );
GregorianCalendar cal1 = new GregorianCalendar();
Timestamp tsNow = new Timestamp(cal1.getTimeInMillis());
TimeZone tsEST = TimeZone.getTimeZone("America/New_York");
String inPattern = "yyyy-MM-dd HH:mm:ss.SS";
DateFormat df = new SimpleDateFormat(inPattern);
df.setTimeZone(tsEST);
Date date = df.parse(tsNow.toString());
df.setTimeZone(tsEST);
ts = new Timestamp( df1.parse( df.format(date) ).getTime() );
System.out.println("tsEST: " + ts.toString());
DateFormat df2 = new SimpleDateFormat(inPattern);
TimeZone tsCST = TimeZone.getTimeZone("US/Central");
df2.setTimeZone(tsCST);
ts = new Timestamp( df1.parse( df2.format(date) ).getTime() );
System.out.println("tsCST: " + ts.toString());
TimeZone tsPST = TimeZone.getTimeZone("America/Los_Angeles");
df.setTimeZone(tsPST);
ts = new Timestamp( df1.parse( df.format(date) ).getTime() );
System.out.println("tsPST: " + ts.toString());
Can somone help me identify why the server is not producing correct time.