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

  • SEARCH
  • Home
  • 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 7184489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:12:45+00:00 2026-05-28T18:12:45+00:00

I have some dates I have to represent in an XML file in a

  • 0

I have some dates I have to represent in an XML file in a format different than the one JAXB uses by default.

So, I’ve written some static methods to print and parse the required format:

public static String toDateTime(XMLGregorianCalendar d) {
    return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        .format(d.toGregorianCalendar().getTime());
}

public static XMLGregorianCalendar parseDateTime(String s)
        throws DatatypeConfigurationException, ParseException {

    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                .parse(s));
    return DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);
}

And I’ve written a .xjb file specifying to use these parse and print methods:

<jxb:bindings
    node="//xs:element[@name='date']">
  <jxb:javaType name="javax.xml.datatype.XMLGregorianCalendar"
                parseMethod="MyClass.parseDateTime"
                printMethod="MyClass.toDateTime"
                />
</jxb:bindings>

(The XML schema specifies the element as an xsd:dateTime).

The xjc tool is generating an adapter that calls my methods:

public XMLGregorianCalendar unmarshal(String value) {
    return (MyClass.parseDateTime(value));
}

public String marshal(XMLGregorianCalendar value) {
    return (MyClass.toDateTime(value));
}

My problem is that the generated adapter won’t compile. The unmarshal method is declared not to throw exceptions, but MyClass.parseDateTime does. It seems perfectly reasonable that it should, since the date might be invalid. And indeed, the unmarshal method of java.xml.bind.annotation.adapters.XmlAdapter that the generated adapter overrides declares that it throws Exception.

I couldn’t find any way to declare in the xjb file which exceptions the parseMethod throws. It occurred to me that maybe I should create my own adapter class, but I couldn’t find any way to declare in the xjb that my custom adapter should be used. I would very much like to be able to generate the classes from the xsd and xjb with xjc and not have to modify them afterwards.

What is the best way to resolve this situation?

  • 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-28T18:12:46+00:00Added an answer on May 28, 2026 at 6:12 pm

    You should handle both checked exceptions in your code. For an invalid date-time, you have to decide whether to leave it as null (and maybe emit a warning), or blow up and kill the whole parsing attempt (in that case, throw a RuntimeException wrapping the original ParseException).

    In the case of DatatypeConfigurationException, this is one of the JDK APIs that is badly designed, throwing checked exceptions when there’s nothing your code can do. Just rethrow it as RuntimeException.

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

Sidebar

Related Questions

I have some Ruby code which takes dates on the command line in the
I would like to compare two dates in javascript. I have been doing some
I have some code that uses the Oracle function add_months to increment a Date
I have two large source trees. One of them has some out of date
I have a db, that stores dates in OleDateTime format, in GMT timezone. I've
I have an array of dates whose format is like this: 10/15/2005 or 2/10/2011.
I have class than spins off a backgroundworker to do some processor intensive stuff.
I have a Delphi form with some DB controls on it. To represent a
Given some short integers and the dates they represent, is there any systematic method
I have to use some dates and times from a legacy database. They are

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.