: String dateString = "20101230";
: SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
: Date date = sdf.parse(dateString);
: doc.addField("date", date);
: 
: In the index, the date "20101230" is saved as "2010-12-29T23:00:00Z" ( because
: of GMT).

"because of GMT" is missleading and vague ... what you get in your index 
is a value of "2010-12-29T23:00:00Z" because that is the canonical 
string representation of the date object you have passed to doc.addField 
-- the date object you have passed in represents that time, because you 
constructed a SimpleDateFormat object w/o specifying which TimeZone that 
SDF object should assume is in use when it parses it's string input.  So 
when you give it the input "20101230" it treats that is Dec 30, 2010, 
00:00:00.000 in whatever teh local timezone of your client is.

If you want it to treat that input string as a date expression in GMT, 
then you need to configure the parser to use GMT 
(SimpleDateFormat.setTimeZone)

: I tried the following code :
: 
: String dateString = "20101230";
: SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
: Date date = sdf.parse(dateString);
: SimpleDateFormat gmtSdf = new
: SimpleDateFormat("yyyy-MM-dd'T'HH\\:mm\\:ss'Z'");
: String gmtString = gmtSdf.format(Date);
: 
: The problem is that gmtString is equals to "2010-12-30T00\:00\:00Z". There is

again, that is not a "gmtString" .. in this case, both of the SDF objects 
you are using have not been configured with an explicit TimeZone, so they 
use whatever hte platform default is where this code is run -- so the 
variable you are calling "gmtString" is actaully a string representation 
of Date object formated in your local TimeZone.

Bottom line...

* when parsing a string into a Date, you really need to know (and be 
explicit to the parser) about what timezone is represented in that string 
(unless the formated of hte string includes the TimeZone)

* when building a query string to pass to solr, then the DateFormat 
you use to formate a Date object must format it using GMT -- there is a 
DateUtil class included in solrj to make this easier.

If you really don't care at all about TimeZones, then just use GMT 
everywhere .. but if you actually care about what time of day something 
happened, and want to be able to query for events with hour/min/sec/etc.. 
granularity, then you need to be precise about the TimeZone in every 
Formatter you use.


-Hoss

Reply via email to