KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jical > DateTimeRange


1 /*
2  * Created on 31-Oct-2004
3  *
4  * Purpose is to get the date time range to use for a jical calc.
5  */

6 package org.jical;
7
8 import java.text.SimpleDateFormat JavaDoc;
9 import java.util.Date JavaDoc;
10 import java.util.logging.Logger JavaDoc;
11
12 /**
13  * @author sfg
14  *
15  * TODO To change the template for this generated type comment go to
16  * Window - Preferences - Java - Code Style - Code Templates
17  */

18 public class DateTimeRange {
19     Date JavaDoc dateFrom;
20     Date JavaDoc dateTo;
21     private static final SimpleDateFormat JavaDoc DATEONLY = new SimpleDateFormat JavaDoc("yyyyMMdd");
22     private static final SimpleDateFormat JavaDoc DATETIME = new SimpleDateFormat JavaDoc("yyyyMMddHHmmss");
23     private static String JavaDoc CURRENT = "CURRENT";
24
25     private Logger JavaDoc logger = Logger.getLogger(this.getClass().getName());
26     /*
27      * TODO Work out dateFrom dateTo and timeRange.
28      *
29      */

30     public void calcDateTimeRange(String JavaDoc dateRangeOrDaysForward, String JavaDoc timeRange)
31     {
32         dateFrom = null;
33         dateTo = null;
34         
35         // Allow CURRENT to fetch all calendar details up to today.
36
if (dateRangeOrDaysForward.equalsIgnoreCase(CURRENT))
37         {
38             dateFrom = new Date JavaDoc(1);
39             dateTo = new Date JavaDoc();
40         }
41         else
42             // Check for a range
43
if (dateRangeOrDaysForward.indexOf("-") != -1)
44             {
45                 try
46                 {
47                     dateFrom = getDateFrom(dateRangeOrDaysForward);
48                     dateFrom = getDateTo(dateRangeOrDaysForward);
49                 }
50                 catch (Exception JavaDoc 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 // Assume a 'days forward'.
59
{
60              long daysForward = getDaysForwardNumeric(dateRangeOrDaysForward);
61              calcDateRangeFromDaysForward(daysForward);
62         }
63     }
64     
65     public long getDaysForwardNumeric(String JavaDoc dateRangeOrDaysForward)
66     {
67         try
68         {
69             return java.lang.Integer.parseInt(dateRangeOrDaysForward);
70         }
71         catch (Exception JavaDoc e)
72         {
73             e.printStackTrace();
74             logger.severe("Really bad days forward parameter of "+dateRangeOrDaysForward+" caused exception: "+e);
75             // Allow proceed but with zero days forward.
76
return 0;
77         }
78         
79     }
80     private void calcDateRangeFromDaysForward(long daysForward)
81     {
82         Date JavaDoc dateFrom = new Date JavaDoc();
83         Date JavaDoc dateTo = getDateToFromDaysForward(daysForward);
84         /*
85          * Currently, keep this dateFrom/To as coarse. ie, dateFrom/To are DAYS not DAY/HH:MM
86          * ie for FROM date, set to time of 0000, for TO date set to time of 235959
87          */

88         try
89         {
90             dateFrom = (Date JavaDoc)DATEONLY.parse(DATEONLY.format(dateFrom));
91             dateTo = (Date JavaDoc)DATETIME.parse(DATEONLY.format(dateTo) + "235959");
92         }
93         catch (Exception JavaDoc e)
94         {
95             logger.severe("Error setting dates to process full day range." + e);
96         }
97     }
98     public Date JavaDoc getDateToFromDaysForward(long daysForward)
99     {
100         Date JavaDoc dateTo = new Date JavaDoc();
101         long rollMicroSecs = 86400000 * daysForward;
102         dateTo.setTime(dateTo.getTime() + (rollMicroSecs));
103         return dateTo;
104     }
105     /*
106      * This is not the optimal place for this but it works!
107      *
108      */

109     public Date JavaDoc getDateFrom(String JavaDoc dateRangeOrDaysForward) throws Exception JavaDoc
110     {
111         try
112         {
113             return (Date JavaDoc)DATETIME.parse(dateRangeOrDaysForward.substring(0,14));
114         }
115         catch (Exception JavaDoc e)
116         {
117             throw e;
118         }
119     }
120     public Date JavaDoc getDateTo(String JavaDoc dateRangeOrDaysForward) throws Exception JavaDoc
121     {
122         // Gets the From Date
123
try
124         {
125             return (Date JavaDoc)DATETIME.parse(dateRangeOrDaysForward.substring(15));
126         }
127         catch (Exception JavaDoc e)
128         {
129             throw e;
130         }
131     }
132 }
133
Popular Tags