1 16 package org.apache.axis.types; 17 18 import org.apache.axis.utils.Messages; 19 20 import java.text.NumberFormat ; 21 22 28 public class YearMonth implements java.io.Serializable { 29 int year; 30 int month; 31 String timezone = null; 32 33 37 public YearMonth(int year, int month) throws NumberFormatException { 38 setValue(year, month); 39 } 40 41 45 public YearMonth(int year, int month, String timezone) throws NumberFormatException { 46 setValue(year, month, timezone); 47 } 48 49 52 public YearMonth(String source) throws NumberFormatException { 53 int negative = 0; 54 55 if (source.charAt(0) == '-') { 56 negative = 1; 57 } 58 if (source.length() < (7 + negative)) { 59 throw new NumberFormatException ( 60 Messages.getMessage("badYearMonth00")); 61 } 62 63 int pos = source.substring(negative).indexOf('-'); 65 if (pos < 0) { 66 throw new NumberFormatException ( 67 Messages.getMessage("badYearMonth00")); 68 } 69 if (negative > 0) pos++; 71 setValue(Integer.parseInt(source.substring(0,pos)), 72 Integer.parseInt(source.substring(pos+1,pos+3)), 73 source.substring(pos+3)); 74 } 75 76 public int getYear() { 77 return year; 78 } 79 80 public void setYear(int year) { 81 if (year == 0) { 83 throw new NumberFormatException ( 84 Messages.getMessage("badYearMonth00")); 85 } 86 87 this.year = year; 88 } 89 90 public int getMonth() { 91 return month; 92 } 93 94 public void setMonth(int month) { 95 if (month < 1 || month > 12) { 97 throw new NumberFormatException ( 98 Messages.getMessage("badYearMonth00")); 99 } 100 this.month = month; 101 } 102 103 public String getTimezone() { 104 return timezone; 105 } 106 107 public void setTimezone(String timezone) { 108 if (timezone != null && timezone.length() > 0) { 110 if (timezone.charAt(0)=='+' || (timezone.charAt(0)=='-')) { 112 if (timezone.length() != 6 || 113 !Character.isDigit(timezone.charAt(1)) || 114 !Character.isDigit(timezone.charAt(2)) || 115 timezone.charAt(3) != ':' || 116 !Character.isDigit(timezone.charAt(4)) || 117 !Character.isDigit(timezone.charAt(5))) 118 throw new NumberFormatException ( 119 Messages.getMessage("badTimezone00")); 120 121 } else if (!timezone.equals("Z")) { 122 throw new NumberFormatException ( 123 Messages.getMessage("badTimezone00")); 124 } 125 this.timezone = timezone; 127 } 128 } 129 130 public void setValue(int year, int month, String timezone) throws NumberFormatException { 131 setYear(year); 132 setMonth(month); 133 setTimezone(timezone); 134 } 135 136 public void setValue(int year, int month) throws NumberFormatException { 137 setYear(year); 138 setMonth(month); 139 } 140 141 public String toString() { 142 NumberFormat nf = NumberFormat.getInstance(); 144 nf.setGroupingUsed(false); 145 146 nf.setMinimumIntegerDigits(4); 148 String s = nf.format(year) + "-"; 149 150 nf.setMinimumIntegerDigits(2); 152 s += nf.format(month); 153 154 if (timezone != null) { 156 s = s + timezone; 157 } 158 return s; 159 } 160 161 public boolean equals(Object obj) { 162 if (!(obj instanceof YearMonth)) return false; 163 YearMonth other = (YearMonth) obj; 164 if (obj == null) return false; 165 if (this == obj) return true; 166 167 boolean equals = (this.year == other.year && this.month == other.month); 168 if (timezone != null) { 169 equals = equals && timezone.equals(other.timezone); 170 } 171 return equals; 172 } 173 174 180 public int hashCode() { 181 return null == timezone 182 ? (month + year) 183 : (month + year) ^ timezone.hashCode(); 184 } 185 } 186 | Popular Tags |