TimeZone problem

A

abhay

Hi all !

How can i convert a date from one timezone to a date in another
timezone ?
I coded the following ... but i feel that it's not correct ...

public Date getConvertedDate(TimeZone tzFrom,TimeZone tzTo,String
date) {
Date retDate = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy
HH:mm:ss");
sdf.setTimeZone(tzFrom);
Date dtFrom = sdf.parse(date);
sdf.setTimeZone(tzTo);
retDate = sdf.parse(sdf.format(dtFrom));
} catch(ParseException pe) {
pe.printStackTrace();
}

return retDate;
}

By simply setting the timezone into the simple date format, will i get
a date in that timezone ?
Please comment ...

thanks 4 your help !

warm regards
casper
 
P

P.Hill

abhay said:
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy
HH:mm:ss");
sdf.setTimeZone(tzFrom);
Date dtFrom = sdf.parse(date);
sdf.setTimeZone(tzTo);
retDate = sdf.parse(sdf.format(dtFrom));

That all looks fine, except you might consider figuring out
the Timezone for each SDF and then saving each one instead of
trying to keep track to which timezone the one SDF is currently
set.

Is there some problem?
Are you then printing out retDate and can't figure out
why it doesn't think it has moved TZ?
See my recent comments in related thread.
"Parsing IETF standard date format."
As I said there, a Date has no "idea of Timezones, UNTIL YOU
CONVERT IT TO A STRING. at that point it has to
pick a timezone. In that case it just uses the
VM default."

But if you knew that a Date is TZ neutral then you wouldn't send
it into SDF to try to change it. You don't want to change it,
you want to sav it as is, and record the TZ of the user or
whatever you think is needed to recreate original clock time.
One alternative is to pick some variation of ISO-8601 2003-12-13 23:59:59.000
and include TZ information.
By simply setting the timezone into the simple date format, will i get
a date in that timezone ?

No, Dates are not in timezones.
One way to prove this print out the binary time ( date.getTime() )
from your dtFrom and your retDate and see what they contain.

HTH,
-Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top