Transform UTC milliseconds into other Time Zone

2

I get a date from the service in milliseconds and UTC.

I need to transform into the User's Time Zone.

For this I did the following:

public static void main(String[] args) {
    // Milisegundos está em UTC...
    long time = 1457037493000L; 
    String format = "HH:mm:ss";

    SimpleDateFormat sdfUtc = new SimpleDateFormat(format, Locale.getDefault());
    sdfUtc.setTimeZone(TimeZone.getTimeZone("UTC"));

    SimpleDateFormat sdfLocal = new SimpleDateFormat(format, Locale.getDefault());
    sdfLocal.setTimeZone(TimeZone.getDefault()); // Meu TimeZone é -3...

    System.out.println("Local : "+sdfLocal.format(time));
    System.out.println("UTC : "+sdfUtc.format(time));

}

Result:

  

Location: 17:38:13
  UTC: 20:38:13

Actually 17:38:13 is the time in UTC!

And the location would be 14:38:13 because my Time Zone is -3.

How do I fix the date in UTC for the user's Time Zone?

    
asked by anonymous 03.03.2016 / 20:03

1 answer

0

I was able to do this by adding getOffset of TimeZone.

  public static long convertToTimeZoneDefault(final long time, final TimeZone zone){
        final Date localTime = new Date(time);
        final Date fromGmt = new Date(time + zone.getOffset(localTime.getTime()));
        return fromGmt.getTime();
    }
    
04.03.2016 / 12:27