1 10 package com.hp.hpl.jena.datatypes.xsd; 11 12 import java.util.*; 13 14 15 23 public class XSDDateTime extends AbstractDateTime { 24 25 public static final short YEAR_MASK = 0x1; 26 27 28 public static final short MONTH_MASK = 0x2; 29 30 31 public static final short DAY_MASK = 0x4; 32 33 34 public static final short TIME_MASK = 0x8; 35 36 37 public static final short FULL_MASK = 0xf; 38 39 40 public static final HashMap maskMap = new HashMap(); 41 42 43 protected short mask; 44 45 53 public XSDDateTime(Object value, int mask) { 54 super(value); 55 this.mask = (short)mask; 56 } 57 58 59 64 public XSDDateTime(Calendar date) { 65 super(convertCalendar(date)); 66 this.mask = FULL_MASK; 67 } 68 69 72 private static int[] convertCalendar(Calendar date) { 73 int[] data = new int[TOTAL_SIZE]; 74 int offset = date.get(Calendar.ZONE_OFFSET) + date.get(Calendar.DST_OFFSET); 75 Calendar cal = date; 77 if (offset != 0) { 78 cal = (Calendar)date.clone(); 79 cal.add(Calendar.MILLISECOND, -offset); 80 } 81 data[AbstractDateTime.CY] = cal.get(Calendar.YEAR); 82 data[AbstractDateTime.M] = cal.get(Calendar.MONTH) + 1; 83 data[AbstractDateTime.D] = cal.get(Calendar.DAY_OF_MONTH); 84 data[AbstractDateTime.h] = cal.get(Calendar.HOUR_OF_DAY); 85 data[AbstractDateTime.m] = cal.get(Calendar.MINUTE); 86 data[AbstractDateTime.s] = cal.get(Calendar.SECOND); 87 data[AbstractDateTime.ms] = cal.get(Calendar.MILLISECOND); 88 data[AbstractDateTime.utc] = 'Z'; 89 return data; 90 } 91 92 105 public Calendar asCalendar () throws IllegalDateTimeFieldException { 106 TimeZone tz = data[utc] == 'Z' ? TimeZone.getTimeZone("GMT") : TimeZone.getDefault(); 107 Calendar calendar = new GregorianCalendar(tz); 108 calendar.set(data[CY], data[M] - 1, data[D], data[h], data[m], data[s]); 109 calendar.set(Calendar.MILLISECOND, data[ms]); 110 return calendar; 113 } 114 115 119 public int getYears() throws IllegalDateTimeFieldException { 120 if ((mask & YEAR_MASK) == 0) throw new IllegalDateTimeFieldException("Year not available"); 121 return data[CY]; 122 } 123 124 128 public int getMonths() throws IllegalDateTimeFieldException { 129 if ((mask & MONTH_MASK) == 0) throw new IllegalDateTimeFieldException("Month not available"); 130 return data[M]; 131 } 132 133 137 public int getDays() throws IllegalDateTimeFieldException { 138 if ((mask & DAY_MASK) == 0) throw new IllegalDateTimeFieldException("Day not available"); 139 return data[D]; 140 } 141 142 146 public int getHours() throws IllegalDateTimeFieldException { 147 if ((mask & TIME_MASK) == 0) throw new IllegalDateTimeFieldException("Time not available"); 148 return data[h]; 149 } 150 151 155 public int getMinutes() throws IllegalDateTimeFieldException { 156 if ((mask & TIME_MASK) == 0) throw new IllegalDateTimeFieldException("Time not available"); 157 return data[m]; 158 } 159 160 164 public int getFullSeconds() throws IllegalDateTimeFieldException { 165 if ((mask & TIME_MASK) == 0) throw new IllegalDateTimeFieldException("Time not available"); 166 return data[s]; 167 } 168 169 173 public double getSeconds() throws IllegalDateTimeFieldException { 174 if ((mask & TIME_MASK) == 0) throw new IllegalDateTimeFieldException("Time not available"); 175 return data[s] + fractionalSeconds; 176 } 177 178 183 public double getTimePart() throws IllegalDateTimeFieldException { 184 if ((mask & TIME_MASK) == 0) throw new IllegalDateTimeFieldException("Time not available"); 185 return ((data[h]) * 60l + data[m]) * 60l + getSeconds(); 186 } 187 188 191 public String toString() { 192 StringBuffer buff = new StringBuffer (); 193 if ((mask & YEAR_MASK) != 0) { 194 buff.append(data[CY]); 195 } else { 196 buff.append("-"); 197 } 198 if ((mask & (MONTH_MASK | DAY_MASK)) != 0) { 199 buff.append("-"); 200 if ((mask & MONTH_MASK) != 0) { 201 if (data[M] <= 9) buff.append("0"); 202 buff.append(data[M]); 203 } else { 204 buff.append("-"); 205 } 206 if ((mask & DAY_MASK) != 0) { 207 if (mask != DAY_MASK) buff.append("-"); 208 if (data[D] <= 9) buff.append("0"); 209 buff.append(data[D]); 210 } 211 } 212 if ((mask & TIME_MASK) != 0 ) { 213 buff.append("T"); 214 if(data[h]<10) buff.append("0"); 215 buff.append(data[h]); 216 217 buff.append(":"); 218 if(data[m]<10) buff.append("0"); 219 buff.append(data[m]); 220 221 buff.append(":"); 222 if(data[s]<10) buff.append("0"); 223 buff.append(data[s]); 224 225 if (data[ms] != 0) { 226 buff.append("."); 227 buff.append(data[ms]); 228 } 229 buff.append("Z"); 230 } 231 return buff.toString(); 232 } 233 234 } 235 236 265 | Popular Tags |