SOLVED
I have found that between midnight and 1am my device returns a time 1 hour later (the other 23 hours a day it returns correctly). More weirdly it returns correctly if I use kk
instead of HH
(though the resulting string is no use to me)
Code I am running: (in this instance strFormat
matches the hardcoded string 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);
These return:
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"
After 1am these return:
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"
Any help in what I have done wrong/what is going wrong greatly appreciated.
UPDATE
changing code to:
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;
I get the same results:
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"
and
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"
so the problem seems to be unrelated to TimeZone
May be you have to set the timeZone for your dateFormat objects.
I tried this :
Calendar c = new GregorianCalendar(TimeZone.getTimeZone("GMT-11:00"), Locale.US);
c.set(2016, 0, 7, 00, 39, 32);
Date date = c.getTime();
SimpleDateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
df.setTimeZone(TimeZone.getTimeZone("GMT-11:00"));
SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd kk:mm", Locale.US);
df2.setTimeZone(TimeZone.getTimeZone("GMT-11:00"));
SimpleDateFormat df3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
df3.setTimeZone(TimeZone.getTimeZone("GMT-11:00"));
String ret = df.format(date.getTime());
String ret2 = df.format(date.getTime());
String ret3 = df2.format(date.getTime());
String ret4 = df3.format(date.getTime());
System.out.println(" ret : "+ret);
System.out.println(" ret2 : "+ret2);
System.out.println(" ret3 kk: "+ret3);
System.out.println(" ret4 : "+ret4);
The result is correct :
ret : Thu Jan 07 00:39:32 GMT-11:00 2016
ret2 : Thu Jan 07 00:39:32 GMT-11:00 2016
ret3 kk: 2016-01-07 24:39
ret4 : 2016-01-07 00:39:32.217
Idk... honestly. I tried recreting your problem, but in Eclipse on PC, not in Android. This is what I used:
Calendar c = new GregorianCalendar(TimeZone.getTimeZone(ZoneId.ofOffset("", ZoneOffset.ofHours(-11))), Locale.US);
c.set(2016, 0, 7, 0, 7);
String strFormat = "yyyy-MM-dd HH:mm:ss.SSS";
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);
System.out.printf("s = %s%nret = %s%nret2 = %s%nret3 = %s%nret4 = %s%nr1 = %s%n%n", s, ret, ret2, ret3, ret4, r1);
df.setTimeZone(TimeZone.getTimeZone(ZoneId.ofOffset("", ZoneOffset.ofHours(-11))));
df2.setTimeZone(TimeZone.getTimeZone(ZoneId.ofOffset("", ZoneOffset.ofHours(-11))));
df3.setTimeZone(TimeZone.getTimeZone(ZoneId.ofOffset("", ZoneOffset.ofHours(-11))));
d = c.getTime();
s = d.toString();
ret = df.format(c.getTime());
ret2 = df.format(new Date(System.currentTimeMillis()));
ret3 = df2.format(c.getTime());
ret4 = df3.format(c.getTime());
r1 = ""+c.get(Calendar.HOUR);
System.out.printf("s = %s%nret = %s%nret2 = %s%nret3 = %s%nret4 = %s%nr1 = %s%n%n", s, ret, ret2, ret3, ret4, r1);
c.set(2016, 0, 7, 1, 7);
d = c.getTime();
s = d.toString();
ret = df.format(c.getTime());
ret2 = df.format(new Date(System.currentTimeMillis()));
ret3 = df2.format(c.getTime());
ret4 = df3.format(c.getTime());
r1 = ""+c.get(Calendar.HOUR);
System.out.printf("s = %s%nret = %s%nret2 = %s%nret3 = %s%nret4 = %s%nr1 = %s%n", s, ret, ret2, ret3, ret4, r1);
And this is what I got:
s = Thu Jan 07 12:07:58 CET 2016
ret = 2016-01-07 12:07:58.662
ret2 = 2016-01-07 17:55:58.697
ret3 = 2016-01-07 12:07
ret4 = 2016-01-07 12:07:58.662
r1 = 0
s = Thu Jan 07 12:07:58 CET 2016
ret = 2016-01-07 00:07:58.662
ret2 = 2016-01-07 05:55:58.718
ret3 = 2016-01-07 24:07
ret4 = 2016-01-07 00:07:58.662
r1 = 0
s = Thu Jan 07 13:07:58 CET 2016
ret = 2016-01-07 01:07:58.662
ret2 = 2016-01-07 05:55:58.722
ret3 = 2016-01-07 01:07
ret4 = 2016-01-07 01:07:58.662
r1 = 1
Maybe there is a problem in your SimpleDateFormat, which uses a different timezone... This is really interesting.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With