Convert objects into XMLGregorianCalendar

The following examples show, how a date, calendar or formatted String can be converted into XMLGregorianCalendar object.

  1. Convert Date into XMLGregorianCalendar
  2. Convert String into XMLGregorianCalendar
  3. Convert Calendar into XMLGregorianCalendar

Convert Date into XMLGregorianCalendar

The first code show the converting from Date into XMLGregorianCalendar.

package it.heber.sandbox;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

/**
 * Converts different objects into XMLGregorianCalendar
 */
public class XMLGregorianCalendarConverter {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws ParseException {

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS z", Locale.GERMAN);

        System.out.printf("%-24s %s%n", "Date:", dateToXMLGregorianCalendar(date));
    }

    /**
     * Converts Date object into XMLGregorianCalendar
     *
     * @param date Object to be converted
     * @return XMLGregorianCalendar
     */
    private static XMLGregorianCalendar dateToXMLGregorianCalendar(Date date) {

        try {
            GregorianCalendar gc = (GregorianCalendar) GregorianCalendar.getInstance();
            gc.setTime(date);
            return DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
        } catch (DatatypeConfigurationException e) {
            // TODO: Optimize exception handling
            System.out.print(e.getMessage());
            return null;
        }
    }
}

The output is following:

Date:                    2012-03-29T23:33:32.264+02:00

Convert String into XMLGregorianCalendar

The second example show the converting from a formatted date String into XMLGregorianCalendar.

package it.heber.sandbox;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

/**
 * Converts different objects into XMLGregorianCalendar
 */
public class XMLGregorianCalendarConverter {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws ParseException {

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS z", Locale.GERMAN);

        String formattedDate = sdf.format(date);

        System.out.printf("%-24s %s%n", "String:", stringToXMLGregorianCalendar(formattedDate, sdf));
    }

    /**
     * Converts Date object into XMLGregorianCalendar
     *
     * @param date Object to be converted
     * @return XMLGregorianCalendar
     */
    private static XMLGregorianCalendar dateToXMLGregorianCalendar(Date date) {

        try {
            GregorianCalendar gc = (GregorianCalendar) GregorianCalendar.getInstance();
            gc.setTime(date);
            return DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
        } catch (DatatypeConfigurationException e) {
            // TODO: Optimize exception handling
            System.out.print(e.getMessage());
            return null;
        }
    }

    /**
     * Converts a formatted string into XMLGregorianCalendar
     *
     * @param datetime Formatted string
     * @param sdf Date format of the given string
     * @return XMLGregorianCalendar
     */
    private static XMLGregorianCalendar stringToXMLGregorianCalendar(String datetime, SimpleDateFormat sdf) {

        try {
            Date date = sdf.parse(datetime);
            return dateToXMLGregorianCalendar(date);
        } catch (ParseException e) {
            // TODO: Optimize exception handling
            System.out.print(e.getMessage());
            return null;
        }
    }
}

The output is following:

String:                  2012-03-29T23:33:32.264+02:00

Convert Calendar into XMLGregorianCalendar

The third example show the converting from a Calendar object into XMLGregorianCalendar.

package it.heber.sandbox;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

/**
 * Converts different objects into XMLGregorianCalendar
 *
 * @author Uwe Heber
 */
public class XMLGregorianCalendarConverter {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws ParseException {

        Date date = new Date();

        Calendar calendar = Calendar.getInstance(Locale.GERMAN);
        calendar.setTime(date);

        System.out.printf("%-24s %s%n", "Calendar:", calendarToXMLGregorianCalendar(calendar));
    }

    /**
     * Converts Calendar object into XMLGregorianCalendar
     *
     * @param calendar Object to be converted
     * @return XMLGregorianCalendar
     */
    private static XMLGregorianCalendar calendarToXMLGregorianCalendar(Calendar calendar) {

        try {
            DatatypeFactory dtf = DatatypeFactory.newInstance();
            XMLGregorianCalendar xgc = dtf.newXMLGregorianCalendar();
            xgc.setYear(calendar.get(Calendar.YEAR));
            xgc.setMonth(calendar.get(Calendar.MONTH) + 1);
            xgc.setDay(calendar.get(Calendar.DAY_OF_MONTH));
            xgc.setHour(calendar.get(Calendar.HOUR_OF_DAY));
            xgc.setMinute(calendar.get(Calendar.MINUTE));
            xgc.setSecond(calendar.get(Calendar.SECOND));
            xgc.setMillisecond(calendar.get(Calendar.MILLISECOND));

            // Calendar ZONE_OFFSET and DST_OFFSET fields are in milliseconds.
            int offsetInMinutes = (calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET)) / (60 * 1000);
            xgc.setTimezone(offsetInMinutes);
            return xgc;
        } catch (DatatypeConfigurationException e) {
            // TODO: Optimize exception handling
            System.out.print(e.getMessage());
            return null;
        }
    }
}

The output is following:

Calendar:                2012-03-29T23:33:32.264+02:00
This entry was posted in Java and tagged , . Bookmark the permalink.

Hinterlasse eine Antwort

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *

Du kannst folgende HTML-Tags benutzen: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>