Search code examples
javasolrsolrjsolr6

Use solrj to get the date difference of 8 hours


There's a data in my solr: Using the Solr Administration User Interface, the modifyTime is "2016-04-20T13:58:35.805Z".

Using solrj: enter image description here, the modifyTime is "Wed Apr 20 21:58:35 CST 2016".

I'm using solr6. Why?


Solution

  • modifyTime data format coming from Solr Administration User Interface is UTC, you can understand it by the Z at the end of datetime string, indicating that this string representation of the date is in UTC.

    2016-04-20T13:58:35.805Z 
    

    On the other hand, modifyTime coming from data format is CST – Central China Standard Time, which in your case it seems to be + 8 hours.

    Wed Apr 20 21:58:35 CST 2016
    

    This happens because of a Java annoying and bad feature where the java.util.Date has no time zone yet it's toString method applies the JVM's current default time zone.

    java.util.Date date = ( java.util.Date ) doc.getFieldValue("modifyTime");
    DateTime dateTimeUtc = new DateTime( date, DateTimeZone.UTC );
    String output = dateTimeUtc.toString();
    

    EDIT

    Thanks to @BasilBourque for the suggestion on the meaning of CST (Time Zone Abbreviation)