• Post Reply Bookmark Topic Watch Topic
  • New Topic

SimpleDateFormat  RSS feed

 
Patrick Noah
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am trying to parse a date such that it will interpret the time as my current time zone. I read on google that I can do this by appending a Z in my format string, which I have tried with no success. What is the format string I need to use and what does the date I need to pass in look like?
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Patrick Noah wrote:I read on google that I can do this by appending a Z in my format string, which I have tried with no success. What is the format string I need to use and what does the date I need to pass in look like?


Well, no, that tells the SimpleDateFormat object to assume that the String represents a time in GMT. And presumably your timezone is not GMT?

The SimpleDateFormat will by default assume that the String represents a time in the system's default timezone. So all you have to do is to ensure that the system's default timezone is your timezone. It's very likely that you did that when you first got your computer, but if you didn't, then go and do it now.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Class DateFormat has a setTimeZone method. When the string you are parsing does not include timezone information, it will use the timezone you set with setTimeZone. If you don't call setTimeZone, it will by default assume your local system timezone. So you probably don't even have to do anything special to make it interpret the string in your local timezone.

For example:

Note that class java.util.Date itself does not know anything about timezones - it represents an "absolute" point in time.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!