Getting time GMT with Android
Estive pesquisando a questão há algum tempo no StackOverflowAndroid obtém a hora UTC atual eComo posso obter a data e hora atuais no UTC ou GMT em Java?
Tentei duas maneiras de obter a hora atual do meu telefone no GMT. Estou na Espanha e a diferença é GMT + 2. Então, vamos ver com um exemplo: 1º tentativa: criei um formato e o apliquei a System.currentTimeMillis ();
DateFormat dfgmt = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
dfgmt.setTimeZone(TimeZone.getTimeZone("GMT"));
String gmtTime = dfgmt.format(new Date());
//Using System.currentTimeMillis() is the same as new Date()
Date dPhoneTime = dfgmt.parse(gmtTime);
Long phoneTimeUTC = dPhoneTime.getTime();
Preciso subtrair esse tempo para outro momento, é por isso que faço o elenco para Lon
DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date arrivalDate = df.parse(item.getArrivalDate());
//the String comes from JSON and is for example:"UTC_arrival":"2011-05-16 18:00:00"
//which already is in UTC format. So the DateFormat doesnt have the GMT paramater as dfgmt
diff = arrival.getTime() - phoneTimeUTC ;
Eu também tentei isso:
Calendar aGMTCalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
Long phoneTimeUTC = aGMTCalendar.getTimeInMillis()
E ainda não entendi a diferença certa. Mas se eu fizer isso:
Long phoneTimeUTC = aGMTCalendar.getTimeInMillis()-3600000*2;
Funciona bem.
Alguma ideia
Muito obrigado
David.