The following works (shows UTC time)
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
System.out.println(new Date());
but this doesn’t (shows local time)
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
System.out.println(cal.getTime());
System.out.println(new Date());
Is there something simple, that I’m missing?
You’re printing out the result of
Date.toString(), which always uses the default time zone.I suggest you use
DateFormatinstead, which is better suited for formatting dates.Date.toStringis really only suitable for debugging – it provides no control over the format.Alternatively, use Joda Time for all your date and time operations – it’s a much better API to start with 🙂