wood burning stoves
The moose likes Java in General and the fly likes Time Zone problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Time Zone problem" Watch "Time Zone problem" New topic

Time Zone problem

Naresh Talluri
Ranch Hand

Joined: Oct 12, 2007
Posts: 115
I have a small problem with seting TimeZone.
in my apllication i have a date value as string , i need to parse it
as Date object, and setting default timezone as PST, then if i parsed
that string it is automatically converted into IST with different

here if i print the date iam getting like 05/19/2008 04:33 AM (IST)
but it has to show same except timezone as PST
after parsing the string using parse()

the output iam getting like Mon May 19 08:33:00 EDT 2008

but it is taking input as IST and setting to EDT as some time difference
but what i need is it has to take as PST what i had given in string
and if i change timezone as EST it has to show 3 hours difference as well as EST..

appriciate if any ideas..

the output value is :
Mon May 19 15:03:00 IST 2008

but i need :
05/19/2008 04:33 AM (EST)";

can you please let me know how can i do this..
appriciate if any ideas..

[ May 20, 2008: Message edited by: Naresh Talluri ]
Rajah Nagur
Ranch Hand

Joined: Nov 06, 2002
Posts: 239
Try this:

You can't wake a person who is <b><i>pretending</i></b> to be asleep.<br />Like what <b>"it"</b> does not like - <i> Gurdjieff </i>
Naresh Talluri
Ranch Hand

Joined: Oct 12, 2007
Posts: 115
Hi Rajah
thanks for reply, but i tried how you suggested but it is giving same
problem, can you please check..
Marko Kencheto
Ranch Hand

Joined: Jan 15, 2008
Posts: 33
have you tried java tutorial???
Naresh Talluri
Ranch Hand

Joined: Oct 12, 2007
Posts: 115
here iam ableto set the timezone, but while i formatting my date iam getting IST instead of PST

here is my code...

iam getting outputp like :
formatted date is-->05/19/2008 02:00 PM (IST)
temp date Mon May 19 01:30:00 PDT 2008

but i need the first format with second time
how can i do this one, appreciate for any ideas..

Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
One problem I see here is that no one uses PST or EST in May. It should be PDT or EDT - Pacific Daylight Time or Eastern Daylight Time. Whoever managed to put a PST on the original time there is probably mistaken, and should be ignored. (Or flogged.) This also appies to whoever is telling you that you "need" the output to be in EST. Not in May you don't. I would probably remove the final five characters from the String before parsing it, to avoid confusing Java's TimeZone class with a nonsense time zone indicator.

Also, three-letter time zones are not recommended for looking up TimeZone values. (Read the API for that class.) Unfortunately, Sun doesn't do a good job of documenting what your alternatives are. I recommend:

As for "I need the first format with the second time", I'm not sure what you mean. Can you show exactly what you think the output should look like?

I recommend you not use TimeZone.setDefault(), since you need two different time zones here (besides IST where you are presumably located). Instead call setTimeZone() on two different DateFormat objects - one for Pacific time, and one for Eastern. Use parse() with one, and format() with the other.

"I'm not back." - Bill Harding, Twister
Naresh Talluri
Ranch Hand

Joined: Oct 12, 2007
Posts: 115
Hi jim
thanks for your explanation
but here if i used the code which i had given , it is not setting
timezone properly, but if set using setTimeZone(TimeZone.getDefault());
it is able to set timezone.

the thing is if i set timezone to SimpleDateFormat it is working fine

here is the sample code...

and can you please let me know why we dont use setTimeZone(TimeZone.getDefault());
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265


It's not intuitive, but SimpleDateFormat is a different object than your Date, and has its own TimeZone member that will use the system's default time zone unless you set it differently.

As you discovered, you need to set the TimeZone of the SimpleDateFormat to change the TimeZone it uses for display.

TimeZone.getDefault() gets the default TimeZone for the host on which the JVM, which is apparently IST for you.

If you need your entire program to run as if it were in a different TimeZone than the system you are running on, then it's appropriate to use TimeZone.setDefault().

The use of three letter time-zone IDs is deprecated, which confirms what Jim was communicating. You can use TimeZone.getAvailableIDs() to get String[] array to discover what values are available.

There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
Pat Farrell

Joined: Aug 11, 2007
Posts: 4659

You need to set the timezone on the same data/calendar object that you are printing. Like this:

subject: Time Zone problem
It's not a secret anymore!