Es bastante sencillo, utilizando la clase DateFormat (y derivados), convertir un conjunto fecha/hora a otra zona horaria.
Código a continuación:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
/**
* Simple Java program to convert local time into GMT or any other TimeZone in Java
* SimpleDateFormat in Java can be used to convert Date from one timezone to other
* @author Javin
*/
public class TimeZoneConverter {
public static void main(String args[]) {
//Date will return local time in Java
Date localTime = new Date();
//creating DateFormat for converting time from local timezone to GMT
DateFormat converter = new SimpleDateFormat("dd/MM/yyyy:HH:mm:ss");
//getting GMT timezone, you can get any timezone e.g. UTC
converter.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("local time : " + localTime);;
System.out.println("time in GMT : " + converter.format(localTime));
}
}
Si ejecutamos, la salida es:
local time : Wed Apr 11 05:48:16 VET 2012 time in GMT : 11/04/2012:10:18:16
La clase TimeZone por su parte permite especificar el huso horario en varios formatos diferentes, que podemos ver en este enlace.
Vía | Java Revisited