1 16 17 package jaxp; 18 19 import javax.xml.datatype.DatatypeConfigurationException ; 20 import javax.xml.datatype.DatatypeConstants ; 21 import javax.xml.datatype.DatatypeFactory ; 22 import javax.xml.datatype.Duration ; 23 import javax.xml.datatype.XMLGregorianCalendar ; 24 25 30 public class DatatypeAPIUsage { 31 32 public static void main (String [] args) { 33 try { 34 DatatypeFactory df = DatatypeFactory.newInstance(); 35 Duration myPhone = df.newDuration(9054133519l); 37 Duration myLife = df.newDuration(true, 29, 2, 15, 13, 45, 0); 38 int compareVal = myPhone.compare(myLife); 39 switch (compareVal) { 40 case DatatypeConstants.LESSER: 41 System.out.println("There are fewer milliseconds in my phone number than my lifespan."); 42 break; 43 case DatatypeConstants.EQUAL: 44 System.out.println("The same number of milliseconds are in my phone number and my lifespan."); 45 break; 46 case DatatypeConstants.GREATER: 47 System.out.println("There are more milliseconds in my phone number than my lifespan."); 48 break; 49 case DatatypeConstants.INDETERMINATE: 50 System.out.println("The comparison could not be carried out."); 51 } 52 53 Duration ymDuration = df.newDurationYearMonth("P12Y10M"); 55 System.out.println("P12Y10M is of type: " + ymDuration.getXMLSchemaType()); 56 57 Duration dtDuration = df.newDurationDayTime("P10DT10H12M0S"); 59 System.out.println("P10DT10H12M0S is of type: " + dtDuration.getXMLSchemaType()); 60 61 try { 63 ymDuration = df.newDurationYearMonth("P12Y10M1D"); 64 } 65 catch(IllegalArgumentException e) { 66 System.out.println("'duration': P12Y10M1D is not 'yearMonthDuration'!!!"); 67 } 68 69 XMLGregorianCalendar xgc = df.newXMLGregorianCalendar(); 70 xgc.setYear(1975); 71 xgc.setMonth(DatatypeConstants.AUGUST); 72 xgc.setDay(11); 73 xgc.setHour(6); 74 xgc.setMinute(44); 75 xgc.setSecond(0); 76 xgc.setMillisecond(0); 77 xgc.setTimezone(5); 78 xgc.add(myPhone); 79 System.out.println("The approximate end of the number of milliseconds in my phone number was " + xgc); 80 81 xgc.add(myLife); 83 System.out.println("Adding the duration myLife to the above calendar:" + xgc); 84 85 XMLGregorianCalendar xgcCopy = df.newXMLGregorianCalendar(xgc.toXMLFormat()); 87 88 if (xgcCopy.compare(xgc) != DatatypeConstants.EQUAL) { 90 System.out.println("oooops!"); 91 } 92 else { 93 System.out.println("Very good: " + xgc + " is equal to " + xgcCopy); 94 } 95 } 96 catch (DatatypeConfigurationException dce) { 97 System.err.println("error: Datatype error occurred - " + dce.getMessage()); 98 dce.printStackTrace(System.err); 99 } 100 } 102 } | Popular Tags |