1 24 package javax.jcr; 25 26 import javax.jcr.util.ISO8601; 27 import java.util.Calendar ; 28 29 35 public class DateValue extends BaseValue { 36 37 public static final int TYPE = PropertyType.DATE; 38 39 private final Calendar date; 40 41 47 public DateValue(Calendar date) { 48 super(TYPE); 49 this.date = date; 50 } 51 52 66 public static DateValue valueOf(String s) throws ValueFormatException { 67 Calendar cal = ISO8601.parse(s); 68 if (cal != null) { 69 return new DateValue(cal); 70 } else { 71 throw new ValueFormatException("not a valid date format"); 72 } 73 } 74 75 86 public boolean equals(Object obj) { 87 if (this == obj) { 88 return true; 89 } 90 if (obj instanceof DateValue) { 91 DateValue other = (DateValue) obj; 92 if (date == other.date) { 93 return true; 94 } else if (date != null && other.date != null) { 95 return date.equals(other.date); 96 } 97 } 98 return false; 99 } 100 101 105 public Calendar getDate() throws ValueFormatException, IllegalStateException , RepositoryException { 106 setValueConsumed(); 107 108 if (date != null) { 109 return date; 110 } else { 111 throw new ValueFormatException("empty value"); 112 } 113 } 114 115 118 public long getLong() throws ValueFormatException, IllegalStateException , RepositoryException { 119 setValueConsumed(); 120 121 if (date != null) { 122 return date.getTime().getTime(); 123 } else { 124 throw new ValueFormatException("empty value"); 125 } 126 } 127 128 131 public boolean getBoolean() throws ValueFormatException, IllegalStateException , RepositoryException { 132 setValueConsumed(); 133 134 if (date != null) { 135 throw new ValueFormatException("cannot convert date to boolean"); 136 } else { 137 throw new ValueFormatException("empty value"); 138 } 139 } 140 141 144 public double getDouble() throws ValueFormatException, IllegalStateException , RepositoryException { 145 setValueConsumed(); 146 147 if (date != null) { 148 long ms = date.getTime().getTime(); 149 if (ms <= Double.MAX_VALUE) { 150 return ms; 151 } 152 throw new ValueFormatException("conversion from date to double failed: inconvertible types"); 153 } else { 154 throw new ValueFormatException("empty value"); 155 } 156 } 157 158 161 public String getString() throws ValueFormatException, IllegalStateException , RepositoryException { 162 setValueConsumed(); 163 164 if (date != null) { 165 return ISO8601.format(date); 166 } else { 167 throw new ValueFormatException("empty value"); 168 } 169 } 170 } 171 | Popular Tags |