我想在 MapReduce 缩减端连接程序中使用日期时间作为键。表 A 的值为 2013-01-01 15:11:48
,表 B 的值为 1-1-2013 15:11 GMT
。
我将 A 解析为:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //2013-01-01 15:11:48
Calendar cal = Calendar.getInstance();
cal.setTime(df.parse(split[TaxiFields.PICKUP_DATETIME]));
cal.set(Calendar.SECOND, 0); //disconsidering the seconds
pickupDatetime.set(cal.getTime().getTime());
我将 B 解析为:
DateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm zzz"); //1-1-2013 15:11 GMT,
Calendar cal = Calendar.getInstance();
cal.setTime(df.parse(split[0]));
cal.set(Calendar.SECOND, 0);
rainDate.set(cal.getTime().getTime());
但是,第一个结果给了我 1357063860000
,而第二个结果给了我 1357053060000
。
我缺少什么?我尝试将时区设置为默认值 (cal.setTimeZone(TimeZone.getDefault());
),但它不起作用。
谢谢
最佳答案
您没有使用两个相等日期时间。你有:
2013-01-01 15:11:48
在您本地的时区(可能不是 GMT)1-1-2013 15:11 GMT
位于 GMT 时区
因此,这两个日期的不同之处在于您所在时区和 GMT 之间的小时数。
您可以像解析 GMT 时区一样解析第一个日期:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 2013-01-01 15:11:48
df.setTimeZone(TimeZone.getTimeZone("GMT"));
如果这样做,您将获得两个日期时间相同的值 (1357053060000
)
关于java - 获取两个相同日期时间的长值会产生不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26178559/