1 40 package org.dspace.content; 41 42 import java.util.Calendar ; 43 import java.util.Date ; 44 import java.util.GregorianCalendar ; 45 import java.util.TimeZone ; 46 47 import org.apache.log4j.Logger; 48 49 52 69 public class DCDate 70 { 71 72 private static Logger cat = Logger.getLogger(DCDate.class); 73 74 77 private final static String [] MONTHNAMES = { "January", "February", 78 "March", "April", "May", "June", "July", "August", "September", 79 "October", "November", "December" }; 80 81 82 private int year; 83 84 85 private int month; 86 87 88 private int day; 89 90 91 private int hours; 92 93 94 private int minutes; 95 96 97 private int seconds; 98 99 103 private GregorianCalendar localGC; 104 105 108 public DCDate() 109 { 110 year = month = day = hours = minutes = seconds = -1; 112 localGC = null; 113 } 114 115 121 public DCDate(String fromDC) 122 { 123 year = month = day = hours = minutes = seconds = -1; 125 localGC = null; 126 127 if ((fromDC == null) || fromDC.equals("")) 129 { 130 return; 131 } 132 133 try 134 { 135 switch (fromDC.length()) 136 { 137 case 20: 138 139 hours = Integer.parseInt(fromDC.substring(11, 13)); 141 minutes = Integer.parseInt(fromDC.substring(14, 16)); 142 seconds = Integer.parseInt(fromDC.substring(17, 19)); 143 144 case 10: 145 146 day = Integer.parseInt(fromDC.substring(8, 10)); 148 149 case 7: 150 151 month = Integer.parseInt(fromDC.substring(5, 7)); 153 154 case 4: 155 156 year = Integer.parseInt(fromDC.substring(0, 4)); 158 } 159 } 160 catch (NumberFormatException e) 161 { 162 cat.warn("Mangled date: " + fromDC + " Exception: " + e); 164 year = month = day = hours = minutes = seconds = -1; 165 } 166 } 167 168 174 public DCDate(Date date) 175 { 176 Calendar calendar = Calendar.getInstance(); 177 178 calendar.setTime(date); 179 180 setDateLocal(calendar.get(Calendar.YEAR), 182 183 calendar.get(Calendar.MONTH) + 1, calendar 186 .get(Calendar.DAY_OF_MONTH), calendar 187 .get(Calendar.HOUR_OF_DAY), calendar 188 .get(Calendar.MINUTE), calendar.get(Calendar.SECOND)); 189 } 190 191 196 public static DCDate getCurrent() 197 { 198 return (new DCDate(new Date ())); 199 } 200 201 206 public String toString() 207 { 208 StringBuffer sb = new StringBuffer (); 209 210 if (year > 0) 211 { 212 sb.append(year); 213 } 214 215 if (month > 0) 216 { 217 sb.append('-').append(fleshOut(month)); 218 } 219 220 if (day > 0) 221 { 222 sb.append('-').append(fleshOut(day)); 223 } 224 225 if (hours > 0) 226 { 227 sb.append("T").append(fleshOut(hours)).append(':').append( 228 fleshOut(minutes)).append(':').append(fleshOut(seconds)) 229 .append("Z"); 230 } 231 232 return (sb.toString()); 233 } 234 235 240 public Date toDate() 241 { 242 GregorianCalendar utcGC = new GregorianCalendar (TimeZone 243 .getTimeZone("UTC")); 244 245 utcGC.set(year, month - 1, day, hours, minutes, seconds); 246 247 return utcGC.getTime(); 248 } 249 250 268 public void setDateLocal(int yyyy, int mm, int dd, int hh, int mn, int ss) 269 { 270 year = month = day = hours = minutes = seconds = -1; 271 272 if (yyyy > 0) 273 { 274 year = yyyy; 275 } 276 else 277 { 278 return; 279 } 280 281 if (mm > 0) 282 { 283 month = mm; 284 } 285 else 286 { 287 return; 288 } 289 290 if (dd > 0) 291 { 292 day = dd; 293 } 294 else 295 { 296 return; 297 } 298 299 if (hh == -1) 300 { 301 return; 302 } 303 304 localGC = new GregorianCalendar (year, month - 1, day, hh, mn, ss); 306 307 GregorianCalendar utcGC = new GregorianCalendar (TimeZone 309 .getTimeZone("UTC")); 310 311 utcGC.setTime(localGC.getTime()); 312 313 year = utcGC.get(Calendar.YEAR); 314 315 month = utcGC.get(Calendar.MONTH) + 1; 317 day = utcGC.get(Calendar.DAY_OF_MONTH); 318 hours = utcGC.get(Calendar.HOUR_OF_DAY); 319 minutes = utcGC.get(Calendar.MINUTE); 320 seconds = utcGC.get(Calendar.SECOND); 321 } 322 323 329 private int[] getDateLocal() 330 { 331 if (hours == -1) 333 { 334 return new int[] { year, month, day, -1, -1, -1 }; 335 } 336 337 if (localGC == null) 339 { 340 GregorianCalendar utcGC = new GregorianCalendar (TimeZone 341 .getTimeZone("UTC")); 342 343 utcGC.set(year, month - 1, day, hours, minutes, seconds); 344 localGC = new GregorianCalendar (); 345 localGC.setTime(utcGC.getTime()); 346 } 347 348 return new int[] { localGC.get(Calendar.YEAR), 349 localGC.get(Calendar.MONTH) + 1, 350 localGC.get(Calendar.DAY_OF_MONTH), 351 localGC.get(Calendar.HOUR_OF_DAY), 352 localGC.get(Calendar.MINUTE), localGC.get(Calendar.SECOND) }; 353 } 354 355 360 public int getYear() 361 { 362 return (getDateLocal())[0]; 363 } 364 365 370 public int getMonth() 371 { 372 return (getDateLocal())[1]; 373 } 374 375 380 public int getDay() 381 { 382 return (getDateLocal())[2]; 383 } 384 385 390 public int getHour() 391 { 392 return (getDateLocal())[3]; 393 } 394 395 400 public int getMinute() 401 { 402 return (getDateLocal())[4]; 403 } 404 405 410 public int getSecond() 411 { 412 return (getDateLocal())[5]; 413 } 414 415 421 private int[] getDateGMT() 422 { 423 return new int[] { year, month, day, hours, minutes, seconds }; 424 } 425 426 431 public int getYearGMT() 432 { 433 return (getDateGMT())[0]; 434 } 435 436 441 public int getMonthGMT() 442 { 443 return (getDateGMT())[1]; 444 } 445 446 451 public int getDayGMT() 452 { 453 return (getDateGMT())[2]; 454 } 455 456 461 public int getHourGMT() 462 { 463 return (getDateGMT())[3]; 464 } 465 466 471 public int getMinuteGMT() 472 { 473 return (getDateGMT())[4]; 474 } 475 476 481 public int getSecondGMT() 482 { 483 return (getDateGMT())[5]; 484 } 485 486 493 private String fleshOut(int n) 494 { 495 if (n < 10) 496 { 497 return "0" + n; 498 } 499 else 500 { 501 return String.valueOf(n); 502 } 503 } 504 505 514 public static String getMonthName(int m) 515 { 516 if ((m > 0) && (m < 13)) 517 { 518 return MONTHNAMES[m - 1]; 519 } 520 else 521 { 522 return "Unspecified"; 523 } 524 } 525 } 526 | Popular Tags |