Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SimpleDateFormat and Timezones

 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In order to make a particular query, I need to find all record entered since a particular DateTime.

The Query Language I'm using defines the DateTime to be in this format:
YYYY-MM-DDThh:mm:ss+hh:mm (ex: 1999-01-01T23:01:01+04:00)
YYYY-MM-DDThh:mm:ss-hh:mm (ex: 1999-01-01T23:01:01-08:00)

SimpleDateFormat almost gets me the right format... but the timezone isn't quite right. It doesn't put in the ":" in the timezone.

SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssZ" );
This produces 1999-01-01T23:01:01-0800

Any ideas on how to produce the correct format?

Thanks

- Jess
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think it's possible to get this in one step with SimpleDateFormat. You can get the colon by replacing 'Z' with 'z', but that also gives you a "GMT". I think the best solution would be to just use the SDF with the format you're currently useing, then put the result in a StringBuilder and insert a colon manually. That may seem inelegant, but it works.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just don't tell anybody where you got this from:

String date = formatter.format(new Date());
date = date.substring(0, date.length()-2) + ":00";

[ EDIT: Ah, Jim beat me to it. ]
[ August 08, 2005: Message edited by: Ernest Friedman-Hill ]
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks boys!

Didn't know if I was missing some magic in SimpleDateFormat.... bruteforce works nicely.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that some time zones (e.g. in India & Iran) do not end in 00, so I'd recommend
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, didn't know that!

You left out the colon, though.

date = date.substring(0, date.length()-2) + ":" + date.substring(date.length()-2);
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops. I would have to concede that the error in your code was smaller than the error in my code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic