1
votes

I have one date in millisecond -6847824600000.This is the input data used in pig and getting converted by using ToDate() function which internally use Joda date time to calculate the date.

The output format is like hour minitue second - 00:23:28.(Actual) But in java Date it is giving 00:00:00 which is (Expected) output.

The local time zone is Asia/Calcutta.I have also tried with Etc/GMT+0 time zone. Its not giving same result as java date. I have never used Joda date time. Please help how to get the similar output of Java date .

1
What exactly does the number 6847824600000 mean? Is that a count from epoch? If so, what epoch? And what granularity (seconds, milliseconds or such)? Did you mean that hyphen as a negative sign? Is this a date-time but you only give us a time-of-day? How do expect to explain the conversion when you do not post the code for this mysterious toDate method? - Basil Bourque
it is a negative epoch time.the time is 1753-01-01:00:00:00.To date is a built in function in apache pig. - sp_user123

1 Answers

1
votes

You have stumbled on an interesting oddity.

Seems that when a time zone is applied to this date-time of the year 1752 the offset-from-UTC is mysteriously altered by 7 minutes and 2 seconds under either java.time or Joda-Time.

Instead of an offset of -08:00 we see -07:52:58.

The old java.util.Date class shows no such surprise.

I do not have a solution or explanation. But I can demonstrate the mystery.

Time Zone America/Los_Angeles

First we try my own default time zone of America/Los_Angeles.

java.util.Date

// java.util.Date class.
java.util.Date date = new java.util.Date ( input );

java.time

// java.time framework (built into Java 8 and later).
Instant instant = Instant.ofEpochMilli ( input );
ZonedDateTime zdt = ZonedDateTime.ofInstant ( instant , ZoneId.systemDefault () );

Joda-Time

// Joda-Time library, version 2.9.2.
org.joda.time.Instant jodaInstant = new org.joda.time.Instant ( input );
DateTime dateTime = new DateTime ( input );

Dump to console.

    System.out.println ( "---|  java.util.Date  |-------" );
    System.out.println ( "JVM’s current default time zone: " + TimeZone.getDefault () );
    System.out.println ( "date: " + date );

    System.out.println ( "---|  java.time  |-------" );
    System.out.println ( "Current ZoneId: " + ZoneId.systemDefault () );
    System.out.println ( "instant: " + instant + " | zdt: " + zdt );

    System.out.println ( "---|  Joda-Time  |-------" );
    System.out.println ( "Current DateTimeZone: " + DateTimeZone.getDefault () );
    System.out.println ( "jodaInstant: " + jodaInstant + " | dateTime: " + dateTime );

When run.

---|  java.util.Date  |-------
JVM’s current default time zone: sun.util.calendar.ZoneInfo[id="America/Los_Angeles",offset=-28800000,dstSavings=3600000,useDaylight=true,transitions=185,lastRule=java.util.SimpleTimeZone[id=America/Los_Angeles,offset=-28800000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]
date: Sun Dec 31 10:30:00 PST 1752
---|  java.time  |-------
Current ZoneId: America/Los_Angeles
instant: 1752-12-31T18:30:00Z | zdt: 1752-12-31T10:37:02-07:52:58[America/Los_Angeles]
---|  Joda-Time  |-------
Current DateTimeZone: America/Los_Angeles
jodaInstant: 1752-12-31T18:30:00.000Z | dateTime: 1752-12-31T10:37:02.000-07:52:58

Time Zone Asia/Kolkata

Now we add this single line up top, to specify Asia/Kolkata as the time zone.

TimeZone.setDefault ( TimeZone.getTimeZone ( "Asia/Kolkata" ) );

Asia/Kolkata is the modern time zone of India, normally an offset-from-UTC of +05:30 (five and a half hours ahead of UTC). Yet in this example we see the offset similarly affected by several minutes and a few seconds in both java.time and Joda-Time. Meanwhile the old java.util.Date shows the first moment of the following year 1753.

When run.

---|  java.util.Date  |-------
JVM’s current default time zone: sun.util.calendar.ZoneInfo[id="Asia/Kolkata",offset=19800000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null]
date: Mon Jan 01 00:00:00 IST 1753
---|  java.time  |-------
Current ZoneId: Asia/Kolkata
instant: 1752-12-31T18:30:00Z | zdt: 1753-01-01T00:23:28+05:53:28[Asia/Kolkata]
---|  Joda-Time  |-------
Current DateTimeZone: Asia/Kolkata
jodaInstant: 1752-12-31T18:30:00.000Z | dateTime: 1753-01-01T00:23:28.000+05:53:28