Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Working with dates, timezones, zulu

 
Anthony Smith
Ranch Hand
Posts: 285
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simple code...


String timestamp = "20050801042000Z+0500";
SimpleDateFormat fmtTimestamp = new SimpleDateFormat("yyyyMMddHHmmssZ");
Date deliveryDate = fmtTimestamp.parse(timestamp.replaceAll("Z", ""));
System.out.println(deliveryDate);

When I print this I get: 2005-07-31 18:20:00.0

But I am really wanting to see is this:
2005-08-01 9:20:00.0

What am I doing wrong?
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you want to produce the following, string right?

Because that would be the GMT time for your timestamp.
Anyhowdy, the problem with your code is that Date has no
timezone state -- when you output it (implicitly using its toString()),
it formats using the timezone of your JVM.
The solution is to use a DateFormat and set its timezone to GMT:
[CODE
String timestamp = "20050801042000Z+0500";
SimpleDateFormat fmtTimestamp = new SimpleDateFormat("yyyyMMddHHmmssZ");
Date deliveryDate = fmtTimestamp.parse(timestamp.replaceAll("Z", ""));

DateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
TimeZone timezone = TimeZone.getTimeZone("GMT+00:00");
outputFormat.setTimeZone(timezone);
System.out.println(outputFormat.format(deliveryDate));
[/CODE]
 
Anthony Smith
Ranch Hand
Posts: 285
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I want to see is this: 2005-08-01 9:20:00.0

I tried the code you gave me and it gave me this: 2005-07-31 18:20:00.0
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tnen I'm having trouble understanding your timestamp string:

20050801042000Z+0500

That's hour=04, minute=20, second=00, timezone=5 hours ahead of GMT, right?

That time zone make it somewhere in Pakistan, right?
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to make sure I understand how you view timezones:

20050801042000Z+0500

That's 5 hours ahead of GMT, right?

That timezone makes it somewhere in Pakistan, for example?
 
Anthony Smith
Ranch Hand
Posts: 285
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
right.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if it's 4:20 in Pakistan, why do you want your result to show 9:20?
Wouldn't that be the time in Guam? Wouldn't the time at UTC/GMT be 5 hours
earlier -- 23:20?
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic