• 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

Working with dates, timezones, zulu

 
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
right.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
reply
    Bookmark Topic Watch Topic
  • New Topic