Is there a nice and easy way to convert a Java Date into XML date string format and vice versa?
Cheers,
Andez
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Original answer
I am guessing here that by “XML Date Format” you mean something like “2010-11-04T19:14Z”. It is actually ISO 8601 format.
You can convert it using SimpleDateFormat, as others suggested, FastDateFormat or using Joda Time which was I believe especially created for this purpose.
Edit: code samples and more
As earnshae stated in a comment, this answer could be improved with examples.
First, we have to make clear that the original answer is pretty outdated. It’s because Java 8 introduced the classes to manipulate date and time –
java.timepackage should be of interest. If you are lucky enough to be using Java 8, you should use one of them. However, these things are surprisingly difficult to get right.LocalDate(Time) that isn’t
Consider this example:
At first it may seem that what we’re using here is a local (to the user date and time). However, if you dare to ask, you’ll get different result:
This actually, the ISO time. I believe it should read ‘UTC’ but nonetheless this has no notion of local time zone. So we should consider it universal.
Please notice, that there is no “Z” at the end of the string we are parsing. Should you add anything apart of date and time, you’ll be greeted with
java.time.format.DateTimeParseException. So it seems that this class is of no use if we want to parse ISO8601 string.ZonedDateTime to the rescue
Fortunately, there is a class that allows for parsing ISO8601 strings – it’s a java.time.ZonedDateTime.
The only problem here is, you actually need to use time zone designation. Trying to parse raw date time (i.e. “2016-03-23T18:21”) will result in already mentioned
RuntimeException. Depending on the situation you’d have to choose betweenLocalDateTimeandZonedDateTime.Of course you can easily convert between those two, so it should not be a problem:
I recommend using this classes nowadays. However, if your job description includes archeology (meaning you are not lucky enough to be working with more than 2 year old Java 8…), you may need to use something else.
The joy of SimpleDateFormat
I am not a very big fan of https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html, but sometimes you just have no other choice. Problem is, it is not thread-safe and it will throw a checked
Exception(namelyParseException) in your face if it dislikes something. Therefore the code snippet is rather ugly:FastDateFormat
FastDateFormatis synchronized, therefore you can at least get rid of the synchronized block. However, it is an external dependency. But since it’s the Apache Commons Lang and it is thoroughly used, I guess it is acceptable. It is actually very similar in usage toSimpleDateFormat:JodaTime
With Joda-Time you may think that following works:
Unfortunately, no matter what you put at last position (Z, +03:00, …) the result will be the same. Clearly, it isn’t working.
Well, you really should be parsing it directly:
Now it will be OK. Please note, that unlike one of other answers, I used
dateTimeParser()and notdateTime(). I noticed subtle, but important difference in behavior between them (Joda-Time 2.9.2). But, I leave it to the reader to test it and confirm.