Is there a nice and easy way to convert a Java Date into XML date string format and vice versa?
Cheers,
Andez
Can use the following conversion code to convert UTC format string to any other DateTime format. string result = Convert. ToDateTime("2011-02-04T00:00:00+05:30"). ToString("MM/dd/yyyy h:mm:ss tt");
XML Date is defined as a data type to specify the date in the XML Documents. According to W3C XML Schema, a date is a period of one day in its time zone.
String date_s = " 2011-01-18 00:00:00.0"; SimpleDateFormat dt = new SimpleDateFormat("yyyyy-mm-dd hh:mm:ss"); Date date = dt. parse(date_s); SimpleDateFormat dt1 = new SimpleDateFormat("yyyyy-mm-dd"); System. out. println(dt1.
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.
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.time
package 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.
Consider this example:
LocalDateTime dateTime = LocalDateTime.parse("2016-03-23T18:21"); System.out.println(dateTime); // 2016-03-23T18:21
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:
System.out.println(dateTime.getChronology()); // ISO
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.
Fortunately, there is a class that allows for parsing ISO8601 strings - it's a java.time.ZonedDateTime.
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2016-03-23T18:21+01:00"); System.out.println(zonedDateTime); // 2016-03-23T18:21+01:00 ZonedDateTime zonedDateTimeZulu = ZonedDateTime.parse("2016-03-23T18:21Z"); System.out.println(zonedDateTimeZulu); // 2016-03-23T18:21Z
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 between LocalDateTime
and ZonedDateTime
.
Of course you can easily convert between those two, so it should not be a problem:
System.out.println(zonedDateTimeZulu.toLocalDateTime()); // 2016-03-23T18:21 // Zone conversion ZonedDateTime cetDateTime = zonedDateTimeZulu.toLocalDateTime() .atZone(ZoneId.of("CET")); System.out.println(cetDateTime); // 2016-03-23T18:21+01:00[CET]
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.
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
(namely ParseException
) in your face if it dislikes something. Therefore the code snippet is rather ugly:
private Object lock = new Object(); // ... try { synchronized (lock) { // Either "2016-03-23T18:21+01:00" or "2016-03-23T18:21Z" // will be correctly parsed (mind the different meaning though) Date date = dateFormat.parse("2016-03-23T18:21Z"); System.out.println(date); // Wed Mar 23 19:21:00 CET 2016 } } catch (ParseException e) { LOG.error("Date time parsing exception", e); }
FastDateFormat
is 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 to SimpleDateFormat
:
FastDateFormat fastDateFormat = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mmX"); try { Date fastDate = fastDateFormat.parse("2016-03-23T18:21+01:00"); System.out.println(fastDate); } catch (ParseException e) { LOG.error("Date time parsing exception", e); }
With Joda-Time you may think that following works:
DateTimeFormatter parser = ISODateTimeFormat.dateTimeParser(); LocalDateTime dateTime = LocalDateTime.parse("2016-03-23T20:48+01:00", parser); System.out.println(dateTime); // 2016-03-23T20:48:00.000
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:
DateTimeFormatter parser = ISODateTimeFormat.dateTimeParser(); DateTime dateTime = parser.parseDateTime("2016-03-23T21:12:23+04:00"); System.out.println(dateTime); // 2016-03-23T18:12:23.000+01:00
Now it will be OK. Please note, that unlike one of other answers, I used dateTimeParser()
and not dateTime()
. 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With