tags:

views:

25

answers:

1

Hi,

I am in a locale where the time is two hours ahead GMT+2. When I encode a date using new GregorianCalendar(y,m,d,d,h,m,s) and then use DateTools.dateToString with DAY resolution, I end up getting the day before.

Encoding 12:00 midnight 1,1,1970 I end up getting the 31st of january (22:00) which is clearly incorrect. The problem is even worse because stringToDate doesn't give me the same number back.

Apparently this monstrous confusion is by design.

What is the correct way to compensate for this so that a birth date can actually be searched correctly.

Thanks

+1  A: 

Which constructor are you using to create the GregorianCalendar? If you don't specify the time zone, it will just use the default, which is the time zone of the machine you happen to be running the code on.

Make sure the Date object you pass into dateToString is normalized to GMT correctly. On the search side of things you'll need to normalize the date queries to GMT as well.

There is no way getting around normalization -- you don't know where you code is going to be executed, so you'll need to anchor your dates by normalizing to GMT

bajafresh4life
Thanks,I figured it out, exactly as you point out. The important thing (in my opinion) is that dateToString and stringToDate should be symmetric, which they are not. I am sure opinions vary, but in mine, this is confusing. Anyway - your advice was spot on.Thank You
Jim
Great! If you think about it, dateToString can't be symmetric with stringToDate, because dateToString just normalizes to GMT and you lose the time zone info. stringToDate doesn't know what the original time zone is, so it just assumes the string is in GMT when it's converted back.
bajafresh4life