Calendar getTimeInMillis difference of 1 hour

2019-05-21 09:03发布

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.

2条回答
仙女界的扛把子
2楼-- · 2019-05-21 09:44

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
查看更多
做个烂人
3楼-- · 2019-05-21 09:51

(Posted on behalf of the question author).

The problem was because of the TimePicker.

Calendar.currentTimeMillis() actually gives time independent of timezone (i.e. UTC). This link (Java: Timezone why different timezone give same value in millisec) is so misleading! This should be what you should be looking at: http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#getTimeInMillis()

So I should be just doing this:

Calendar cal = Calendar.getInstance();
cal.set(mDatePicker.getYear(), mDatePicker.getMonth(), mDatePicker.getDayOfMonth(), mTimePicker.getCurrentHour(), mTimePicker.getCurrentMinute(), 0);
long timeInSec = (cal.getTimeInMillis()/1000);
查看更多
登录 后发表回答