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 YearMonth 68 { 69 int year; 70 int month; 71 String timezone = null; 72 73 77 public YearMonth(int year, int month) throws NumberFormatException 78 { 79 setValue(year, month); 80 } 81 82 86 public YearMonth(int year, int month, String timezone) throws NumberFormatException 87 { 88 setValue(year, month, timezone); 89 } 90 91 94 public YearMonth(String source) throws NumberFormatException 95 { 96 int negative = 0; 97 98 if (source.charAt(0) == '-') 99 { 100 negative = 1; 101 } 102 if (source.length() < (7 + negative)) 103 { 104 throw new NumberFormatException (Messages.getMessage("badYearMonth00")); 105 } 106 107 int pos = source.substring(negative).indexOf('-'); 109 if (pos < 0) 110 { 111 throw new NumberFormatException (Messages.getMessage("badYearMonth00")); 112 } 113 if (negative > 0) pos++; 115 setValue(Integer.parseInt(source.substring(0, pos)), 116 Integer.parseInt(source.substring(pos + 1, pos + 3)), 117 source.substring(pos + 3)); 118 } 119 120 public int getYear() 121 { 122 return year; 123 } 124 125 public void setYear(int year) 126 { 127 if (year == 0) 129 { 130 throw new NumberFormatException (Messages.getMessage("badYearMonth00")); 131 } 132 133 this.year = year; 134 } 135 136 public int getMonth() 137 { 138 return month; 139 } 140 141 public void setMonth(int month) 142 { 143 if (month < 1 || month > 12) 145 { 146 throw new NumberFormatException (Messages.getMessage("badYearMonth00")); 147 } 148 this.month = month; 149 } 150 151 public String getTimezone() 152 { 153 return timezone; 154 } 155 156 public void setTimezone(String timezone) 157 { 158 if (timezone != null && timezone.length() > 0) 160 { 161 if (timezone.charAt(0) == '+' || (timezone.charAt(0) == '-')) 163 { 164 if (timezone.length() != 6 || 165 !Character.isDigit(timezone.charAt(1)) || 166 !Character.isDigit(timezone.charAt(2)) || 167 timezone.charAt(3) != ':' || 168 !Character.isDigit(timezone.charAt(4)) || 169 !Character.isDigit(timezone.charAt(5))) 170 throw new NumberFormatException (Messages.getMessage("badTimezone00")); 171 172 } 173 else if (!timezone.equals("Z")) 174 { 175 throw new NumberFormatException (Messages.getMessage("badTimezone00")); 176 } 177 this.timezone = timezone; 179 } 180 } 181 182 public void setValue(int year, int month, String timezone) throws NumberFormatException 183 { 184 setYear(year); 185 setMonth(month); 186 setTimezone(timezone); 187 } 188 189 public void setValue(int year, int month) throws NumberFormatException 190 { 191 setYear(year); 192 setMonth(month); 193 } 194 195 public String toString() 196 { 197 NumberFormat nf = NumberFormat.getInstance(); 199 nf.setGroupingUsed(false); 200 201 nf.setMinimumIntegerDigits(4); 203 String s = nf.format(year) + "-"; 204 205 nf.setMinimumIntegerDigits(2); 207 s += nf.format(month); 208 209 if (timezone != null) 211 { 212 s = s + timezone; 213 } 214 return s; 215 } 216 217 public boolean equals(Object obj) 218 { 219 if (!(obj instanceof YearMonth)) return false; 220 YearMonth other = (YearMonth)obj; 221 if (obj == null) return false; 222 if (this == obj) return true; 223 224 boolean equals = (this.year == other.year && this.month == other.month); 225 if (timezone != null) 226 { 227 equals = equals && timezone.equals(other.timezone); 228 } 229 return equals; 230 } 231 } 232 | Popular Tags |