• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

SimpleDateFormat and Timezones

 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks boys!

Didn't know if I was missing some magic in SimpleDateFormat.... bruteforce works nicely.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic