1 16 package org.apache.axis.types; 17 18 import org.apache.axis.utils.Messages; 19 20 import java.text.SimpleDateFormat ; 21 import java.util.Calendar ; 22 import java.util.Date ; 23 import java.util.TimeZone ; 24 25 28 public class Time implements java.io.Serializable { 29 private Calendar _value; 30 31 32 36 private static SimpleDateFormat zulu = 37 new SimpleDateFormat ("HH:mm:ss.SSS'Z'"); 38 39 static { 41 zulu.setTimeZone(TimeZone.getTimeZone("GMT")); 42 } 43 44 45 48 public Time(Calendar value) { 49 this._value = value; 50 _value.set(0,0,0); } 52 53 56 public Time(String value) throws NumberFormatException { 57 _value = makeValue(value); 58 } 59 60 64 public Calendar getAsCalendar() { 65 return _value; 66 } 67 68 72 public void setTime(Calendar date) { 73 this._value = date; 74 _value.set(0,0,0); } 76 77 81 public void setTime(Date date) { 82 _value.setTime(date); 83 _value.set(0,0,0); } 85 86 89 private Calendar makeValue(String source) throws NumberFormatException { 90 Calendar calendar = Calendar.getInstance(); 91 Date date; 92 93 validateSource(source); 94 95 date = ParseHoursMinutesSeconds(source); 97 98 int pos = 8; 100 if ( source != null ) { 102 if (pos < source.length() && source.charAt(pos)=='.') { 103 int milliseconds = 0; 104 int start = ++pos; 105 while (pos<source.length() && 106 Character.isDigit(source.charAt(pos))) { 107 pos++; 108 } 109 110 111 String decimal=source.substring(start,pos); 112 if (decimal.length()==3) { 113 milliseconds=Integer.parseInt(decimal); 114 } else if (decimal.length() < 3) { 115 milliseconds=Integer.parseInt((decimal+"000") 116 .substring(0,3)); 117 } else { 118 milliseconds=Integer.parseInt(decimal.substring(0,3)); 119 if (decimal.charAt(3)>='5') { 120 ++milliseconds; 121 } 122 } 123 124 date.setTime(date.getTime()+milliseconds); 126 } 127 128 if (pos+5 < source.length() && 130 (source.charAt(pos)=='+' || (source.charAt(pos)=='-'))) { 131 if (!Character.isDigit(source.charAt(pos+1)) || 132 !Character.isDigit(source.charAt(pos+2)) || 133 source.charAt(pos+3) != ':' || 134 !Character.isDigit(source.charAt(pos+4)) || 135 !Character.isDigit(source.charAt(pos+5))) 136 { 137 throw new NumberFormatException ( 138 Messages.getMessage("badTimezone00")); 139 } 140 141 int hours = (source.charAt(pos+1)-'0')*10 142 +source.charAt(pos+2)-'0'; 143 int mins = (source.charAt(pos+4)-'0')*10 144 +source.charAt(pos+5)-'0'; 145 int milliseconds = (hours*60+mins)*60*1000; 146 147 if (source.charAt(pos)=='+') { 149 milliseconds=-milliseconds; 150 } 151 date.setTime(date.getTime()+milliseconds); 152 pos+=6; 153 } 154 155 if (pos < source.length() && source.charAt(pos)=='Z') { 156 pos++; 157 calendar.setTimeZone(TimeZone.getTimeZone("GMT")); 158 } 159 160 if (pos < source.length()) { 161 throw new NumberFormatException ( 162 Messages.getMessage("badChars00")); 163 } 164 } 165 166 calendar.setTime(date); 167 calendar.set(0,0,0); 169 return calendar; 170 } 171 172 private int getTimezoneNumberValue(char c) { 173 int n=c-'0'; 174 if(n<0 || n>9) { 175 throw new NumberFormatException ( 177 Messages.getMessage("badTimezone00")); 178 } 179 return n; 180 } 181 182 191 private static Date ParseHoursMinutesSeconds(String source) { 192 Date date; 193 try { 194 synchronized (zulu) { 195 String fulltime = source == null ? null : 196 (source.substring(0,8)+".000Z"); 197 date = zulu.parse(fulltime); 198 } 199 } catch (Exception e) { 200 throw new NumberFormatException (e.toString()); 201 } 202 return date; 203 } 204 205 209 private void validateSource(String source) { 210 if ( source != null ) { 212 if (source.charAt(2) != ':' || source.charAt(5) != ':') { 213 throw new NumberFormatException ( 214 Messages.getMessage("badTime00")); 215 } 216 if (source.length() < 8) { 217 throw new NumberFormatException ( 218 Messages.getMessage("badTime00")); 219 } 220 } 221 } 222 223 228 public String toString() { 229 if(_value==null) { 230 return "unassigned Time"; 231 } 232 synchronized (zulu) { 233 return zulu.format(_value.getTime()); 234 } 235 236 } 237 238 public boolean equals(Object obj) { 239 if (obj == null) return false; 240 if (!(obj instanceof Time)) return false; 241 Time other = (Time) obj; 242 if (this == obj) return true; 243 244 boolean _equals; 245 _equals = true && 246 ((_value ==null && other._value ==null) || 247 (_value !=null && 248 _value.getTime().equals(other._value.getTime()))); 249 250 return _equals; 251 252 } 253 254 259 public int hashCode() { 260 return _value == null ? 0 : _value.hashCode(); 261 } 262 } 263 | Popular Tags |