The java.util.Date toString() method displays the date in the local time zone.
There are several common scenarios where we want the data to be printed in
The following simplified code, based on the accepted answer above, worked for me:
public class GetSync {
public static String ISO_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS zzz";
private static final TimeZone utc = TimeZone.getTimeZone("UTC");
private static final SimpleDateFormat isoFormatter = new SimpleDateFormat(ISO_FORMAT);
static {
isoFormatter.setTimeZone(utc);
}
public static String now() {
return isoFormatter.format(new Date()).toString();
}
}
I hope this helps somebody.