aspose file tools*
The moose likes Java in General and the fly likes SimpleDateFormat and Timezones Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "SimpleDateFormat and Timezones" Watch "SimpleDateFormat and Timezones" New topic
Author

SimpleDateFormat and Timezones

Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

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


- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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 ]

[Jess in Action][AskingGoodQuestions]
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

Thanks boys!

Didn't know if I was missing some magic in SimpleDateFormat.... bruteforce works nicely.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Jan 30, 2000
Posts: 18671
Oops. I would have to concede that the error in your code was smaller than the error in my code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SimpleDateFormat and Timezones