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 8890393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:29:30+00:00 2026-06-14T22:29:30+00:00

When JAXB marshals a date object ( XMLGregorianCalendar ) into an xsd:dateTime element. How

  • 0

When JAXB marshals a date object (XMLGregorianCalendar) into an xsd:dateTime element. How can you specify the format of the resulting XML?

For example:
The default data format uses milliseconds <StartDate>2012-08-21T13:21:58.000Z</StartDate>
I need to omit the milliseconds. <StartDate>2012-08-21T13:21:58Z</StartDate>

How can I specify the output form/date format that I want it to use?
I’m using javax.xml.datatype.DatatypeFactory to create the XMLGregorianCalendar object.

XMLGregorianCalendar xmlCal = datatypeFactory.newXMLGregorianCalendar(cal);
  • 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-06-14T22:29:31+00:00Added an answer on June 14, 2026 at 10:29 pm

    You can use an XmlAdapter to customize how a date type is written to XML.

    package com.example;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import javax.xml.bind.annotation.adapters.XmlAdapter;
    
    public class DateAdapter extends XmlAdapter<String, Date> {
    
        private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
        @Override
        public String marshal(Date v) throws Exception {
            synchronized (dateFormat) {
                return dateFormat.format(v);
            }
        }
    
        @Override
        public Date unmarshal(String v) throws Exception {
            synchronized (dateFormat) {
                return dateFormat.parse(v);
            }
        }
    
    }
    

    Then you use the @XmlJavaTypeAdapter annotation to specify that the XmlAdapter should be used for a specific field/property.

    @XmlElement(name = "timestamp", required = true) 
    @XmlJavaTypeAdapter(DateAdapter.class)
    protected Date timestamp; 
    

    Using a xjb binding file:

    <xjc:javaType name="java.util.Date" xmlType="xs:dateTime"
            adapter="com.example.DateAdapter"/>
    

    will produce the above mentioned annotation.
    (By eventually adding the xjc namespace: xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc")

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

Sidebar

Related Questions

When I marshal java object using JAXB I am getting below xml element <error
I need to Marshal a JAXB object to xml format string. I'm using a
I am trying to marshall data within an object into an xml file based
I have to generate a xml element that can have as value any primitive
I have a XML format that I want to marshal using JAXB and it
I would like to unmarshal an XML file with this xsd definition using jaxb.
I am using JAXB XMLadapter to marshal and unmarshal Boolean values. The application's XML
It seems JAXB can't read what it writes. Consider the following code: interface IFoo
How can I get jaxb to bind to my Vector? I cannot seem to
I am using JAXB 2.0 JDK 6 in order to unmarshall an XML instance

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.