1 22 23 package org.xquark.schema.datatypes; 24 25 import java.util.Date ; 26 27 32 public class DateTime extends Date { 33 34 public static final int MAX_TIMEZONE_OFFSET = 14 * 60 * 60 * 1000; 35 38 private long tz = 0; 39 42 private boolean tzSpecified = false; 43 44 47 public DateTime(long millis) { 48 this(millis, 0, false); 49 } 50 51 56 public DateTime(long millis, long tz) { 57 this(millis, tz, true); 58 } 59 60 66 public DateTime(long millis, long tz, boolean tzSpecified) { 67 super(millis-tz); 68 this.tz = tz; 69 this.tzSpecified = tzSpecified; 70 } 71 72 public boolean equals(Object obj) { 73 if (this == obj) return true; 74 if (!checkClass(obj)) return false; 75 return this.tzSpecified == ((DateTime)obj).tzSpecified && this.getTime() == ((DateTime)obj).getTime(); 76 } 77 78 protected boolean checkClass(Object obj) { 79 return obj instanceof DateTime; 80 } 81 82 public boolean hasTimeZone() { 83 return tzSpecified; 84 } 85 86 public long getTimeZone() { 87 return tz; 88 } 89 99 public int compareTo(Object obj) throws ClassCastException , IllegalArgumentException { 100 return compareTo((Date ) obj); 101 } 102 103 public int compareTo(Date other) throws IllegalArgumentException { 104 long min1, min2, max1, max2; 105 min1 = max1 = getTime(); 106 min2 = max2 = other.getTime(); 107 boolean tz1 = tzSpecified; 108 boolean tz2 = (other instanceof DateTime) && ((DateTime)other).tzSpecified; 109 if (tz1 == tz2) { 110 if (min1 < min2) return -1; 111 else if (min1 > min2) return 1; 112 else return 0; 113 } else if (!tz1) { 114 min1 -= MAX_TIMEZONE_OFFSET; 115 max1 += MAX_TIMEZONE_OFFSET; 116 } else { 117 min2 -= MAX_TIMEZONE_OFFSET; 118 max2 += MAX_TIMEZONE_OFFSET; 119 } 120 if (max1 < min2) return -1; 121 else if (max2 < min1) return 1; 122 else throw new IllegalArgumentException ("Indeterminate comparison result for these arguments"); 123 } 124 } 125 | Popular Tags |