redochka
redochka

Reputation: 12829

date time in Android inconsistencies

I am stuck with this example running on Android:

SimpleDateFormat sdf = new SimpleDateFormat("dd-mm-yyyy kkmm"); 
sdf.setTimeZone(TimeZone.getTimeZone(Time.TIMEZONE_UTC));

Date scheduledDateTime = sdf.parse(sms.getScheduledDateTime());

Log.i(TAG, "scheduledDateTime is : " + scheduledDateTime);    // scheduledDateTime is : Fri Jan 06 23:58:00 HNEC 2012
Log.i(TAG, "scheduledDateTime milli : " + scheduledDateTime.getTime());    // scheduledDateTime milli : 1325890680000

Calendar calendar = Calendar.getInstance();
Calendar defaultTZCalendar = Calendar.getInstance(TimeZone.getDefault());
Calendar utcTZCalendar = Calendar.getInstance(TimeZone.getTimeZone(Time.TIMEZONE_UTC));

calendar.setTime(scheduledDateTime);
defaultTZCalendar.setTime(scheduledDateTime);
utcTZCalendar.setTime(scheduledDateTime);

Log.i(TAG, "calendar : " + calendar.getTimeInMillis());    // calendar : 1325890680000
Log.i(TAG, "defaultTZCalendar : " + defaultTZCalendar.getTimeInMillis());    // defaultTZCalendar : 1325890680000
Log.i(TAG, "utcTZCalendar : " + utcTZCalendar.getTimeInMillis());    // utcTZCalendar : 1325890680000

And now look at this:

final long currentTimeMillis = System.currentTimeMillis();
Log.i(TAG, "  currentTimeMillis is " + currentTimeMillis);    //  currentTimeMillis is 1341608182431
Date d = new Date(currentTimeMillis);
Log.i(TAG, "  currentTimeMillis is " + d + " ::: d.getTime() = " + d.getTime());    // currentTimeMillis is Fri Jul 06 22:56:22 HAEC 2012 ::: d.getTime() = 1341608182431

How is possible to have such a difference of 15713902431 between two date that looks the same : Fri Jan 06 23:58:00 HNEC 2012 and Fri Jul 06 22:56:22 HAEC 2012

BTW,

HNEC is (in french) Normal Central Europ time

HAEC is Advanced Central Europ time (which is DST)

Thanks

Upvotes: 0

Views: 293

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500675

All looks fine to me at the end part. The dates don't "look the same". One is in July, the other is January. So 15713902431 milliseconds - nearly 182 days - seems entirely reasonable.

Now as for why your data isn't parsing correctly - your format is broken:

new SimpleDateFormat("dd-mm-yyyy kkmm"); 

Note the "mm" between "dd" and "yyyy". mm is minutes, not months.

I strongly suspect you should be using:

new SimpleDateFormat("dd-MM-yyyy kkmm"); 

Then you might get values in the right month :)

Upvotes: 2

Related Questions