Reputation: 3582
So I have some code:
String ANNOUNCEMENT_DATE = "Sat, 25-Aug-2014 11:00:00 GMT";
DateFormat df = new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss z");
Date announcementDate = null;
try {
announcementDate = df.parse(announcementDateString);
} catch (ParseException e) {
e.printStackTrace();
}
announcementEpochTime = announcementDate.getTime()/1000;
So, as I expected, up until 11:00:00 today, the following expression resolved to false:
System.currentTimeMillis()/1000 >= announcementEpochTime
However, after 11:00:00, I expected it to resolve to true...but it doesn't.
I'm looking at the fastTime value of the announcementDate, and it seems to be significantly larger than the current system time, even now (33 minutes later)
when I look at the parsed announcementDate in a debugger I see:
2014-08-25T12:00:00.000+0100
which looks wrong to me. I'm sure there's some issue with timezones or some-such, but I can't quite get my head around it.
Any idea why my date being parsed incorrectly?
I know these things are far from trivial problems in many circumstances, so I'm hoping someone will be familiar with these utilities and spot my problem out of hand (and save me half a day of googling frantically)
Upvotes: 0
Views: 128
Reputation: 235
@paul What time-jone your system has? If it is GMT then only your comparision is valid. otherwise you will have to consider timezone offset in comparision too.
Upvotes: 0
Reputation: 20608
You are obviously living in Ireland with a daylight saving time of +1 hour in summer.
That means, your time 12:00h IST corresponds to 11:00h GMT. And consequently that means: Your assumption will become true at 12:00h local time.
I tested it with this little program and a daylight savin time of +2 hours against GMT (in Germany):
public class Time {
public static void main(String[] args) {
String ANNOUNCEMENT_DATE1 = "Mon, 25-Aug-2014 10:00:00 GMT";
String ANNOUNCEMENT_DATE2 = "Mon, 25-Aug-2014 11:00:00 GMT";
DateFormat df = new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss z", Locale.ENGLISH);
Date announcementDate1 = null;
Date announcementDate2 = null;
try {
announcementDate1 = df.parse(ANNOUNCEMENT_DATE1);
announcementDate2 = df.parse(ANNOUNCEMENT_DATE2);
} catch (ParseException e) {
e.printStackTrace();
}
long now = System.currentTimeMillis();
System.out.println(now >= announcementDate1.getTime());
System.out.println(now >= announcementDate2.getTime());
}
}
Running that on 12:54 (local time) results in the following output:
true
false
Upvotes: 3
Reputation: 2583
May be your simpledateformat pattern should be ("EEE, dd-MMM-yyyy HH:mm:ss yyyy z")
as you have year before your GMT
Upvotes: 1