| 1 | package com.hcarralero.toljava; |
|---|
| 2 | |
|---|
| 3 | import java.text.DateFormat; |
|---|
| 4 | import java.util.Calendar; |
|---|
| 5 | import java.util.Date; |
|---|
| 6 | |
|---|
| 7 | /** |
|---|
| 8 | * Representa a un objeto TOL de tipo Date. |
|---|
| 9 | * |
|---|
| 10 | * @author Humberto Andrés Carralero López (hcarralero@gmail.com) |
|---|
| 11 | * @version 1.0 |
|---|
| 12 | */ |
|---|
| 13 | public class TOLDate extends TOLLObject { |
|---|
| 14 | |
|---|
| 15 | /** |
|---|
| 16 | * Constante que representa la fecha del "Inicio del Tiempo" |
|---|
| 17 | */ |
|---|
| 18 | public static final TOLDate BEGIN_DATE= new TOLDate('B'); |
|---|
| 19 | |
|---|
| 20 | /** |
|---|
| 21 | * Constante que representa la fecha del "Fin del Tiempo" |
|---|
| 22 | */ |
|---|
| 23 | public static final TOLDate END_DATE= new TOLDate('E'); |
|---|
| 24 | |
|---|
| 25 | /** |
|---|
| 26 | * Construye un objeto <code>TOLDate</code> sin especificar valores. Ãtil para construir fechas especiales |
|---|
| 27 | * no asociadas a fechas reales, tales como fechas TOL: el "INICIO" y el "FIN" de los tiempos |
|---|
| 28 | */ |
|---|
| 29 | private TOLDate(char t) { |
|---|
| 30 | switch (t) { |
|---|
| 31 | case 'B': |
|---|
| 32 | this.year= java.lang.Integer.MIN_VALUE; |
|---|
| 33 | this.month= java.lang.Integer.MIN_VALUE; |
|---|
| 34 | this.day= java.lang.Integer.MIN_VALUE; |
|---|
| 35 | this.hour= java.lang.Integer.MIN_VALUE; |
|---|
| 36 | this.minute= java.lang.Integer.MIN_VALUE; |
|---|
| 37 | this.second= java.lang.Double.MIN_VALUE; |
|---|
| 38 | break; |
|---|
| 39 | case 'E': |
|---|
| 40 | this.year= java.lang.Integer.MAX_VALUE; |
|---|
| 41 | this.month= java.lang.Integer.MAX_VALUE; |
|---|
| 42 | this.day= java.lang.Integer.MAX_VALUE; |
|---|
| 43 | this.hour= java.lang.Integer.MAX_VALUE; |
|---|
| 44 | this.minute= java.lang.Integer.MAX_VALUE; |
|---|
| 45 | this.second= java.lang.Double.MAX_VALUE; |
|---|
| 46 | break; |
|---|
| 47 | } |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | /** |
|---|
| 51 | * Construye un objeto <code>TOLDate</code> a partir de: año, mes, dÃa, hora, minuto y segundo |
|---|
| 52 | * |
|---|
| 53 | * @param year Año |
|---|
| 54 | * @param month Mes |
|---|
| 55 | * @param day DÃa |
|---|
| 56 | * @param hour Hora |
|---|
| 57 | * @param minute Minuto |
|---|
| 58 | * @param second Segundo |
|---|
| 59 | * @throws TOLDateException cuando la combinación year-month-day-hour-minute-second no corresponde a una fecha válida |
|---|
| 60 | */ |
|---|
| 61 | public TOLDate(int year, int month, int day, int hour, int minute, double second) throws TOLDateException { |
|---|
| 62 | if (year<0) { |
|---|
| 63 | throw new TOLDateException(TOLDateException.ERROR_INVALID_YEAR, "Año NO VÃLIDO por ser negativo"); |
|---|
| 64 | } |
|---|
| 65 | this.year = year; |
|---|
| 66 | if (month<1 ||month>12) { |
|---|
| 67 | throw new TOLDateException(TOLDateException.ERROR_INVALID_MONTH, "Mes NO VÃLIDO por no estar en el rango 1-12"); |
|---|
| 68 | } |
|---|
| 69 | this.month = month; |
|---|
| 70 | int nd= 30; |
|---|
| 71 | if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) { |
|---|
| 72 | nd= 31; |
|---|
| 73 | } |
|---|
| 74 | if (month==2) { |
|---|
| 75 | if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { |
|---|
| 76 | //El año es bisiesto |
|---|
| 77 | nd= 29; |
|---|
| 78 | } else { |
|---|
| 79 | nd= 28; |
|---|
| 80 | } |
|---|
| 81 | } |
|---|
| 82 | if (day<1 ||day>nd) { |
|---|
| 83 | throw new TOLDateException(TOLDateException.ERROR_INVALID_DAY, "DÃa NO VÃLIDO por no estar en el rango de valores permitidos para el mes"); |
|---|
| 84 | } |
|---|
| 85 | this.day = day; |
|---|
| 86 | if (hour<0 ||hour>23) { |
|---|
| 87 | throw new TOLDateException(TOLDateException.ERROR_INVALID_HOUR, "Hora NO VÃLIDA por no estar en el rango 0-23"); |
|---|
| 88 | } |
|---|
| 89 | this.hour = hour; |
|---|
| 90 | if (minute<0 ||minute>59) { |
|---|
| 91 | throw new TOLDateException(TOLDateException.ERROR_INVALID_MINUTE, "Minuto NO VÃLIDO por no estar en el rango 0-59"); |
|---|
| 92 | } |
|---|
| 93 | this.minute = minute; |
|---|
| 94 | if (second<0 ||second>59) { |
|---|
| 95 | throw new TOLDateException(TOLDateException.ERROR_INVALID_MINUTE, "Minuto NO VÃLIDO por no estar en el rango 0-59"); |
|---|
| 96 | } |
|---|
| 97 | this.second = second; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | /** |
|---|
| 101 | * Construye un objeto <code>TOLDate</code> a partir de: año, mes y dÃa. |
|---|
| 102 | * |
|---|
| 103 | * @param year Año |
|---|
| 104 | * @param month Mes |
|---|
| 105 | * @param day DÃa |
|---|
| 106 | * @throws TOLDateException cuando la combinación year-month-day no corresponde a una fecha válida |
|---|
| 107 | */ |
|---|
| 108 | public TOLDate(int year, int month, int day) throws TOLDateException { |
|---|
| 109 | if (year<0) { |
|---|
| 110 | throw new TOLDateException(TOLDateException.ERROR_INVALID_YEAR, "Año NO VÃLIDO por ser negativo"); |
|---|
| 111 | } |
|---|
| 112 | this.year = year; |
|---|
| 113 | if (month<1 ||month>12) { |
|---|
| 114 | throw new TOLDateException(TOLDateException.ERROR_INVALID_MONTH, "Mes NO VÃLIDO por no estar en el rango 1-12"); |
|---|
| 115 | } |
|---|
| 116 | this.month = month; |
|---|
| 117 | int nd= 30; |
|---|
| 118 | if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) { |
|---|
| 119 | nd= 31; |
|---|
| 120 | } |
|---|
| 121 | if (month==2) { |
|---|
| 122 | if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { |
|---|
| 123 | //El año es bisiesto |
|---|
| 124 | nd= 29; |
|---|
| 125 | } else { |
|---|
| 126 | nd= 28; |
|---|
| 127 | } |
|---|
| 128 | } |
|---|
| 129 | if (day<1 ||day>nd) { |
|---|
| 130 | throw new TOLDateException(TOLDateException.ERROR_INVALID_DAY, "DÃa NO VÃLIDO por no estar en el rango de valores permitidos para el mes"); |
|---|
| 131 | } |
|---|
| 132 | this.day = day; |
|---|
| 133 | this.hour= 0; |
|---|
| 134 | this.minute= 0; |
|---|
| 135 | this.second= 0; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | private int year; |
|---|
| 139 | |
|---|
| 140 | /** |
|---|
| 141 | * Obtiene el año correspondiente a la fecha almacenada |
|---|
| 142 | * |
|---|
| 143 | * @return el año |
|---|
| 144 | */ |
|---|
| 145 | public int getYear() { |
|---|
| 146 | return year; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | /** |
|---|
| 150 | * Cambia el año por el valor indicado en el parámetro. |
|---|
| 151 | * |
|---|
| 152 | * @param year nuevo valor para el año. |
|---|
| 153 | * @throws TOLDateException cuando el año no corresponde a una fecha válida |
|---|
| 154 | */ |
|---|
| 155 | public void setYear(int year) throws TOLDateException { |
|---|
| 156 | if (year<0) { |
|---|
| 157 | throw new TOLDateException(TOLDateException.ERROR_INVALID_YEAR, "Año NO VÃLIDO por ser negativo"); |
|---|
| 158 | } |
|---|
| 159 | this.year = year; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | private int month; |
|---|
| 163 | |
|---|
| 164 | /** |
|---|
| 165 | * Obtiene el mes correspondiente a la fecha almacenada |
|---|
| 166 | * |
|---|
| 167 | * @return el mes |
|---|
| 168 | */ |
|---|
| 169 | public int getMonth() { |
|---|
| 170 | return month; |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | /** |
|---|
| 174 | * Cambia el valor del mes por el valor indicado en el parámetro. |
|---|
| 175 | * |
|---|
| 176 | * @param month nuevo valor para el mes. |
|---|
| 177 | * @throws TOLDateException cuando el mes no corresponde a una fecha válida |
|---|
| 178 | */ |
|---|
| 179 | public void setMonth(int month) throws TOLDateException { |
|---|
| 180 | if (month<1 ||month>12) { |
|---|
| 181 | throw new TOLDateException(TOLDateException.ERROR_INVALID_MONTH, "Mes NO VÃLIDO por no estar en el rango 1-12"); |
|---|
| 182 | } |
|---|
| 183 | this.month = month; |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | private int day; |
|---|
| 187 | |
|---|
| 188 | /** |
|---|
| 189 | * Obtiene el dÃa correspondiente a la fecha almacenada |
|---|
| 190 | * |
|---|
| 191 | * @return el dÃa. |
|---|
| 192 | */ |
|---|
| 193 | public int getDay() { |
|---|
| 194 | return day; |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | /** |
|---|
| 198 | * Cambia el valor del dÃa por el valor indicado en el parámetro. |
|---|
| 199 | * |
|---|
| 200 | * @param day nuevo valor para el dÃa. |
|---|
| 201 | * @throws TOLDateException cuando el dÃa no corresponde a una fecha válida |
|---|
| 202 | */ |
|---|
| 203 | public void setDay(int day) throws TOLDateException { |
|---|
| 204 | int nd= 30; |
|---|
| 205 | if (this.month==1 || this.month==3 || this.month==5 || this.month==7 || this.month==8 || this.month==10 || this.month==12) { |
|---|
| 206 | nd= 31; |
|---|
| 207 | } |
|---|
| 208 | if (this.month==2) { |
|---|
| 209 | if ((this.year % 4 == 0 && this.year % 100 != 0) || this.year % 400 == 0) { |
|---|
| 210 | //El año es bisiesto |
|---|
| 211 | nd= 29; |
|---|
| 212 | } else { |
|---|
| 213 | nd= 28; |
|---|
| 214 | } |
|---|
| 215 | } |
|---|
| 216 | if (day<1 ||day>nd) { |
|---|
| 217 | throw new TOLDateException(TOLDateException.ERROR_INVALID_DAY, "DÃa NO VÃLIDO por no estar en el rango de valores permitidos para el mes"); |
|---|
| 218 | } |
|---|
| 219 | this.day = day; |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | private int hour= 0; |
|---|
| 223 | |
|---|
| 224 | /** |
|---|
| 225 | * Obtiene la hora correspondiente a la fecha almacenada |
|---|
| 226 | * |
|---|
| 227 | * @return la hora |
|---|
| 228 | */ |
|---|
| 229 | public int getHour() { |
|---|
| 230 | return hour; |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | /** |
|---|
| 234 | * Cambia el valor de la hora por el valor indicado en el parámetro |
|---|
| 235 | * |
|---|
| 236 | * @param hour nuevo valor para la hora |
|---|
| 237 | * @throws TOLDateException cuando la hora no corresponde a una fecha válida |
|---|
| 238 | */ |
|---|
| 239 | public void setHour(int hour) throws TOLDateException { |
|---|
| 240 | if (hour<0 ||hour>23) { |
|---|
| 241 | throw new TOLDateException(TOLDateException.ERROR_INVALID_HOUR, "Hora NO VÃLIDA por no estar en el rango 0-23"); |
|---|
| 242 | } |
|---|
| 243 | this.hour = hour; |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | private int minute= 0; |
|---|
| 247 | |
|---|
| 248 | /** |
|---|
| 249 | * Obtiene el minuto correspondiente a la fecha almacenada |
|---|
| 250 | * |
|---|
| 251 | * @return el minuto |
|---|
| 252 | */ |
|---|
| 253 | public int getMinute() { |
|---|
| 254 | return minute; |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | /** |
|---|
| 258 | * Cambia el valor del minuto por el valor indicado en el parámetro. |
|---|
| 259 | * |
|---|
| 260 | * @param minute nuevo valor para el minuto |
|---|
| 261 | * @throws TOLDateException cuando el minuto no corresponde a una fecha válida |
|---|
| 262 | */ |
|---|
| 263 | public void setMinute(int minute) throws TOLDateException { |
|---|
| 264 | if (minute<0 ||minute>59) { |
|---|
| 265 | throw new TOLDateException(TOLDateException.ERROR_INVALID_MINUTE, "Minuto NO VÃLIDO por no estar en el rango 0-59"); |
|---|
| 266 | } |
|---|
| 267 | this.minute = minute; |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | private double second= 0.0; |
|---|
| 271 | |
|---|
| 272 | /** |
|---|
| 273 | * Obtiene el segundo correspondiente a la fecha almacenada |
|---|
| 274 | * |
|---|
| 275 | * @return el segundo |
|---|
| 276 | */ |
|---|
| 277 | public double getSecond() { |
|---|
| 278 | return second; |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | /** |
|---|
| 282 | * Cambia el valor del segundo por el valor indicado en el parámetro |
|---|
| 283 | * |
|---|
| 284 | * @param second nuevo valor para el segundo |
|---|
| 285 | * @throws TOLDateException cuando el segundo no corresponde a una fecha válida |
|---|
| 286 | */ |
|---|
| 287 | public void setSecond(double second) throws TOLDateException { |
|---|
| 288 | if (second<0 ||second>59) { |
|---|
| 289 | throw new TOLDateException(TOLDateException.ERROR_INVALID_MINUTE, "Minuto NO VÃLIDO por no estar en el rango 0-59"); |
|---|
| 290 | } |
|---|
| 291 | this.second = second; |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | /** |
|---|
| 295 | * Cambia el valor de la fecha por el valor indicado en el parámetro |
|---|
| 296 | * |
|---|
| 297 | * @param date la nueva fecha |
|---|
| 298 | */ |
|---|
| 299 | public void setDate(Date date) { |
|---|
| 300 | Calendar cal= Calendar.getInstance(); |
|---|
| 301 | cal.setTime(date); |
|---|
| 302 | this.year= cal.get(Calendar.YEAR); |
|---|
| 303 | this.month= cal.get(Calendar.MONTH)-1; |
|---|
| 304 | this.day= cal.get(Calendar.DAY_OF_MONTH); |
|---|
| 305 | this.hour= cal.get(Calendar.HOUR_OF_DAY); |
|---|
| 306 | this.minute= cal.get(Calendar.MINUTE); |
|---|
| 307 | this.second= cal.get(Calendar.SECOND); |
|---|
| 308 | } |
|---|
| 309 | |
|---|
| 310 | /** |
|---|
| 311 | * Obtiene como un objeto <code>java.util.Date</code> la fecha almacenada |
|---|
| 312 | * |
|---|
| 313 | * @return la fecha almacenada como un objeto <code>java.util.Date</code> |
|---|
| 314 | */ |
|---|
| 315 | public Date getDate() { |
|---|
| 316 | Calendar cal= Calendar.getInstance(); |
|---|
| 317 | cal.set(year, month+1, day+1, hour, minute, (int)second); |
|---|
| 318 | Date date = cal.getTime(); |
|---|
| 319 | return date; |
|---|
| 320 | } |
|---|
| 321 | |
|---|
| 322 | /** |
|---|
| 323 | * Determina si el objeto corresponde al tipo Date de TOL |
|---|
| 324 | * |
|---|
| 325 | * @return <code>true</code> ya que el objeto corresponde al tipo Date de TOL |
|---|
| 326 | */ |
|---|
| 327 | @Override |
|---|
| 328 | public boolean isDate() { |
|---|
| 329 | return true; |
|---|
| 330 | } |
|---|
| 331 | |
|---|
| 332 | /** |
|---|
| 333 | * Compara a este objeto con otro |
|---|
| 334 | * |
|---|
| 335 | * @param obj el objeto con el que se comparará |
|---|
| 336 | * @return <code>true</code> si el objeto pasado como parámetro es de tipo <code>TOLDate</code> y contiene una fecha idéntica |
|---|
| 337 | */ |
|---|
| 338 | @Override |
|---|
| 339 | public boolean equals(Object obj) { |
|---|
| 340 | if (obj == null) { |
|---|
| 341 | return false; |
|---|
| 342 | } |
|---|
| 343 | if (getClass() != obj.getClass()) { |
|---|
| 344 | return false; |
|---|
| 345 | } |
|---|
| 346 | final TOLDate other = (TOLDate) obj; |
|---|
| 347 | if (this.year != other.year || this.month!=other.month || this.day!=other.day || this.hour!=other.hour || this.minute!=other.minute || this.second!=other.second) { |
|---|
| 348 | return false; |
|---|
| 349 | } |
|---|
| 350 | return true; |
|---|
| 351 | } |
|---|
| 352 | |
|---|
| 353 | /** |
|---|
| 354 | * Calcula el código hash asignado al objeto |
|---|
| 355 | * |
|---|
| 356 | * @return el código hash asignado al objeto |
|---|
| 357 | */ |
|---|
| 358 | @Override |
|---|
| 359 | public int hashCode() { |
|---|
| 360 | int hash = 3; |
|---|
| 361 | hash = 31 * hash + this.year; |
|---|
| 362 | hash = 31 * hash + this.month; |
|---|
| 363 | hash = 31 * hash + this.day; |
|---|
| 364 | hash = 31 * hash + this.hour; |
|---|
| 365 | hash = 31 * hash + this.minute; |
|---|
| 366 | hash = 31 * hash + (int) (Double.doubleToLongBits(this.second) ^ (Double.doubleToLongBits(this.second) >>> 32)); |
|---|
| 367 | return hash; |
|---|
| 368 | } |
|---|
| 369 | |
|---|
| 370 | /** |
|---|
| 371 | * Obtiene una representación como cadena de texto de la fecha |
|---|
| 372 | * |
|---|
| 373 | * @return la representación como cadena de texto de la fecha |
|---|
| 374 | */ |
|---|
| 375 | @Override |
|---|
| 376 | public String toString() { |
|---|
| 377 | Calendar cal= Calendar.getInstance(); |
|---|
| 378 | cal.set(year, month-1, day, hour, minute, (int)second); |
|---|
| 379 | Date date = cal.getTime(); |
|---|
| 380 | DateFormat df= DateFormat.getDateTimeInstance(); |
|---|
| 381 | return df.format(date); |
|---|
| 382 | } |
|---|
| 383 | |
|---|
| 384 | } |
|---|