简体   繁体   English

在两个相等的日期时间获得长值会产生不同的值

[英]Getting long value for two equal date times yields different values

I want to use a date time as key in a MapReduce reduce-side join program. 我想使用日期时间作为MapReduce减少端连接程序中的键。 Table A has the value 2013-01-01 15:11:48 and table B has 1-1-2013 15:11 GMT . 表A的值为2013-01-01 15:11:48 ,表B的值为1-1-2013 15:11 GMT

I'm parsing A as: 我将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());

I'm parsing B as: 我将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());

However the first gives me a 1357063860000 as result, while the second gives me 1357053060000 . 但是第一个给我一个1357063860000结果,而第二个给我1357053060000

What I'm missing? 我想念的是什么? I try to set the time zones to default ( cal.setTimeZone(TimeZone.getDefault()); ) but it did not work. 我尝试将时区设置为默认( cal.setTimeZone(TimeZone.getDefault()); ),但是它没有用。

Thanks 谢谢

You are not using two equal date times. 您没有使用两个相等的日期时间。 You have: 你有:

  1. 2013-01-01 15:11:48 in your local time zone (which is presumably not GMT) 2013-01-01 15:11:48在您当地的时区(可能不是格林尼治标准时间)
  2. 1-1-2013 15:11 GMT which is in the GMT timezone 1-1-2013 15:11 GMT (格林尼治标准时间)

So these two dates differ by the number of hours between your timezone and GMT. 因此,这两个日期的时区与格林尼治标准时间之间的小时数有所不同。

You can parse the first date as if it was in the GMT timezone: 您可以将第一个日期解析为GMT时区:

DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 2013-01-01 15:11:48
df.setTimeZone(TimeZone.getTimeZone("GMT"));

If you do that, you'll get the same value for both date-times ( 1357053060000 ) 如果这样做,则两个日期时间都将获得相同的值( 1357053060000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM