Pedro Dusso
Pedro Dusso

Reputation: 2140

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. Table A has the value 2013-01-01 15:11:48 and table B has 1-1-2013 15:11 GMT.

I'm parsing A as:

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:

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.

What I'm missing? I try to set the time zones to default (cal.setTimeZone(TimeZone.getDefault());) but it did not work.

Thanks

Upvotes: 0

Views: 76

Answers (1)

Erwin Bolwidt
Erwin Bolwidt

Reputation: 31299

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)
  2. 1-1-2013 15:11 GMT which is in the GMT timezone

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:

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)

Upvotes: 2

Related Questions