码迷,mamicode.com
首页 > 其他好文 > 详细

solr date 时区 8小时时差问题

时间:2015-06-03 17:53:30      阅读:445      评论:0      收藏:0      [点我收藏+]

标签:

初学solr在schema 里面配置并使用sorl的date或者tdate类型,具体类型与使用如下:
<field name="tdate_field"type="tdate"indexed="true"stored="true"/> 
 <field name="date_field"type="date"indexed="true"stored="true"/> 

 <fieldType name="date"class="solr.TrieDateField"precisionStep="0"positionIncrementGap="0"/
 <!-- A Trie based date field for faster date range queries and date faceting. -->
 <fieldType name="tdate"class="solr.TrieDateField"precisionStep="6"positionIncrementGap="0"/>
     在solr的web界面上进行query操作时,返回xml与json的时间结果总是相差8小时。但是sorj返回的得到的java.util.Date数据值又是正确的,没有相差8小时。
找到solr-crore包的 org.apache.solr.schema. DateField代码:
public static TimeZone UTC = TimeZone. getTimeZone("UTC");
     发现默认的时区是UTC。可见solr存的时间值是对的,只不过显示的时间是以"UTC"零时区的时间显示,所以返回给Date数据也是正确的。
    可以说,这是solr的一个坑,solr不提供时区配置的方式,只以" yyyy-MM-dd‘T‘HH:mm:ss‘Z‘"的时间格式返回xml或json的字符串,同时solr通过http传输,查询参数以字符串组装(好像只能以字符串方式建立主查询),例如:
Date date = new Date();
 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd‘T‘HH:mm:ss‘Z‘");
 String queryTime = QueryRule.ONLINE_TIME +":["+sdf.format(date )+"TO"+"*]";
query.setQuery(queryTime);
  这样做的话一般采用本机默认时区,自然就有8小时的相差。
 
可以按如下方式解决:
       
Date date = new Date(); 
 SimpleDateFormat sdf = new SimpleDateFormat("yyyy -MM-dd‘T‘HH:mm:ss‘Z‘"); 
 sdf.setTimeZone(TimeZone. getTimeZone("UTC")); 
 String queryTime = QueryRule.ONLINE_TIME +":["+sdf.format(date )+"TO"+"*]";
query.setQuery(queryTime);
 
但是还有一点还是很别扭,solr在网页,还有xml,json上返回的时间,还是以UTC为时区,相差8小时。
 
扯了半天,你可能早就想到了,用啥Date类型,干脆用long存得了。

solr date 时区 8小时时差问题

标签:

原文地址:http://my.oschina.net/u/2249726/blog/424364

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!