我使用以下方法来区分两个时间戳。
public static long tokenTimeDuration(String loginTime, String expiryTime){
long diff = 0;
Log.i("du_loginTimeFromServer",loginTime);
Log.i("du_expiryTimeFromServer",expiryTime);
DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss a z yyyy");
format.setLenient(false);
Date loginDate = null;
Date expiryDate = null;
try {
loginDate = (Date)format.parse(loginTime);
Log.i("du_loginTimeAfterParsing",format.format(loginDate));
expiryDate = (Date)format.parse(expiryTime);
Log.i("du_expiryTimeAfterParsing",format.format(expiryDate));
Log.i("du_loginTimeInMilliseconds",loginDate.getTime()+"");
Log.i("du_expiryTimeInMilliseconds",expiryDate.getTime()+"");
//in milliseconds
diff = expiryDate.getTime() - loginDate.getTime();
Log.i("du_tokenExpiryDuration", diff + "");
} catch (Exception e) {
Log.i("duEx", e.getMessage());
}
return diff;
}
但是在尝试使用该方法时,我得到了错误的日期转换。并记录输出如下:
12-05 19:30:45.802: I/du_loginTimeFromServer(16894): Fri Dec 05 14:00:44 PM UTC 2014
12-05 19:30:45.802: I/du_expiryTimeFromServer(16894): Fri Dec 05 17:20:44 PM UTC 2014
12-05 19:30:45.822: I/du_loginTimeAfterParsing(16894): Fri Dec 05 05:30:44 AM IST 2014
12-05 19:30:45.824: I/du_expiryTimeAfterParsing(16894): Fri Dec 05 05:50:44 AM IST 2014
12-05 19:30:45.824: I/du_loginTimeInMilliseconds(16894): 1417737644000
12-05 19:30:45.824: I/du_expiryTimeInMilliseconds(16894): 1417738844000
12-05 19:30:45.824: I/du_tokenExpiryDuration(16894): 1200000
现在,即使到期时间早于登录时间4小时,它也会产生负面差异。 请帮我找到我错在哪里。
更新: 如果我添加
format.setTimeZone(TimeZone.getTimeZone("UTC"));
logcat说:
12-05 22:05:59.903: I/du_loginTimeFromServer(23574): Fri Dec 05 16:35:58 PM UTC 2014
12-05 22:05:59.903: I/du_expiryTimeFromServer(23574): Fri Dec 05 19:55:58 PM UTC 2014
12-05 22:05:59.915: I/du_loginTimeAfterParsing(23574): Fri Dec 05 00:35:58 AM UTC 2014
12-05 22:05:59.918: I/du_expiryTimeAfterParsing(23574): Fri Dec 05 00:55:58 AM UTC 2014
12-05 22:05:59.918: I/du_loginTimeInMilliseconds(23574): 1417739758000
12-05 22:05:59.919: I/du_expiryTimeInMilliseconds(23574): 1417740958000
12-05 22:05:59.919: I/du_tokenExpiryDuration(23574): 1200000
仍然不正确。
答案 0 :(得分:1)
相同的代码片段是:
public static long tokenTimeDuration(String loginTime, String expiryTime){
DateTimeFormatter FMT = DateTimeFormat.forPattern("EEE MMM dd HH:mm:ss a z yyyy");
final DateTime dt1 = new DateTime(FMT.parseDateTime(loginTime).withZone(DateTimeZone.getDefault()));
Log.i("du_loginTimeAfterParsing",dt1.toString());
final DateTime dt2 = new DateTime(FMT.parseDateTime(expiryTime).withZone(DateTimeZone.getDefault()));
Log.i("du_expiryTimeAfterParsing",dt2.toString());
long diff = dt2.getMillis() - dt1.getMillis();
Log.i("du_tokenExpiryDuration", diff + "");
return diff;
}
logcat输出现在说:
12-06 06:48:22.073: I/du_loginTimeAfterParsing(10440): 2014-12-06T06:48:22.000+05:30
12-06 06:48:22.077: I/du_expiryTimeAfterParsing(10440): 2014-12-06T10:08:22.000+05:30
12-06 06:48:22.078: I/du_tokenExpiryDuration(10440): 12000000
已添加解决方案,以便其他人可以从中受益。
答案 1 :(得分:0)
时区未对齐,服务器为UTC
,解析后为IST
。尝试添加这样的东西
format.setTimeZone(TimeZone.getTimeZone("UTC"));
答案 2 :(得分:0)
只是在黑暗中拍摄(虽然我确实用此解决了类似的问题here),但尝试明确地将您的Locale
添加到SimpleDateFormat中:
DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss a z yyyy", Locale.US);//Or whatever your desired Locale is
此外,我认为没有必要将loginDate
和expiryDate
转换为Date,因为parse已经返回Date对象。