aspose file tools*
The moose likes Beginning Java and the fly likes Working with dates, timezones, zulu Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Working with dates, timezones, zulu" Watch "Working with dates, timezones, zulu" New topic
Author

Working with dates, timezones, zulu

Anthony Smith
Ranch Hand

Joined: Sep 10, 2001
Posts: 285
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

Joined: Sep 16, 2005
Posts: 1780
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]


There is no emoticon for what I am feeling!
Anthony Smith
Ranch Hand

Joined: Sep 10, 2001
Posts: 285
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

Joined: Sep 16, 2005
Posts: 1780
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

Joined: Sep 16, 2005
Posts: 1780
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

Joined: Sep 10, 2001
Posts: 285
right.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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
 
subject: Working with dates, timezones, zulu
 
Similar Threads
Formatting the String
How to handle this Date format?
Date printed in IST format when EDT format required
Date Formatting
need 2 rows rather than 5 from this query