1 55 package org.jboss.axis.types; 56 57 import org.jboss.axis.utils.Messages; 58 59 import java.text.NumberFormat ; 60 61 67 public class Year 68 { 69 int year; 70 String timezone = null; 71 72 76 public Year(int year) throws NumberFormatException 77 { 78 setValue(year); 79 } 80 81 85 public Year(int year, String timezone) throws NumberFormatException 86 { 87 setValue(year, timezone); 88 } 89 90 93 public Year(String source) throws NumberFormatException 94 { 95 int negative = 0; 96 97 if (source.charAt(0) == '-') 98 { 99 negative = 1; 100 } 101 if (source.length() < (4 + negative)) 102 { 103 throw new NumberFormatException (Messages.getMessage("badYear00")); 104 } 105 106 int pos = 4 + negative; while (pos < source.length() && Character.isDigit(source.charAt(pos))) 109 { 110 ++pos; 111 } 112 113 setValue(Integer.parseInt(source.substring(0, pos)), 114 source.substring(pos)); 115 } 116 117 public int getYear() 118 { 119 return year; 120 } 121 122 public void setYear(int year) 123 { 124 if (year == 0) 126 { 127 throw new NumberFormatException (Messages.getMessage("badYear00")); 128 } 129 130 this.year = year; 131 } 132 133 public String getTimezone() 134 { 135 return timezone; 136 } 137 138 public void setTimezone(String timezone) 139 { 140 if (timezone != null && timezone.length() > 0) 142 { 143 if (timezone.charAt(0) == '+' || (timezone.charAt(0) == '-')) 145 { 146 if (timezone.length() != 6 || 147 !Character.isDigit(timezone.charAt(1)) || 148 !Character.isDigit(timezone.charAt(2)) || 149 timezone.charAt(3) != ':' || 150 !Character.isDigit(timezone.charAt(4)) || 151 !Character.isDigit(timezone.charAt(5))) 152 throw new NumberFormatException (Messages.getMessage("badTimezone00")); 153 154 } 155 else if (!timezone.equals("Z")) 156 { 157 throw new NumberFormatException (Messages.getMessage("badTimezone00")); 158 } 159 this.timezone = timezone; 161 } 162 } 163 164 public void setValue(int year, String timezone) 165 throws NumberFormatException 166 { 167 setYear(year); 168 setTimezone(timezone); 169 } 170 171 public void setValue(int year) throws NumberFormatException 172 { 173 setYear(year); 174 } 175 176 public String toString() 177 { 178 NumberFormat nf = NumberFormat.getInstance(); 180 nf.setGroupingUsed(false); 181 182 nf.setMinimumIntegerDigits(4); 184 String s = nf.format(year); 185 186 if (timezone != null) 188 { 189 s = s + timezone; 190 } 191 return s; 192 } 193 194 public boolean equals(Object obj) 195 { 196 if (!(obj instanceof Year)) return false; 197 Year other = (Year)obj; 198 if (obj == null) return false; 199 if (this == obj) return true; 200 201 boolean equals = (this.year == other.year); 202 if (timezone != null) 203 { 204 equals = equals && timezone.equals(other.timezone); 205 } 206 return equals; 207 } 208 } 209 | Popular Tags |