Conversão de Horas e TimeZone [Resolvido]

3 respostas
maiconramones

Prezados, estou tentando recuperar a data e hora de um timezone diferente do meu. Por exemplo se aqui no Brasil é 12:00:00 eu quero recuperar a hora equivalente nos EUA. Sei que existe o Joda Time.

Existe alguma coisa na api do java que faz esse trabalho?

Estou usando Calendar, fiz uma classe de testes mas não funfou... :(

public class HoraTeste {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String[] zonas = TimeZone.getAvailableIDs();

//		 for (String zona : zonas) {
//			System.out.println(zona);
//		}

		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"dd/MM/yyyy HH:mm:ss");

		TimeZone timeZoneChile = TimeZone.getTimeZone("America/Santiago");
		TimeZone.setDefault(timeZoneChile);
		Locale localeChile = new Locale("esp", "CL");
		
		System.out.println(TimeZone.getDefault());
		
		Calendar calendar = GregorianCalendar.getInstance(timeZoneChile, localeChile);
		System.out.println(dateFormat.format(calendar.getTime()));
		System.out.println(calendar.getTimeZone().getID());

		calendar.setTimeZone(TimeZone.getTimeZone("Pacific/Kiritimati"));
		System.out.println(dateFormat.format(calendar.getTime()));
		System.out.println(calendar.getTimeZone().getID());

	}

}

3 Respostas

T

Você precisa setar o timezone no DateFormat, não no Calendar. É que você tem de converter o Calendar para um Date na hora de passar os dados para DateFormat.format, mas nessa hora você perde a informação de timezone. Por isso é que se seta o timezone no DateFormat.

bestlinux

Veja se ajuda:

# Date today = new Date();  
       
     // Get all time zone ids  
     String[] zoneIds = TimeZone.getAvailableIDs();  
       
     // View every time zone  
     for (int i=0; i<zoneIds.length; i++) {  
         // Get time zone by time zone id  
         TimeZone tz = TimeZone.getTimeZone(zoneIds[i]);  
       
         // Get the display name  
         String shortName = tz.getDisplayName(tz.inDaylightTime(today), TimeZone.SHORT);  
         String longName = tz.getDisplayName(tz.inDaylightTime(today), TimeZone.LONG);  
       
        // Get the number of hours from GMT  
         int rawOffset = tz.getRawOffset();  
        int hour = rawOffset / (60*60*1000);  
         int min = Math.abs(rawOffset / (60*1000)) % 60;  
       
         // Does the time zone have a daylight savings time period?  
         boolean hasDST = tz.useDaylightTime();  
       
         // Is the time zone currently in a daylight savings time?  
         boolean inDST = tz.inDaylightTime(today);  
     }
maiconramones

Nossa… não precisa de tanta coisa hehehe basta fazer como o thingol falou

dateFormat.setTimeZone(timeZoneChile);

Valeu ai thingol te devo uma ceva hehehe

Criado 13 de agosto de 2009
Ultima resposta 13 de ago. de 2009
Respostas 3
Participantes 3