File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes changing timzone using calender api Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "changing timzone using calender api" Watch "changing timzone using calender api" New topic
Author

changing timzone using calender api

nancy andrew
Ranch Hand

Joined: Jan 19, 2010
Posts: 30
I am trying to change timezone to EST (New York) time. But the below code still give me time in GMT. Kindly help me. My code is below
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3096
    
  33

Use a format in SimpleDateFormat which supports TimeZone.


Mohamed Sanaulla | My Blog
nancy andrew
Ranch Hand

Joined: Jan 19, 2010
Posts: 30
My code started working when i removed the formatter


But i have a question. Calender functionality takes the system date and converts it into a desired format. In case someone alters the system date then program will not give desired results. What can be done so that it takes date , say from internet server or some other place. What changes we need to make to the above code
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3096
    
  33

The code worked because the format specified in the SimpleDateFormat didnt support Timezone information. You need to add "z" at the end of the string passed to SimpleDateFormat for timezone information to be shown.

Please start a new thread for a new question. (There should be some API available, not sure if its in JDK, to get the correct time from the Internet).
M Massa
Greenhorn

Joined: Sep 03, 2010
Posts: 7
I believe you could have set the time zone on the SimpleDateFormat instead of the Calendar object to display correctly.

Jacob Anawalt
Ranch Hand

Joined: Oct 27, 2010
Posts: 64

Setting the formatter's calendar's timezone like that seems cleanest and side-effect free. If you want to use the timezone of a passed calendar, be safe and clone:



This is a pretty good resource for common mistakes with calendars and formatters: http://www.odi.ch/prog/design/datetime.php


Jacob
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: changing timzone using calender api