This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Handling a strange date format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Handling a strange date format" Watch "Handling a strange date format" New topic
Author

Handling a strange date format

M Bryan
Ranch Hand

Joined: Jun 15, 2011
Posts: 64
Hey Folks,

i want to talk with a small gadget via SOAP. Getting the communication done was quite a heavy task, but now i have to handle with a strange date format, which looks like this: 2011-09-06T14:30:00.050+02:00
When getting this format in an answer, i remove the T and the third : and parse this format in a date.
The first problem lies when trying to get this format back into the gadgets format: Since date handles timezones, the time is set back to 12:30. I would have to get a String of the date, change the time, add the timezone information add the end...
The second problem is where i really struggle: In an answer i have a timestamp in the given format. Now i want to get an entry after this entry, that means, one millisecond later. How can i add one millisecond in this format?

Thank you for help!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Have you confirmed that whatever you're sending that string to actually cares whether you format your date as "12:30 ... +00:00" instead of "14.30 ... +02:00"? After all both strings represent the same instant of time.
M Bryan
Ranch Hand

Joined: Jun 15, 2011
Posts: 64
Yes, it seems logical. But when debugging i found out:
The first entry i get is at 14:30:00.000+02:00, i managed to change the time to 12:30:01, but the timezone information seems to gets lost or does not get considered, because the next entry i get is the same as before.
So i would like to convert the String i get to date, add one millisecond and then convert it back in the exact same format:
2011-09-06T14:30:00.000+02:00 -> converting to date (12:30 +0000) -> 2011-09-06T14:30:00.001+02:00
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

So does that mean that your target software cares, or not?

Or does it mean you haven't got so far as to test that because you are floundering in a swamp of confusion?

I suspect it's the latter because I'm confused by what you posted there. Could you post an actual example? With the code that produces the results which are confusing you?
M Bryan
Ranch Hand

Joined: Jun 15, 2011
Posts: 64
You're right, i'm kinda confused and i am not sure if the timezone information is used. But i want to show what i want:



This code changes the given date to 2011-09-06T14:30:00.000+0:00.
As you can the, the timezone information is gone. How can i format the string to date and back to string and get the exact same string?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I don't know. Since I very much suspect you don't really need the exact same string, just a similar string which represents the same timestamp, that's why I asked whether you really needed the exact same string.

So I suggest you find out whether you really have a problem before you spend a lot of time trying to solve something which isn't a problem.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16


That looks like the ISO 8601 date format, which is the standard format for dates in XML files.

Unfortunately, SimpleDateFormat has limitations so that it cannot handle this format very well. In fact, the date and time APIs in standard Java are not very good at all. A very popular library to work with dates and times, and which does handle ISO 8601 format properly, is Joda Time.

Fortunately, standard Java does have some classes and methods to handle XML date representations, such as class XMLGregorianCalendar:

Lookup class XMLGregorianCalendar in the API documentation.

Another point:

Note that class java.util.Date does not know anything about timezones. Class Date just represents an "absolute" moment in time. When you want the content of a Date to be printed with respect to a particular timezone, you have to set the timezone on the DateFormat object that you're using to format it:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
M Bryan
Ranch Hand

Joined: Jun 15, 2011
Posts: 64
Thank you for this hint. But how do i add one millisecond to a XMLGregorianCalendar object?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

I think like this (not tested):

See the API documentation of the classes XMLGregorianCalendar, Duration and DatatypeFactory. They are in the package javax.xml.datatype.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Handling a strange date format
 
Similar Threads
f:convertDateTime and data type conversion
JTable cell to show time only and change based on time entered (only time, disregard date)
Having trouble using Dates in SQL
Coverting from String to Date.
Date Formatting