1 55 package org.jboss.axis.types; 56 57 import org.jboss.axis.utils.Messages; 58 59 import java.text.SimpleDateFormat ; 60 import java.util.Calendar ; 61 import java.util.Date ; 62 import java.util.TimeZone ; 63 64 67 public class Time 68 { 69 private Calendar _value; 70 71 private static SimpleDateFormat zulu = 72 new SimpleDateFormat ("HH:mm:ss.SSS'Z'"); 73 74 static 76 { 77 zulu.setTimeZone(TimeZone.getTimeZone("GMT")); 78 } 79 80 81 84 public Time(Calendar value) 85 { 86 this._value = value; 87 _value.set(0, 0, 0); } 89 90 93 public Time(String value) throws NumberFormatException 94 { 95 _value = makeValue(value); 96 } 97 98 public Calendar getAsCalendar() 99 { 100 return _value; 101 } 102 103 public void setTime(Calendar date) 104 { 105 this._value = date; 106 _value.set(0, 0, 0); } 108 109 public void setTime(Date date) 110 { 111 _value.setTime(date); 112 _value.set(0, 0, 0); } 114 115 118 private Calendar makeValue(String source) throws NumberFormatException 119 { 120 Calendar calendar = Calendar.getInstance(); 121 Date date; 122 123 if (source != null) 125 { 126 if (source.charAt(2) != ':' || source.charAt(5) != ':') 127 throw new NumberFormatException (Messages.getMessage("badTime00")); 128 if (source.length() < 8) 129 { 130 throw new NumberFormatException (Messages.getMessage("badTime00")); 131 } 132 } 133 134 try 136 { 137 synchronized (zulu) 138 { 139 date = zulu.parse(source == null ? null : 140 (source.substring(0, 8) + ".000Z")); 141 } 142 } 143 catch (Exception e) 144 { 145 throw new NumberFormatException (e.toString()); 146 } 147 148 int pos = 8; 150 if (source != null) 152 { 153 if (pos < source.length() && source.charAt(pos) == '.') 154 { 155 int milliseconds = 0; 156 int start = ++pos; 157 while (pos < source.length() && 158 Character.isDigit(source.charAt(pos))) 159 pos++; 160 161 String decimal = source.substring(start, pos); 162 if (decimal.length() == 3) 163 { 164 milliseconds = Integer.parseInt(decimal); 165 } 166 else if (decimal.length() < 3) 167 { 168 milliseconds = Integer.parseInt((decimal + "000") 169 .substring(0, 3)); 170 } 171 else 172 { 173 milliseconds = Integer.parseInt(decimal.substring(0, 3)); 174 if (decimal.charAt(3) >= '5') ++milliseconds; 175 } 176 177 date.setTime(date.getTime() + milliseconds); 179 } 180 181 if (pos + 5 < source.length() && 183 (source.charAt(pos) == '+' || (source.charAt(pos) == '-'))) 184 { 185 if (!Character.isDigit(source.charAt(pos + 1)) || 186 !Character.isDigit(source.charAt(pos + 2)) || 187 source.charAt(pos + 3) != ':' || 188 !Character.isDigit(source.charAt(pos + 4)) || 189 !Character.isDigit(source.charAt(pos + 5))) 190 throw new NumberFormatException (Messages.getMessage("badTimezone00")); 191 192 int hours = (source.charAt(pos + 1) - '0') * 10 193 + source.charAt(pos + 2) - '0'; 194 int mins = (source.charAt(pos + 4) - '0') * 10 195 + source.charAt(pos + 5) - '0'; 196 int milliseconds = (hours * 60 + mins) * 60 * 1000; 197 198 if (source.charAt(pos) == '+') milliseconds = -milliseconds; 200 date.setTime(date.getTime() + milliseconds); 201 pos += 6; 202 } 203 204 if (pos < source.length() && source.charAt(pos) == 'Z') 205 { 206 pos++; 207 calendar.setTimeZone(TimeZone.getTimeZone("GMT")); 208 } 209 210 if (pos < source.length()) 211 throw new NumberFormatException (Messages.getMessage("badChars00")); 212 } 213 214 calendar.setTime(date); 215 calendar.set(0, 0, 0); 217 return calendar; 218 } 219 220 public String toString() 221 { 222 synchronized (zulu) 223 { 224 return zulu.format(_value.getTime()); 225 } 226 227 } 228 229 public boolean equals(Object obj) 230 { 231 if (!(obj instanceof Time)) return false; 232 Time other = (Time)obj; 233 if (obj == null) return false; 234 if (this == obj) return true; 235 236 boolean _equals; 237 _equals = true && 238 ((_value == null && other._value == null) || 239 (_value != null && 240 _value.equals(other._value))); 241 242 return _equals; 243 244 } 245 } 246 | Popular Tags |