Aller au contenu | Aller au menu | Aller à la recherche


XMLGregorianCalendar to Calendar to Date

Comment convertir une Date en Calendar, GregorianCalendar et XMLGregorianCalendar ?

XMLGregorianCalendar semble être une norme pour les services web.

javax.xml.datatype.XMLGregorianCalendar (jre6)
javax.xml.datatype.XMLGregorianCalendar (jre7)

Voici comment comment convertir une Date en (Gregorian)Calendar en XMLGregorianCalendar, et dans le sens inverse.

Alternativement vous avez aussi com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl qui étends XMLGregorianCalendar, tel que l'on peut le voir dans les sources.

Calendar calendar = Calendar.getInstance();
		calendar.set(2011, 12, 22, 23, 59, 30);
		System.out.println("Calendar:");
		System.out.println(calendar);
 
		XMLGregorianCalendar xmlGregorianCalendar = null;
 
		GregorianCalendar gregorianCalendar = new GregorianCalendar();
		gregorianCalendar.setTime(calendar.getTime());
		System.out.println("GregorianCalendar:");
		System.out.println(gregorianCalendar);
 
		Date date = calendar.getTime();
		System.out.println("Date:");
		System.out.println(date);
 
		try {
			xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(gregorianCalendar);
 
			System.out.println("XMLGregorianCalendarImpl (DatatypeFactory.newInstance()):");
			System.out.println(xmlGregorianCalendar);
		} catch (DatatypeConfigurationException e) {
			e.printStackTrace();
		}
 
		XMLGregorianCalendarImpl xmlGregorianCalendarImpl = new XMLGregorianCalendarImpl((GregorianCalendar) calendar);
		System.out.println("XMLGregorianCalendarImpl (cast) Calendar or GregorianCalendar:");
		System.out.println(xmlGregorianCalendarImpl);
 
		System.out.println("XMLGregorianCalendar.to GregorianCalendar+Date/String/XMLFormat");
		System.out.println(xmlGregorianCalendar.toGregorianCalendar());
		System.out.println(xmlGregorianCalendar.toGregorianCalendar().getTime());		
		System.out.println(xmlGregorianCalendar.toString());
		System.out.println(xmlGregorianCalendar.toXMLFormat());
 
		System.out.println("xmlGregorianCalendarImpl GregorianCalendar+Date/String/XMLFormat");
		System.out.println(xmlGregorianCalendarImpl.toGregorianCalendar());
		System.out.println(xmlGregorianCalendarImpl.toGregorianCalendar().getTime());
		System.out.println(xmlGregorianCalendarImpl.toString());
		System.out.println(xmlGregorianCalendarImpl.toXMLFormat());
Calendar:
java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Europe/Paris",offset=3600000,dstSavings=3600000,useDaylight=true,transitions=184,lastRule=java.util.SimpleTimeZone[id=Europe/Paris,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2011,MONTH=12,WEEK_OF_YEAR=11,WEEK_OF_MONTH=3,DAY_OF_MONTH=22,DAY_OF_YEAR=77,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=3,AM_PM=1,HOUR=5,HOUR_OF_DAY=23,MINUTE=59,SECOND=30,MILLISECOND=578,ZONE_OFFSET=3600000,DST_OFFSET=0]
GregorianCalendar:
java.util.GregorianCalendar[time=1327273170578,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Europe/Paris",offset=3600000,dstSavings=3600000,useDaylight=true,transitions=184,lastRule=java.util.SimpleTimeZone[id=Europe/Paris,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2012,MONTH=0,WEEK_OF_YEAR=3,WEEK_OF_MONTH=3,DAY_OF_MONTH=22,DAY_OF_YEAR=22,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=11,HOUR_OF_DAY=23,MINUTE=59,SECOND=30,MILLISECOND=578,ZONE_OFFSET=3600000,DST_OFFSET=0]
Date:
Sun Jan 22 23:59:30 CET 2012
XMLGregorianCalendarImpl (DatatypeFactory.newInstance()):
2012-01-22T23:59:30.578+01:00
XMLGregorianCalendarImpl (cast) Calendar or GregorianCalendar:
2012-01-22T23:59:30.578+01:00
XMLGregorianCalendar.to GregorianCalendar+Date/String/XMLFormat
java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT+01:00",offset=3600000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2012,MONTH=0,WEEK_OF_YEAR=1,WEEK_OF_MONTH=1,DAY_OF_MONTH=22,DAY_OF_YEAR=1,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=0,HOUR_OF_DAY=23,MINUTE=59,SECOND=30,MILLISECOND=578,ZONE_OFFSET=3600000,DST_OFFSET=0]
Sun Jan 22 23:59:30 CET 2012
2012-01-22T23:59:30.578+01:00
2012-01-22T23:59:30.578+01:00
xmlGregorianCalendarImpl GregorianCalendar+Date/String/XMLFormat
java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT+01:00",offset=3600000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2012,MONTH=0,WEEK_OF_YEAR=1,WEEK_OF_MONTH=1,DAY_OF_MONTH=22,DAY_OF_YEAR=1,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=0,HOUR_OF_DAY=23,MINUTE=59,SECOND=30,MILLISECOND=578,ZONE_OFFSET=3600000,DST_OFFSET=0]
Sun Jan 22 23:59:30 CET 2012
2012-01-22T23:59:30.578+01:00
2012-01-22T23:59:30.578+01:00

Voyez aussi le résumé simplifié : Utilisation du type XMLGregorianCalendar

Ajouter un commentaire

Le code HTML est affiché comme du texte et les adresses web sont automatiquement transformées.

Fil des commentaires de ce billet