1 6 package org.jical; 7 8 import java.text.SimpleDateFormat ; 9 import java.util.Date ; 10 import java.util.logging.Logger ; 11 12 18 public class DateTimeRange { 19 Date dateFrom; 20 Date dateTo; 21 private static final SimpleDateFormat DATEONLY = new SimpleDateFormat ("yyyyMMdd"); 22 private static final SimpleDateFormat DATETIME = new SimpleDateFormat ("yyyyMMddHHmmss"); 23 private static String CURRENT = "CURRENT"; 24 25 private Logger logger = Logger.getLogger(this.getClass().getName()); 26 30 public void calcDateTimeRange(String dateRangeOrDaysForward, String timeRange) 31 { 32 dateFrom = null; 33 dateTo = null; 34 35 if (dateRangeOrDaysForward.equalsIgnoreCase(CURRENT)) 37 { 38 dateFrom = new Date (1); 39 dateTo = new Date (); 40 } 41 else 42 if (dateRangeOrDaysForward.indexOf("-") != -1) 44 { 45 try 46 { 47 dateFrom = getDateFrom(dateRangeOrDaysForward); 48 dateFrom = getDateTo(dateRangeOrDaysForward); 49 } 50 catch (Exception e) 51 { 52 logger.severe("Cannot read your datefrom/to parameter! RTFM. 14chars - 14 chars, exception:"+e); 53 dateFrom = null; 54 dateTo = null; 55 } 56 57 } 58 else { 60 long daysForward = getDaysForwardNumeric(dateRangeOrDaysForward); 61 calcDateRangeFromDaysForward(daysForward); 62 } 63 } 64 65 public long getDaysForwardNumeric(String dateRangeOrDaysForward) 66 { 67 try 68 { 69 return java.lang.Integer.parseInt(dateRangeOrDaysForward); 70 } 71 catch (Exception e) 72 { 73 e.printStackTrace(); 74 logger.severe("Really bad days forward parameter of "+dateRangeOrDaysForward+" caused exception: "+e); 75 return 0; 77 } 78 79 } 80 private void calcDateRangeFromDaysForward(long daysForward) 81 { 82 Date dateFrom = new Date (); 83 Date dateTo = getDateToFromDaysForward(daysForward); 84 88 try 89 { 90 dateFrom = (Date )DATEONLY.parse(DATEONLY.format(dateFrom)); 91 dateTo = (Date )DATETIME.parse(DATEONLY.format(dateTo) + "235959"); 92 } 93 catch (Exception e) 94 { 95 logger.severe("Error setting dates to process full day range." + e); 96 } 97 } 98 public Date getDateToFromDaysForward(long daysForward) 99 { 100 Date dateTo = new Date (); 101 long rollMicroSecs = 86400000 * daysForward; 102 dateTo.setTime(dateTo.getTime() + (rollMicroSecs)); 103 return dateTo; 104 } 105 109 public Date getDateFrom(String dateRangeOrDaysForward) throws Exception 110 { 111 try 112 { 113 return (Date )DATETIME.parse(dateRangeOrDaysForward.substring(0,14)); 114 } 115 catch (Exception e) 116 { 117 throw e; 118 } 119 } 120 public Date getDateTo(String dateRangeOrDaysForward) throws Exception 121 { 122 try 124 { 125 return (Date )DATETIME.parse(dateRangeOrDaysForward.substring(15)); 126 } 127 catch (Exception e) 128 { 129 throw e; 130 } 131 } 132 } 133 | Popular Tags |