KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nl > hippo > cms > workflows > shared > DateUtil


1 package nl.hippo.cms.workflows.shared;
2
3 import java.util.Calendar JavaDoc;
4 import java.util.Date JavaDoc;
5 import java.util.Map JavaDoc;
6
7 public class DateUtil
8 {
9
10     private DateUtil()
11     {
12         super();
13     }
14
15     private static void clearTime(Calendar JavaDoc date)
16     {
17         date.set(Calendar.HOUR_OF_DAY, 0);
18         date.set(Calendar.MINUTE, 0);
19         date.set(Calendar.SECOND, 0);
20         date.set(Calendar.MILLISECOND, 0);
21     }
22
23     public static Date JavaDoc createToday()
24     {
25         Calendar JavaDoc today = Calendar.getInstance();
26         clearTime(today);
27         return today.getTime();
28     }
29
30     public static Date JavaDoc create(Date JavaDoc date)
31     {
32         Calendar JavaDoc result = Calendar.getInstance();
33         result.setTime(date);
34         clearTime(result);
35         return result.getTime();
36     }
37
38     public static boolean isBeforeOrEqualToToday(Date JavaDoc date)
39     {
40         Date JavaDoc today = createToday();
41         return !date.after(today);
42     }
43
44     /**
45      * Compare two dates and return whether first is after or equal to second. If second is not give (i.e. it is null)
46      * then it is assumed that first is not after or equal to second.
47      *
48      * @param first The date to compare. Cannot be null.
49      * @param second The date to compare to. Can be null.
50      * @return true if second does not equal null and first is after or equal to second, false otherwise.
51      */

52     public static boolean isAfterOrEquals(Date JavaDoc first, Date JavaDoc second) {
53         return !isBefore(first, second);
54     }
55
56     /**
57      * Compare two dates and return whether first is before second. If second is not give (i.e. it is null)
58      * then it is assumed that first is before second.
59      *
60      * @param first The date to compare. Cannot be null.
61      * @param second The date to compare to. Can be null.
62      * @return true if second equals null or first is before second, false otherwise.
63      */

64     public static boolean isBefore(Date JavaDoc first, Date JavaDoc second) {
65         return second == null || first.before(second);
66     }
67
68     public static boolean isBefore(Date JavaDoc unpublicationDate, String JavaDoc publicationDateMode, Date JavaDoc publicationDate)
69     {
70         Date JavaDoc unpublicationDateWithoutTime = create(unpublicationDate);
71         Date JavaDoc publicationDateWithoutTime = createToday();
72         if (!publicationDateMode.equals("now")) {
73             publicationDateWithoutTime = create(publicationDate);
74         }
75         return unpublicationDateWithoutTime.before(publicationDateWithoutTime);
76     }
77
78     public static boolean isBeforeWithTime(Date JavaDoc unpublicationDate, String JavaDoc publicationDateMode, Date JavaDoc publicationDate)
79     {
80         Date JavaDoc effectivePublicationDate = new Date JavaDoc();
81         if (!publicationDateMode.equals("now")) {
82             effectivePublicationDate = publicationDate;
83         }
84         return unpublicationDate.before(effectivePublicationDate);
85     }
86
87     public static Date JavaDoc getPublicationDate(Map JavaDoc transientVars)
88     {
89         return getPublicationDate(transientVars, "");
90     }
91
92     public static Date JavaDoc getPublicationDate(Map JavaDoc transientVars, String JavaDoc prefix)
93     {
94         Date JavaDoc result;
95         
96         String JavaDoc publicationDateMode = (String JavaDoc) transientVars.get(prefix + "publicationDateMode");
97         if (publicationDateMode.equals("now"))
98         {
99             result = DateUtil.createToday();
100         }
101         else
102         {
103             result = WorkflowExtensionUtil.getDateAndTime(transientVars.get(prefix + "publicationDate"));
104         }
105         
106         return result;
107     }
108
109     public static Date JavaDoc getUnpublicationDate(Map JavaDoc transientVars)
110     {
111         return getUnpublicationDate(transientVars, "");
112     }
113
114     public static Date JavaDoc getUnpublicationDate(Map JavaDoc transientVars, String JavaDoc prefix)
115     {
116         Date JavaDoc result;
117         
118         String JavaDoc unpublicationDateMode = (String JavaDoc) transientVars.get(prefix + "unpublicationDateMode");
119         if (unpublicationDateMode.equals("never"))
120         {
121             result = null;
122         }
123         else
124         {
125             result = WorkflowExtensionUtil.getDateAndTime(transientVars.get(prefix + "unpublicationDate"));
126         }
127         
128         return result;
129     }
130
131 }
132
Popular Tags