Search code examples
javadatetimestamphbaseapache-phoenix

Problem showing correct HBase date type in Apache Phoenix


I got problem while reading date format data which created through hbase api (using 'put' command) from apache phoenix.

This is the date format what i expected : Date : 2018-12-17T21:21:45+07:00 into 'yyyy-MM-dd' => '2018-12-17'

The table is created from phoenix client using below DDL :

CREATE TABLE "TEST"
(
 ID VARCHAR,
 "col1".TRX_DATES UNSIGNED_DATE 
 CONSTRAINT PKD PRIMARY KEY (ID)
)

But, when i'm trying to query the data, it shows incorrect date format :

292269006-11-18

I've tried some research and experiment using various data type to insert date to hbase, include using long value (milliseconds) of the date. But still, it cannot show the correct date value in phoenix ('yyyy-MM-dd')

Any suggestions for this case?


Solution

  • Maybe this should have been a comment because I cannot explain all the details of what has gone wrong nor how you can fix it. But I can see that your problem comes from an inversion of the sign bit of the milliseconds since the epoch. I tried this:

        long milli = OffsetDateTime.parse("2018-12-17T21:21:45+07:00").toInstant().toEpochMilli();
        milli += Long.MIN_VALUE; // Flip the sign bit
        Date wrongDateAsOldfashiondDate = new Date(milli);
        System.out.println(wrongDateAsOldfashiondDate);
        System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(wrongDateAsOldfashiondDate));
    

    The output on my computer (in Europe/Copenhagen time zone) was:

    Fri Nov 18 08:08:49 CET 292269006
    292269006-11-18
    

    Normally one shouldn’t use the old and poorly designed Date class, but this was the way I could get exactly the same result as yours. The difference from the modern date and time API is that Date uses the Julian calendar, so apparently this was also used by the software giving your unexpected result (probably the Date class was involved there too).