Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Working with dates, timezones, zulu

 
Ranch Hand
Posts: 285
  • 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?
 
Ranch Hand
Posts: 1780
  • 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
  • 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
  • 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
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
right.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • 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?
 
Nothing up my sleeve ... and ... presto! A tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic