| 1 | |
package org.jtheque.utils.bean; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
import java.io.Serializable; |
| 20 | |
import java.text.DateFormat; |
| 21 | |
import java.text.SimpleDateFormat; |
| 22 | |
import java.util.Calendar; |
| 23 | |
import java.util.Locale; |
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | 4 | public final class IntDate implements Serializable, Comparable<IntDate> { |
| 31 | |
private static final long serialVersionUID = -5493916511728423774L; |
| 32 | |
private static final int DATE_STR_LENGTH = 20; |
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
public static final class Fields { |
| 40 | |
public static final int MONTH = Calendar.MONTH; |
| 41 | |
public static final int DAY = Calendar.DAY_OF_MONTH; |
| 42 | |
public static final int YEAR = Calendar.YEAR; |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | 0 | private Fields() { |
| 48 | |
|
| 49 | 0 | } |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
public static boolean isValid(int field) { |
| 58 | 26 | return field == MONTH || field == DAY || field == YEAR; |
| 59 | |
} |
| 60 | |
} |
| 61 | |
|
| 62 | 2 | private static final IntDate TODAY_DATE = new IntDate(Calendar.getInstance()); |
| 63 | |
|
| 64 | 44 | private final DateFormat format = new SimpleDateFormat("dd.MM.yyyy", Locale.getDefault()); |
| 65 | |
private final Calendar calendar; |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
public IntDate(int date) { |
| 73 | 40 | super(); |
| 74 | |
|
| 75 | 40 | calendar = Calendar.getInstance(); |
| 76 | 40 | calendar.clear(); |
| 77 | |
|
| 78 | 40 | String dateString = Integer.toString(date); |
| 79 | |
|
| 80 | 40 | int day = Integer.parseInt(dateString.substring(6, 8)); |
| 81 | 40 | int month = Integer.parseInt(dateString.substring(4, 6)) - 1; |
| 82 | 40 | int year = Integer.parseInt(dateString.substring(0, 4)); |
| 83 | |
|
| 84 | 40 | calendar.set(year, month, day); |
| 85 | 40 | } |
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
private IntDate(Calendar calendar) { |
| 93 | 2 | super(); |
| 94 | |
|
| 95 | 2 | this.calendar = calendar; |
| 96 | 2 | } |
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
public IntDate(IntDate value) { |
| 104 | 2 | super(); |
| 105 | |
|
| 106 | 2 | calendar = (Calendar) value.calendar.clone(); |
| 107 | 2 | } |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
public int getDay() { |
| 115 | 48 | return calendar.get(Calendar.DAY_OF_MONTH); |
| 116 | |
} |
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
public int getYear() { |
| 124 | 70 | return calendar.get(Calendar.YEAR); |
| 125 | |
} |
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
public int getMonth() { |
| 133 | |
|
| 134 | 52 | return calendar.get(Calendar.MONTH) + 1; |
| 135 | |
} |
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
public void add(int field, int toAdd) { |
| 145 | 8 | if (Fields.isValid(field)) { |
| 146 | 6 | calendar.add(field, toAdd); |
| 147 | |
} else { |
| 148 | 2 | throw new IllegalArgumentException(); |
| 149 | |
} |
| 150 | 6 | } |
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
public void set(final int field, final int value) { |
| 159 | 8 | if (Fields.isValid(field)) { |
| 160 | 6 | if (field == Fields.MONTH) { |
| 161 | 2 | calendar.set(Fields.MONTH, value - 1); |
| 162 | |
} else { |
| 163 | 4 | calendar.set(field, value); |
| 164 | |
} |
| 165 | |
} else { |
| 166 | 2 | throw new IllegalArgumentException(); |
| 167 | |
} |
| 168 | 6 | } |
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
public String getStrDate() { |
| 176 | 14 | StringBuilder builder = new StringBuilder(DATE_STR_LENGTH); |
| 177 | |
|
| 178 | 14 | builder.append(getYear()); |
| 179 | |
|
| 180 | 14 | if (getMonth() < 10) { |
| 181 | 14 | builder.append('0'); |
| 182 | |
} |
| 183 | |
|
| 184 | 14 | builder.append(getMonth()); |
| 185 | |
|
| 186 | 14 | if (getDay() < 10) { |
| 187 | 14 | builder.append('0'); |
| 188 | |
} |
| 189 | |
|
| 190 | 14 | builder.append(getDay()); |
| 191 | |
|
| 192 | 14 | return builder.toString(); |
| 193 | |
} |
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
public static IntDate today() { |
| 201 | 2 | return TODAY_DATE; |
| 202 | |
} |
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
public int intValue() { |
| 210 | 8 | return Integer.parseInt(getStrDate()); |
| 211 | |
} |
| 212 | |
|
| 213 | |
@Override |
| 214 | |
public int compareTo(IntDate o) { |
| 215 | 16 | if (getYear() > o.getYear()) { |
| 216 | 8 | return 1; |
| 217 | 8 | } else if (getYear() < o.getYear()) { |
| 218 | 4 | return -1; |
| 219 | |
} |
| 220 | |
|
| 221 | 4 | if (getMonth() > o.getMonth()) { |
| 222 | 0 | return 1; |
| 223 | 4 | } else if (getMonth() < o.getMonth()) { |
| 224 | 0 | return -1; |
| 225 | |
} |
| 226 | |
|
| 227 | 4 | if (getDay() > o.getDay()) { |
| 228 | 2 | return 1; |
| 229 | 2 | } else if (getDay() < o.getDay()) { |
| 230 | 0 | return -1; |
| 231 | |
} |
| 232 | |
|
| 233 | 2 | return 0; |
| 234 | |
} |
| 235 | |
|
| 236 | |
@Override |
| 237 | |
public boolean equals(Object o) { |
| 238 | 14 | if (this == o) { |
| 239 | 4 | return true; |
| 240 | |
} |
| 241 | |
|
| 242 | 10 | if (o == null || getClass() != o.getClass()) { |
| 243 | 2 | return false; |
| 244 | |
} |
| 245 | |
|
| 246 | 8 | IntDate other = (IntDate) o; |
| 247 | |
|
| 248 | 8 | return calendar.equals(other.calendar); |
| 249 | |
|
| 250 | |
} |
| 251 | |
|
| 252 | |
@Override |
| 253 | |
public int hashCode() { |
| 254 | 8 | return calendar.hashCode(); |
| 255 | |
} |
| 256 | |
|
| 257 | |
@Override |
| 258 | |
public String toString() { |
| 259 | 2 | return format.format(calendar.getTime()); |
| 260 | |
} |
| 261 | |
} |