0

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?

Radek Postołowicz
  • 4,506
  • 2
  • 30
  • 47
finemi
  • 3
  • 1
  • It's because you have different time zones on both machines(machine that hosts Solr and local machine), your local machine uses CST as a default time zone, can you please check the default time zone on Solr machine ? – Youssef NAIT Jul 25 '16 at 10:44

2 Answers2

3

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)

1

China Standard Time (CST)

The answer by Alfredo is correct but for one assumption. That answer mentions Central Standard Time, but that zone is behind UTC (7 or 6 hours) while the data in the Question is ahead of UTC. So I presume CST here refers to China Standard Time which is 8 ahead of UTC.

This confusion shows why we should never use these 3-4 letter abbreviations such as CST. They are not true time zones, not standardized, and are not even unique (as we see here). Instead use proper tz/IANA time zones in the form of continent/region.

java.time

The problem is the use of the old date-time classes. These poorly designed and notoriously troublesome classes have been supplanted in Java 8 and later by the java.time framework.

That string should be directly parsed by Instant to get a value in UTC.

Instant instant = Instant.parse( "2016-04-20T13:58:35.805Z" );

Call toString to get the same kind of string back, formatted according to the ISO 8601 standard.

Apply a time zone to see the same moment through the lens of a particular locality’s wall-clock time. Apply a ZoneId to get a ZonedDateTime object.

For the example of America/Montreal the wall-clock time is four hours behind UTC, so the hour is 09 instead of 13.

ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = instant.atZone( zoneId );

2016-04-20T09:58:35.805-04:00[America/Montreal]

Both the Instant and the ZonedDateTime represent the same simultaneous point on the timeline.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Thank you!What's the best solution when I use solrj? I have to manually change the result every time.I want to change the result of solrj.For example, let it return DateTime or CST – finemi Jul 27 '16 at 06:12
  • My understanding of date, time, and computer science has just skyrocketed – Ders Jul 21 '21 at 20:54