How to convert date in one timezone to date in another timezone?
807603Oct 24 2007 — edited Nov 23 2007Hi,
I am building a web site which can be accessed by people across world in different timezones. I have a requirement to collect the data from the users in their respective timezones and convert it to the timezone in which the server is hosted before saving it. E.g. a user in "Kwajalein" timezone (GMT - 12) enters date as "10/23/2007 21:00", the time for the server which is hosted in indian timezone (GMT + 5.30) would be "10/24/2007 14:30". If I use the following program to convert the dates in these timezones it gives me wrong output.
TimeZone tz = TimeZone.getTimeZone("Kwajalein");
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm");
df.setTimeZone(tz);
Date d = df.parse("10/23/2007 21:00");
TimeZone tz1 = TimeZone.getTimeZone("Asia/Calcutta");
df.setTimeZone(tz1);
System.out.println("date in IST-->" + df.format(d));
The output of this program is
date in IST-->10/23/2007 14:30
The formatter has changed the time component however it hasn't changed the date. It still appears as 23.
Am I doing anything wrong here?
Please pass your comments.
Thanks,
Shashi