1 2 Previous Next 15 Replies Latest reply on May 2, 2010 3:43 PM by 843834

    jaxb , xs:date and java.util.Calendar

    843834
      Hello all,

      My schema has an element of type xs:date, which jaxb maps to a java.util.Calendar. If I create a Calendar object with Calendar.getInstance(), it marshalls to "2003-11-24-05:00".

      How can I get it to marshall to just "2003-11-24"?

      Thanks,
      Brian

        • 1. Re: jaxb , xs:date and java.util.Calendar
          843834
          Hi Brian,

          I have the identical problem within an application deployed both as servlet and java app.

          I have posted a query on another forum, but mo repy as yet.

          I guess the way might be through either
          a) creating a DateFormat object, and passing this to JAXB
          or
          b) Setting some or other System /Registry property that defines the date format mask to use.

          I cannot see any appropriate references in the API, Tutorial or specifications.

          Of course I need different masks for each external server I talk to
          2003-11-24 24/11/2003 11/24/03 ...etc. I have no control over these external formats.

          Chris
          • 2. Re: jaxb , xs:date and java.util.Calendar
            DrClap
            I am guessing you are in a timezone that is 5 hours away from GMT. Why is it a problem that the data is marshalled that way?
            • 3. Re: jaxb , xs:date and java.util.Calendar
              843834
              If I create a Calendar object by unmarshalling in an xml file that contains a date element "2003-11-24", and then marshall the java object right back out without calling any set methods on it, the text comes back as "2003-11-24", the format I want.

              This leads me to think my problem may in the the value of Calendar, not the formatting of it. I'm not sure however.

              Brian
              • 4. Re: jaxb , xs:date and java.util.Calendar
                843834
                Hi DrClap

                I have an external requirement for data formatted that way.

                The XML parser of my external client expects a date to be e.g. EXACTLY 10 characters yyyy-MM-dd. I have no control over this requirement.

                Chris
                • 5. Re: jaxb , xs:date and java.util.Calendar
                  843834
                  Yes, I am 5 hrs from GMT. Including the add'l time zone stuff isn't a problem but it is not applicable to our application. Keeping it in may confuse so we'd rather not have it there.

                  If anyone knows how to adjust a Calendar object back so that it is spot-on the GM (so the marshalling omits the time zone part?) please advise.

                  Brian
                  • 6. Re: jaxb , xs:date and java.util.Calendar
                    DrClap
                    Have you tried using Calendar's setTimeZone() method? Try setting it to GMT before you set the hours, minutes, etc to zero.
                    • 7. Re: jaxb , xs:date and java.util.Calendar
                      843834
                      Thanks for your suggestion

                      calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
                      calendar.set(Calendar.HOUR,0);
                      calendar.set(Calendar.MINUTE,0);
                      calendar.set(Calendar.SECOND,0);

                      Doing so seems to set the time back to GMT but unfortunately the marshalling is giving unwanted zeroes in the format.

                      "2003-11-24+00:00" (not "2003-11-24")

                      Brian


                      • 8. Re: jaxb , xs:date and java.util.Calendar
                        843834
                        Just a question, are they using the same XSD as you? Because you can just read in the date as a calendar and convert it to a string in your format, and have it marshalled as a String.

                        -G
                        • 9. Re: jaxb , xs:date and java.util.Calendar
                          843834
                          Hi Brian / All,

                          Have you had any resolution on this problem?

                          Altering the xsd to a String type is really a kludge, and I found it easier to marshall to a String, then do a replaceAll to gett rid of the unwanted +02:00.

                          I still feel this is a Format issue and not a Calendar issue (the date and time remain the same in UTC, its the textual representation that differs.

                          Chris
                          • 10. Re: jaxb , xs:date and java.util.Calendar
                            843834
                            Hi Chris,

                            I came up with a solution, although this problem has to be so common I doubt this solution is the "correct" or best way, but it works.

                            I wrote a converter class (see MyConverter below) and added an annotation/appinfo to the xml schema, also shown below.

                            Brian

                            public class MyConverter{
                            static final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                            static public String printCalendar(Calendar c){
                            return df.format(c.getTime());
                            }

                            static public Calendar parseCalendar(String c) throws ParseExceptio{
                            Date d = df.parse(c);
                            Calendar cal = Calendar.getInstance();
                            cal.setTime(d);
                            return cal;
                            }
                            }

                            <xsd:schema ...>
                            <xsd:annotation>
                            <xsd:appinfo>
                            <jaxb:globalBindings
                            <jaxb:javaType name="java.util.Calendar" xmlType="xsd:date"
                            printMethod="MyConverter.printCalendar"
                            parseMethod="MyConverter.parseCalendar"
                            />
                            </jaxb:globalBindings>
                            </xsd:appinfo>
                            </xsd:annotation>
                            ...
                            </xsd:schema>
                            • 11. Re: jaxb , xs:date and java.util.Calendar
                              843834
                              Hi Briank,

                              And thanks for your answer....

                              But can you tell me where you put your file (directory of MyConverter.java for example).
                              Thanks in advance.
                              • 12. Re: jaxb , xs:date and java.util.Calendar
                                843834
                                Thanks for the post on customizations. The whole date things has been messing with me for days.

                                I had a similar problem, but I needed to strip off the fractional seconds and timezone from a dateTime.

                                I've implemented the above code and here's how to use it:

                                The DateTimeFormatter (just change the formatter to whatever you need it to be)
                                package my.package;
                                
                                import java.text.ParseException;
                                import java.text.SimpleDateFormat;
                                
                                import java.util.Calendar;
                                import java.util.Date;
                                
                                public class DateTimeFormatter {
                                  static final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
                                
                                  static public String printCalendar(Calendar c) {
                                    return df.format(c.getTime());
                                  }
                                
                                  static public Calendar parseCalendar(String c) throws ParseException {
                                    Date d = df.parse(c);
                                    Calendar cal = Calendar.getInstance();
                                    cal.setTime(d);
                                
                                    return cal;
                                  }
                                }
                                As long that class in in your class path during binding and marhsalling/unmarshalling/validating, you're good to go.

                                In the xsd, you need the following in your xs:schema element:
                                <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="1.0">
                                I've got multiple schema files, so I just put my global bindings in one file and include it in all others.
                                <xs:annotation>
                                  <xs:appinfo>
                                    <jaxb:globalBindings>
                                      <jaxb:javaType name="java.util.Calendar" xmlType="xs:dateTime" printMethod="my.package.DateTimeFormatter.printCalendar" parseMethod="my.package.DateTimeFormatter.parseCalendar"/>
                                    </jaxb:globalBindings>
                                  </xs:appinfo>
                                </xs:annotation>
                                I'm much happier now.
                                • 13. Re: jaxb , xs:date and java.util.Calendar
                                  843834
                                  I had this same problem, but it was only a problem because the third party I was exchanging data with (who was using .Net) didn't realize that 1980-08-08-04:00 is a valid xsd:date.

                                  Here's a link to the spec: http://www.w3.org/TR/xmlschema-2/#date

                                  -Gene
                                  • 14. Re: jaxb , xs:date and java.util.Calendar
                                    843834
                                    I know this is an old thread, but I found it when I was searching for the solution. I tried using the xsd annotations, but that didn't work for me.

                                    I followed the directions in this article and it worked like a champ.

                                    http://www.devx.com/Java/Article/34069/0/page/3

                                    Personally I had to do some manipulation on the outbound to change XMLGregorianCalendar into the format the client needed.
                                    public class XMLGregorianCalendarAdapter extends XmlAdapter<String, XMLGregorianCalendar> {
                                    
                                         DateFormat df = new SimpleDateFormat("yyyy-dd-MMZ");
                                    
                                         public XMLGregorianCalendar unmarshal(String date) throws Exception {
                                              
                                              Date utilDate = df.parse(date);
                                              GregorianCalendar cal = new GregorianCalendar();
                                              cal.setTime(utilDate);
                                              
                                              DatatypeFactory dataType = DatatypeFactoryImpl.newInstance();
                                              XMLGregorianCalendar retXmlCal = dataType.newXMLGregorianCalendar(cal);
                                              
                                              return retXmlCal;
                                         }
                                    
                                         public String marshal(XMLGregorianCalendar date) throws Exception {
                                              return df.format(new Timestamp(date.toGregorianCalendar().getTimeInMillis()));
                                         }
                                    }
                                    and then referenced in my XML class

                                    @XmlElement(required = true)
                                    @XmlJavaTypeAdapter(XMLGregorianCalendarAdapter.class) 
                                    protected XMLGregorianCalendar orderedDate;
                                    1 2 Previous Next