Tue, 21 May 2013 07:58:28 -0700 (PDT), /
laredo...@zipmail.com/:
What do you expect when you say "convert"?
As others have pointed out the java.util.Date type is a simple
wrapper for a timestamp value which is always in UTC. If you
initialize a java.util.Calendar instance and set its timeZone -
you've already converted it in a way. When you query the Calendar
fields HOUR_OF_DAY, MINUTE, etc. you've got your conversion. You
may use these values to initialize your custom type of time object,
also.
If you want to _format_ a time/timestamp in a text representation
using specific time-zone, instead, you may use a java.util.DateFormat:
final String MY_TIMEZONE = "JST";
long timeInMs = 1368921600000L;
DateFormat dateFormat = DateFormat.getDateTimeInstance();
System.out.println(dateFormat.format(new Date(timeInMs)));
dateFormat.setTimeZone(TimeZone.getTimeZone(MY_TIMEZONE));
System.out.println(dateFormat.format(new Date(timeInMs)));
Note, your example code does really nothing to the Calendar.time
value, even if you change the Calendar's timeZone in between:
long timeInMs = 1368921600000L;
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone(MY_TIMEZONE));
cal.setTimeInMillis(timeInMs);
java.util.Date dateObj = cal.getTime();
cal.setTimeZone(TimeZone.getTimeZone(MY_TIMEZONE2));
java.util.Date dateObj2 = cal.getTime();
System.out.println(dateObj.getTime() == dateObj2.getTime());
--
Stanimir