2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
20
import javax.xml.datatype.DatatypeConfigurationException;
21
import javax.xml.datatype.DatatypeConstants;
22
import javax.xml.datatype.DatatypeFactory;
23
import javax.xml.datatype.Duration;
24
import javax.xml.datatype.XMLGregorianCalendar;
27
* <p>A sample which demonstrates usage of the JAXP 1.3 Datatype API.</p>
29
* @version $Id: DatatypeAPIUsage.java,v 1.2 2009/12/10 03:18:46 matthewoliver Exp $
31
public class DatatypeAPIUsage {
33
public static void main (String [] args) {
35
DatatypeFactory df = DatatypeFactory.newInstance();
36
// my work number in milliseconds:
37
Duration myPhone = df.newDuration(9054133519l);
38
Duration myLife = df.newDuration(true, 29, 2, 15, 13, 45, 0);
39
int compareVal = myPhone.compare(myLife);
41
case DatatypeConstants.LESSER:
42
System.out.println("There are fewer milliseconds in my phone number than my lifespan.");
44
case DatatypeConstants.EQUAL:
45
System.out.println("The same number of milliseconds are in my phone number and my lifespan.");
47
case DatatypeConstants.GREATER:
48
System.out.println("There are more milliseconds in my phone number than my lifespan.");
50
case DatatypeConstants.INDETERMINATE:
51
System.out.println("The comparison could not be carried out.");
54
// create a yearMonthDuration
55
Duration ymDuration = df.newDurationYearMonth("P12Y10M");
56
System.out.println("P12Y10M is of type: " + ymDuration.getXMLSchemaType());
58
// create a dayTimeDuration (really this time)
59
Duration dtDuration = df.newDurationDayTime("P10DT10H12M0S");
60
System.out.println("P10DT10H12M0S is of type: " + dtDuration.getXMLSchemaType());
62
// try to fool the factory!
64
ymDuration = df.newDurationYearMonth("P12Y10M1D");
66
catch(IllegalArgumentException e) {
67
System.out.println("'duration': P12Y10M1D is not 'yearMonthDuration'!!!");
70
XMLGregorianCalendar xgc = df.newXMLGregorianCalendar();
72
xgc.setMonth(DatatypeConstants.AUGUST);
77
xgc.setMillisecond(0);
80
System.out.println("The approximate end of the number of milliseconds in my phone number was " + xgc);
82
//adding a duration to XMLGregorianCalendar
84
System.out.println("Adding the duration myLife to the above calendar:" + xgc);
86
// create a new XMLGregorianCalendar using the string format of xgc.
87
XMLGregorianCalendar xgcCopy = df.newXMLGregorianCalendar(xgc.toXMLFormat());
89
// should be equal-if not what happened!!
90
if (xgcCopy.compare(xgc) != DatatypeConstants.EQUAL) {
91
System.out.println("oooops!");
94
System.out.println("Very good: " + xgc + " is equal to " + xgcCopy);
97
catch (DatatypeConfigurationException dce) {
98
System.err.println("error: Datatype error occurred - " + dce.getMessage());
99
dce.printStackTrace(System.err);
103
} // DatatypeAPIUsage