Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Java Date into XML Date Format (and vice versa)

Tags:

Is there a nice and easy way to convert a Java Date into XML date string format and vice versa?

Cheers,

Andez

like image 512
Andez Avatar asked Nov 04 '10 18:11

Andez


People also ask

How can I change the date format in XML?

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");

What is XML date format?

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.

How do you change date format to MM DD YYYY in java?

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.


1 Answers

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.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.

LocalDate(Time) that isn't

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.

ZonedDateTime to the rescue

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.

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 (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

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); } 

JodaTime

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.

like image 52
Paweł Dyda Avatar answered Oct 07 '22 00:10

Paweł Dyda