Solrj格式化我的日期从UTC到CEST [英] Solrj formats my dates from UTC to CEST

查看:243
本文介绍了Solrj格式化我的日期从UTC到CEST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在应用程序中发现了一个日期字段的问题。我有一个日期字段,索引在索尔的UTC格式。例如,parution_date:2014-09-29T22:00:00Z,。当我使用枢纽向Solr发出请求时,我想获得这个值,Solr将我的日期转换为CET(我在欧洲)。
这是我收到的相同文件的日期: Tue Sep 30 00:00:00 CEST 2014



如您所见,而不是将日期保留为UTC的原始格式,它将转换日期CEST。我想保留UTC格式,因为我使用这个字段来研究一些文档。



对于这个请求,我使用的是一个方位。以下是如何获得结果:

  for(PivotField pv:pivotFields){

日期value =(Date)pv.getValue();
String parutionDate = formatDate(value,DateHelper.DD_MM_YYYY_FORMAT);

}

getValue方法返回一个对象,所以我必须投射它到Date对象。



我看到一些解决方案:




  • 当我向Solr发出请求将我的CEST日期转换为UTC,但是现在我不知道该怎么做。我是Solr的初学者。

  • 或将我的日期保持为UTC格式。



处理这个问题的最好的解决方案?任何帮助将不胜感激。

解决方案

显示Solr与您的问题无关。



java.util.Date有一个令人讨厌的坏功能,对象没有时区,但它的 toString 方法应用JVM的当前默认时区。



最佳方法是避免使用java.util.Date和.Calendar类,因为它们是非常麻烦的。而是使用Joda-Time库或Java 8捆绑的新的java.time包(灵感来自Joda-Time)。



Joda-Time 2.4中的示例。 p>

  java.util.Date date =(java.util.Date)PC.getValue(); 
DateTime dateTimeUtc = new DateTime(date,DateTimeZone.UTC);
String output = dateTimeUtc.toString();

调整到本地区域。

  DateTime dateTimeBerlin = dateTimeUtc.withZone(DateTimeZone.forID(Europe / Berlin)); 


I just discovered a problem with a date field in my application. I have a date field that is indexed in Solr in UTC format. For instance, "parution_date": "2014-09-29T22:00:00Z",. When I make request to Solr using a pivot, and I want to get this value, Solr converts my date to CET (I'm in Europe). Here the date I received for the same document: Tue Sep 30 00:00:00 CEST 2014.

As you see here, instead of keeping the date to its original format which is UTC, it converts the date CEST. I want to keep the UTC format because I using this field to research some documents.

For this request, I am using a facet pivot. Here is how, I get the results :

for(PivotField pv : pivotFields){

        Date value = (Date) pv.getValue();
        String parutionDate = formatDate(value, DateHelper.DD_MM_YYYY_FORMAT);

    }

The getValue method, returns an object so I have to cast it to a Date object.

I see some solutions :

  • When I am making a request to Solr, convert my CEST date to UTC, but I don't know how to do it for now. I am beginner at Solr.
  • or keep my date to UTC formats.

What are the best solutions to deal with this issue? Any help would be appreciated.

解决方案

Appears that Solr has nothing to do with your problem.

The java.util.Date has an annoying bad feature where the object has no time zone yet it's toString method applies the JVM's current default time zone.

Best approach is to avoid using java.util.Date and .Calendar classes as they are notoriously troublesome. Instead use either Joda-Time library or the new java.time package bundled with Java 8 (inspired by Joda-Time).

Example in Joda-Time 2.4.

java.util.Date date = ( java.util.Date ) PC.getValue();
DateTime dateTimeUtc = new DateTime( date, DateTimeZone.UTC );
String output = dateTimeUtc.toString();

Adjust to a local zone.

DateTime dateTimeBerlin = dateTimeUtc.withZone( DateTimeZone.forID( "Europe/Berlin" ) );

这篇关于Solrj格式化我的日期从UTC到CEST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆