I'm not sure I understand what you're trying to achieve. I assume you
need the util.Date object for something other than display and are
just printing it to help you develop your code?

If you call toDate() on a DateTime, it gives you a java.util.Date with
the same number of milliseconds since 1-1-1970 GMT. Problem with
java.util.Date is it has no time zone information, and therefore
doesn't know you changed that earlier. If you toString() that
java.util.Date, it uses the system timezone to print its output,
giving you the client's view of the time.

If your goal is to print that same moment in time in the server's
timezone, use a java.text.DateFormat and call its setTimeZone() method
before calling format(). Don't use the toString() method of
java.util.Date. I think this is what you're looking for.

The thing to keep in mind is that time zones are just "presentation
logic" invented because we humans like midnight to be 00:00 wherever
we are on the globe. If it's 9pm in London and 10pm in Amsterdam
today, it's the same number of milliseconds since 1-1-1970 GMT (or
since the Beginning of Time, if you will). When you call
withTimeZone() on a DateTime instance, it returns a new instance with
the new time zone, but with the originial millisecond value. When you
print out that new DateTime, it shows up differently because the zone
is different.

If you need the time in a different timezone but with the same
day/month/year/hour/minute/second numbers, call the
withZoneRetainFields() of the DateTime instance. This gives you a new
DateTime object with a different number of milliseconds since 1970 (in
other words, a different moment in history). This is an uncommon
requirement.


HTH,
Barend





On Thu, Apr 23, 2009 at 8:28 AM, suganthi kaliamoorthy
<suganth...@yahoo.com> wrote:
>
> Thanks Barend.
>
> How the same can be acheived using DateTime?
> I did following operations client side with timezone IST.
>
> >From client side I used withZone, passed server timezone id, to convert 
> >Datetime object of client timezone(IST) to DateTimeobject of server 
> >timezone(PST/PDT). I tried printing DateTime and it gave me correct server 
> >timezone.
>
> But my ultimate aim is to fetch the date object . So I used 
> DateTime.toDate(). The resultant gave me client time. Can you please help in 
> this regard?
>
> Thanks again.
> Suganthi.
>
>
> --- On Thu, 4/23/09, W.B. Garvelink <barend.garvel...@gmail.com> wrote:
>
>> From: W.B. Garvelink <barend.garvel...@gmail.com>
>> Subject: Re: [Joda-interest] Converting Date from one timezone to another
>> To: "Discussion of the Joda project" <joda-interest@lists.sourceforge.net>
>> Date: Thursday, April 23, 2009, 2:53 AM
>> Hi Suganthi,
>>
>> The java.util.Date object has no timezone information, so
>> there is no
>> such conversion possible.
>>
>> Since you're already posting on the Joda-Time mailing
>> list, the
>> easiest fix is to stop using java.util.Date altogether and
>> switch to
>> using Joda's DateTime class.
>>
>> If you're forced to use util.Date (I think there is a
>> UN resolution
>> against that... there should be), you'll have to carry
>> around the time
>> zone information in a separate variable and pass it into
>> the
>> DateFormat before you parse or print a util.Date as String.
>> You can
>> also use util.Calendar instead of util.Date. The Calendar
>> object
>> carries a time zone with it, but keep in mind that if you
>> use it and
>> DateFormat together, you still have to set the time zone on
>> the
>> DateFormat by hand. It's an error-prone mess, avoid if
>> you can.
>>
>>
>> Hope this helps,
>>
>> Barend
>>
>>
>>
>> On Wed, Apr 22, 2009 at 7:54 PM, suganthi kaliamoorthy
>> <suganth...@yahoo.com> wrote:
>> >
>> > Hi,
>> >
>> > Can someone please help how to convert Date object
>> from one timezone to another?
>> >
>> > I have java.util.Date object with currentTime in one
>> zone. Have to convert this to date of different timezone.
>> >
>> > Thanks,
>> > Suganthi.
>> >
>>
>> ------------------------------------------------------------------------------
>> Stay on top of everything new and different, both inside
>> and
>> around Java (TM) technology - register by April 22, and
>> save
>> $200 on the JavaOne (SM) conference, June 2-5, 2009, San
>> Francisco.
>> 300 plus technical and hands-on sessions. Register today.
>> Use priority code J9JMT32. http://p.sf.net/sfu/p
>> _______________________________________________
>> Joda-interest mailing list
>> Joda-interest@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/joda-interest
>
>
>
>
>
> ------------------------------------------------------------------------------
> Stay on top of everything new and different, both inside and
> around Java (TM) technology - register by April 22, and save
> $200 on the JavaOne (SM) conference, June 2-5, 2009, San Francisco.
> 300 plus technical and hands-on sessions. Register today.
> Use priority code J9JMT32. http://p.sf.net/sfu/p
> _______________________________________________
> Joda-interest mailing list
> Joda-interest@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/joda-interest
>

------------------------------------------------------------------------------
Crystal Reports &#45; New Free Runtime and 30 Day Trial
Check out the new simplified licensign option that enables unlimited
royalty&#45;free distribution of the report engine for externally facing 
server and web deployment.
http://p.sf.net/sfu/businessobjects
_______________________________________________
Joda-interest mailing list
Joda-interest@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/joda-interest

Reply via email to