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 MonthDay implements java.io.Serializable { 29 int month; 30 int day; 31 String timezone = null; 32 33 37 public MonthDay(int month, int day) 38 throws NumberFormatException { 39 setValue(month, day); 40 } 41 42 46 public MonthDay(int month, int day, String timezone) 47 throws NumberFormatException { 48 setValue(month, day, timezone); 49 } 50 51 54 public MonthDay(String source) throws NumberFormatException { 55 if (source.length() < 6) { 56 throw new NumberFormatException ( 57 Messages.getMessage("badMonthDay00")); 58 } 59 60 if (source.charAt(0) != '-' || 61 source.charAt(1) != '-' || 62 source.charAt(4) != '-' ) { 63 throw new NumberFormatException ( 64 Messages.getMessage("badMonthDay00")); 65 } 66 67 setValue(Integer.parseInt(source.substring(2,4)), 68 Integer.parseInt(source.substring(5,7)), 69 source.substring(7)); 70 } 71 72 public int getMonth() { 73 return month; 74 } 75 76 public void setMonth(int month) { 77 if (month < 1 || month > 12) { 79 throw new NumberFormatException ( 80 Messages.getMessage("badMonthDay00")); 81 } 82 this.month = month; 83 } 84 85 public int getDay() { 86 return day; 87 } 88 89 93 public void setDay(int day) { 94 if (day < 1 || day > 31) { 96 throw new NumberFormatException ( 97 Messages.getMessage("badMonthDay00")); 98 } 99 if ((month == 2 && day > 29) || 102 ((month == 9 || month == 4 || month == 6 || month == 11) && day > 30)) { 103 throw new NumberFormatException ( 104 Messages.getMessage("badMonthDay00")); 105 } 106 this.day = day; 107 } 108 109 public String getTimezone() { 110 return timezone; 111 } 112 113 public void setTimezone(String timezone) { 114 if (timezone != null && timezone.length() > 0) { 116 if (timezone.charAt(0)=='+' || (timezone.charAt(0)=='-')) { 118 if (timezone.length() != 6 || 119 !Character.isDigit(timezone.charAt(1)) || 120 !Character.isDigit(timezone.charAt(2)) || 121 timezone.charAt(3) != ':' || 122 !Character.isDigit(timezone.charAt(4)) || 123 !Character.isDigit(timezone.charAt(5))) 124 throw new NumberFormatException ( 125 Messages.getMessage("badTimezone00")); 126 127 } else if (!timezone.equals("Z")) { 128 throw new NumberFormatException ( 129 Messages.getMessage("badTimezone00")); 130 } 131 this.timezone = timezone; 133 } 134 } 135 136 public void setValue(int month, int day, String timezone) 137 throws NumberFormatException { 138 setMonth(month); 139 setDay(day); 140 setTimezone(timezone); 141 } 142 143 public void setValue(int month, int day) throws NumberFormatException { 144 setMonth(month); 145 setDay(day); 146 } 147 148 public String toString() { 149 NumberFormat nf = NumberFormat.getInstance(); 151 nf.setGroupingUsed(false); 152 153 nf.setMinimumIntegerDigits(2); 155 String s = "--" + nf.format(month) + "-" + nf.format(day); 156 157 if (timezone != null) { 159 s = s + timezone; 160 } 161 return s; 162 } 163 164 public boolean equals(Object obj) { 165 if (!(obj instanceof MonthDay)) return false; 166 MonthDay other = (MonthDay) obj; 167 if (obj == null) return false; 168 if (this == obj) return true; 169 170 boolean equals = (this.month == other.month && this.day == other.day); 171 if (timezone != null) { 172 equals = equals && timezone.equals(other.timezone); 173 } 174 return equals; 175 } 176 177 183 public int hashCode() { 184 return null == timezone 185 ? (month + day) 186 : (month + day) ^ timezone.hashCode(); 187 } 188 } 189 | Popular Tags |