java gettimeinmillis,日历getTimeInMillis 1小时差

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

long timezoneOffset = cal.getTimeZone().getOffset(cal.getTimeInMillis());

long timeInSec = ((cal.getTimeInMillis() + timezoneOffset)/1000);

Update 2015/11/11 23:03

After checking thru the code again, i found it is my TimePicker giving the wrong value. As request by Mattia Maestrini, here is my codes that creates the Time Picker:

https://dl.dropboxusercontent.com/u/4256111/AddApptDialogFragment.java

Update 2015/11/12 07:14

Case closed. It is cos of the TimePicker. Pls look into the comment for the ans.

Update 2015/11/14 23:17

Omg... 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 shld be what u shld be looking at: http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#getTimeInMillis()

So I shld 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);

解决方案

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

public long getTimeInMillis()

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值