Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

changing timzone using calender api

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a format in SimpleDateFormat which supports TimeZone.
 
nancy andrew
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Greenhorn
Posts: 7
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe you could have set the time zone on the SimpleDateFormat instead of the Calendar object to display correctly.

 
Ranch Hand
Posts: 64
Netbeans IDE Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!