The Calendar.getTime()
method returns the Date
object, which is just the number of milliseconds since epoch. Printing that instance invokes the Date#toString()
method, which uses the system default timezone to format the date. Giving the Calendar instance a timezone, doesn't associate the resultant Date
object with that timezone. In fact, Date
object has no concept of timezones.
What you want is to format the date yourself using DateFormat
, and set the timezone in that. Change your UTC
method to this:
public void showUTCTime(long millis, final String msg) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(0);
DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(msg + "(UTC):" + dateFormat.format(cal.getTime()));
}