Solved

How do I convert a Java date/time into UTC coordinates ?

Posted on 2008-10-06
11
8,620 Views
Last Modified: 2013-11-23
How do I convert a Java date/time into UTC coordinates in this format: 2008-09-24T15:57:57.2Z
0
Comment
Question by:cindymccartney
  • 5
  • 5
11 Comments
 
LVL 6

Assisted Solution

by:wwnosal
wwnosal earned 200 total points
ID: 22654541
You can do the following trick ;-)
Calendar c = Calendar.getInstance();   

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    SimpleDateFormat stf = new SimpleDateFormat("HH:mm:ss");

    double offset  = c.getTimeZone ( ).getRawOffset ( )/(1000*60*60);

    DecimalFormat NF = new DecimalFormat("###.##");    

    System.out.println (sdf.format ( c.getTime ( ) ) +"T"+ stf.format ( c.getTime ()) +"." +NF.format ( offset )  +"Z");

    

Open in new window

0
 

Author Comment

by:cindymccartney
ID: 22663106
I tried your solution, and came out with the following result:  2008-10-07T12:52:52.-8Z

Should I be getting a negative number there?
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22666013
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 300 total points
ID: 22666128
Try this:
Calendar today = Calendar.getInstance();

      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.S'Z'");

      sdf.setTimeZone(TimeZone.getTimeZone("GMT+0"));

      

      System.out.println(sdf.format(today.getTime()));

Open in new window

0
 

Author Comment

by:cindymccartney
ID: 22680600
I am not sure what the value prior to the "Z" is supposed to be here for UTC.  Is it simply the first character of the milli-seconds, or does it mean something else.  From the examples that I am seeing, it changes, so it can't just be a default offset.

mwvisa1, your code displays three value milli-seconds and does not fit the format.

I am still unable to display the following format: 2008-09-24T15:57:57.2Z
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22681107
I was going on fact that you didn't want the offset shown and that you had 15 hours, 57 minutes, 57 seconds, and 2 milliseconds.  If that is meant to be something different, then please explain what you are looking for there.  In the other examples they are using Z in formating which will return the offset to Zulu.  I put in literal 'Z' since you didn't want to see the -04:00 which is what I see on my system being EDT.

If the two is also a literal instead of actual milliseconds just do this:
Calendar today = Calendar.getInstance();

      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.'2Z'");

      sdf.setTimeZone(TimeZone.getTimeZone("GMT+0"));

      

      System.out.println(sdf.format(today.getTime()));

Open in new window

0
 

Author Comment

by:cindymccartney
ID: 22689786
Well, the problem is I really don't know what the value is prior to the "Z".  I was only given a specification without a description.  So I don't know if it's an offset or not.  What I do know is that the value changes.  I have seen examples of a "2Z" as well a "5Z".  Would there ever be a positive offset like that, or would that have to be the first character of the milli-seconds?  I know it can't be a static literal.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22690136
There are positive offsets for timezones that are GMT+1 for Amsterdam or Berlin.  However, this is usually noted like this:

2008-09-24T15:57:57+0100
OR
2008-09-24T15:57:57-0400

I believe the 2 is your number of milliseconds but they are using the S option versus SSS.  What this means is if you only have 2 milliseconds you will see .2 versus .002; therefore, the number of digits between final . and Z can vary from 1-3.

http:#22666128

I would go with format in linked suggestion versus hardcoded; however, if you want offset there instead just remove the ' from around Z.

yyyy-MM-dd'T'hh:mm:ss.Z
0
 

Author Comment

by:cindymccartney
ID: 22703319
I tried your suggestion in link #22666128,  however that displayed 3 milli-second characters, not just the one.  Your logic seems correct having the single 'S' in the format, but I assume something in the next two lines is overriding the single milli-second and displaying all three:

sdf.setTimeZone(TimeZone.getTimeZone("GMT+0"));
System.out.println(sdf.format(today.getTime()));
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22704727
the S means if the milli-seconds are single digit like say 2 milli-seconds, you will only get 2 instead of 002.  It does NOT mean that if you have 502 milli-seconds that you will only see 5.

If you time it right you can see what I am talking about...I just think the examples you were given were right at beginning of a second and so have single digits; however, I may be wrong.

Just don't want you chasing something for no reason.

Regards,
Kevin
0
 

Author Closing Comment

by:cindymccartney
ID: 31503612
Thanks for your help!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
where is session ID cache stored 1 39
wordappend challenge 8 85
for i loop in grovy 1 32
unix in java example 9 39
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now