Search code examples
javaandroiddatepickercalendartimepicker

Calendar getTimeInMillis difference of 1 hour


I am writing an app which has a DatePicker and a TimePicker in the UI. I need to get the date and time set by the user and store in the server.

For example user chose "13 Nov 2015 13:00", and the timezone of my emulator is set as GMT+8, the returned timeInSec in GMT independent of time zone should be "1447390800", but it turns out to be "1447387200", a difference of 1 hour. End up my displayed time received from server also wrong.

Why is it so? Something to do with daylight savings in GMT timezone countries or what have I done wrongly in the code? In my country there is no daylight savings..

Here is my code:

Calendar cal = Calendar.getInstance();
cal.set(mDatePicker.getYear(), mDatePicker.getMonth(), mDatePicker.getDayOfMonth(), mTimePicker.getCurrentHour(), mTimePicker.getCurrentMinute(), 0);
// get time in seconds independent of timezone <- update on 2015/11/14: this is wrong!! 
long timezoneOffset = cal.getTimeZone().getOffset(cal.getTimeInMillis());
long timeInSec = ((cal.getTimeInMillis() + timezoneOffset)/1000);

Update

After checking through the code again, you found it is my TimePicker giving the wrong value.


Solution

  • If you want the UTC timestamp you can directly use getTimeInMillis()

    public long getTimeInMillis()

    Returns: the current time as UTC milliseconds from the epoch.

    Reference:
    http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#getTimeInMillis()

    For example set the calendar date to 13 Nov 2015 13:00 with a GMT+8 timezone:

    Calendar calendar = Calendar.getInstance();
    calendar.set(2015, Calendar.NOVEMBER, 13, 13, 0, 0);
    int timeInSec = calendar.getTimeInMillis() / 1000;
    

    the value of timeInSec is 1447390800

    EDIT

    What is the output you obtain with this snippet of code?

    Calendar calendar = Calendar.getInstance();
    calendar.set(2015, Calendar.NOVEMBER, 13, 13, 0, 0);
    int offset = calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET);
    long utcTimeInMillis = calendar.getTimeInMillis() + offset;
    
    Calendar utcCalendar = Calendar.getInstance(Locale.ENGLISH);
    utcCalendar.setTimeInMillis(utcTimeInMillis);
    
    Log.d(TAG, "Time: " + calendar.getTime());
    Log.d(TAG, "TimeInMillis: " + calendar.getTimeInMillis());
    Log.d(TAG, "DisplayName: " + calendar.getTimeZone().getDisplayName());
    Log.d(TAG, "Offset: " + offset);
    Log.d(TAG, "UTC Time: " + utcCalendar.getTime());
    Log.d(TAG, "UTC TimeInMillis: " + utcTimeInMillis);
    

    This is the result I obtained:

    Time: Fri Nov 13 13:00:00 GMT+08:00 2015
    TimeInMillis: 1447390800061
    DisplayName: Hong Kong Standard Time
    Offset: 28800000
    UTC Time: Fri Nov 13 21:00:00 GMT+08:00 2015
    UTC TimeInMillis: 1447419600061