Search code examples
javaandroiddateutcgmt

Android get UTC milliseconds from GMT milliseconds


I have a time variable in GMT and I will convert in UTC. I post my code:

long mytime = 1376824500000;
Date date = new Date(mytime);
String time = new SimpleDateFormat("HH:mm").format(date);

This return "13:15" in some device, but I would like to have always UTC date: "11:15". How can I do that?


Solution

  • It's not clear what difference you're expecting between UTC and GMT - for the purposes we're talking here, they're equivalent. (They're not quite technically the same, but...)

    In terms of formatting, you just need to set the time zone on your formatter:

    // TODO: Consider setting a locale explicitly
    SimpleDateFormat format = new SimpleDateFormat("HH:mm");
    format.setTimeZone(TimeZone.getTimeZone("UTC"));
    String time = format.format(date);