KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > value > CalendarValue


1 package net.sf.saxon.value;
2 import net.sf.saxon.ConversionContext;
3 import net.sf.saxon.trans.XPathException;
4
5 import java.util.GregorianCalendar JavaDoc;
6
7
8
9 /**
10 * Abstract superclass for Date, Time, and DateTime
11 */

12
13 public abstract class CalendarValue extends AtomicValue implements Comparable JavaDoc {
14
15     // calendar and zoneSpecified together represent the value space
16
protected GregorianCalendar JavaDoc calendar;
17     protected boolean zoneSpecified;
18
19     public abstract CalendarValue add(DurationValue duration) throws XPathException;
20
21     /**
22      * Determine the difference between two points in time, as a duration
23      * @param other the other point in time
24      * @param context the dynamic context, used to obtain timezone information. May be set to null
25      * only if both values contain an explicit timezone, or if neither does so.
26      * @return the duration as an xdt:dayTimeDuration
27      * @throws net.sf.saxon.trans.XPathException for example if one value is a date and the other is a time
28      */

29
30     public SecondsDurationValue subtract(CalendarValue other, ConversionContext context) throws XPathException {
31         CalendarValue v1 = this;
32         CalendarValue v2 = other;
33         if (v1.zoneSpecified != v2.zoneSpecified) {
34             SecondsDurationValue tz = SecondsDurationValue.fromMilliseconds(context.getImplicitTimezone() * 60000);
35             if (!v1.zoneSpecified) {
36                 v1 = v1.setTimezone(tz);
37             }
38             if (!v2.zoneSpecified) {
39                 v2 = v2.setTimezone(tz);
40             }
41         }
42         long t1 = v1.calendar.getTimeInMillis();
43         long t2 = v2.calendar.getTimeInMillis();
44         long diff = (t1 - t2);
45         return SecondsDurationValue.fromMilliseconds(diff);
46     }
47
48     /**
49      * Return a date, time, or dateTime with the same localized value, but
50      * without the timezone component
51      * @return the result of removing the timezone
52      * @throws XPathException
53      */

54
55     public abstract CalendarValue removeTimezone() throws XPathException;
56
57     /**
58      * Return a date, time, or dateTime with the same normalized value, but
59      * in a different timezone
60      * @return the date/time in the new timezone
61      * @throws XPathException
62      */

63
64     public abstract CalendarValue setTimezone(SecondsDurationValue tz) throws XPathException;
65
66     /**
67      * Compare this value to another value of the same type, using the supplied ConversionContext
68      * to get the implicit timezone if required.
69      */

70
71     public abstract int compareTo(CalendarValue other, ConversionContext conversion);
72 }
73
74 //
75
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
76
// you may not use this file except in compliance with the License. You may obtain a copy of the
77
// License at http://www.mozilla.org/MPL/
78
//
79
// Software distributed under the License is distributed on an "AS IS" basis,
80
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
81
// See the License for the specific language governing rights and limitations under the License.
82
//
83
// The Original Code is: all this file.
84
//
85
// The Initial Developer of the Original Code is Michael H. Kay
86
//
87
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
88
//
89
// Contributor(s): none.
90
//
91

92
Popular Tags