SOLVED Ho scoperto che tra mezzanotte e l'1 il mio dispositivo restituisce un'ora 1 ora dopo (le altre 23 ore al giorno restituiscono correttamente). Più stranamente restituisce correttamente se uso kk
invece di HH
(anche se la stringa risultante è alcuna utilità per me)Android SimpleDateFormat restituisce l'ora non corretta tra mezzanotte e 01:00
Codice sto facendo funzionare: (in questo caso strFormat
corrisponde alla stringa hardcoded in df3
)
SimpleDateFormat df = new SimpleDateFormat(strFormat, Locale.US);
SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd kk:mm", Locale.US);
SimpleDateFormat df3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
Date d = c.getTime();
String s = d.toString();
String ret = df.format(c.getTime());
String ret2 = df.format(new Date(System.currentTimeMillis()));
String ret3 = df2.format(c.getTime());
String ret4 = df3.format(c.getTime());
String r1 = ""+c.get(Calendar.HOUR);
questi ritorno:
s = "Thu Jan 07 00:39:32 GMT-11:00 2016"
ret = "2016-01-07 01:39:32"
ret2 = "2016-01-07 01:39:32"
ret3 = "2016-01-07 24:39"
ret4 = "2016-01-07 01:39:32.525"
r1 = "0"
Dopo 1:00 questi ritorno:
s = "Thu Jan 07 01:07:09 GMT-11:00 2016"
ret = "2016-01-07 01:07:09"
ret2 = "2016-01-07 01:07:09"
ret3 = "2016-01-07 01:07"
ret4 = "2016-01-07 01:07:09.606"
r1 = "1"
Qualsiasi aiuto in ciò che ho fatto di sbagliato/ciò che sta andando storto è molto apprezzato.
UPDATE
codice cambiando a:
SimpleDateFormat df = new SimpleDateFormat(strFormat, Locale.US);
df.setTimeZone(TimeZone.getTimeZone("GMT"));
SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd kk:mm zzz", Locale.US);
df2.setTimeZone(TimeZone.getTimeZone("GMT"));
SimpleDateFormat df3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS zzz", Locale.US);
df3.setTimeZone(TimeZone.getTimeZone("GMT"));
Date d = c.getTime();
String s = d.toString();
String ret = df.format(c.getTime());
String ret2 = df.format(new Date(System.currentTimeMillis()));
String ret3 = df2.format(c.getTime());
String ret4 = df3.format(c.getTime());
String r1 = ""+c.get(Calendar.HOUR);
return ret;
ottengo lo stesso risultato:
s = "Fri Jan 08 00:52:05 GMT 2016"
ret = "2016-01-08 01:52:05"
ret2 = "2016-01-08 01:52:05"
ret3 = "2016-01-08 24:52 GMT"
ret4 = "2016-01-08 01:52:05.169 GMT"
r1 = "0"
e
s = "Fri Jan 08 01:03:23 GMT 2016"
ret = "2016-01-08 01:03:23"
ret2 = "2016-01-08 01:03:23"
ret3 = "2016-01-08 01:03 GMT"
ret4 = "2016-01-08 01:03:23.547 GMT"
r1 = "1"
in modo che il problema sembra essere correlato al fuso orario
Qual è il tuo fuso orario? – SlumpA
Sei sicuro di non aver cambiato nulla nel codice in quei 40 minuti? – SlumpA
Aggiungi "ZZZZ" per vedere il fuso orario in SimpleDateFormat, forse è diverso – headsvk