Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Time Zone problem

 
Naresh Talluri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
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
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:
 
Naresh Talluri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have you tried java tutorial???
 
Naresh Talluri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Naresh Talluri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naresh,

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.
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to set the timezone on the same data/calendar object that you are printing. Like this:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic