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?
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.
posted 9 years ago
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
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?
posted 9 years ago
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?
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.
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.
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: