Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3337930
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:18:46+00:00 2026-05-18T00:18:46+00:00

Is there a nice and easy way to convert a Java Date into XML

  • 0

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

Cheers,

Andez

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T00:18:46+00:00Added an answer on May 18, 2026 at 12:18 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The SWT-Gui looks very nice. Is there an easy way to use it in
Is there a nice way to split a collection into n parts with LINQ?
What I'd like to know is if there is a nice way to queue
Is there a quick and nice way using linq?
Hi are there any nice videos on how to use exceptions in Delphi.
Hi are there any nice videos or other resources on how to use Interfaces
There are some nice free image to ASCII art conversion sites like this one:
i'm wondering if there is any nice and neat tool to replace the GNU
On Windows NTFS there is a nice but mostly unused feature called Alternate Data
In PHP 5.2 there was a nice security function added called input_filter, so instead

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.