KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > util > common > DateUtil


1 /*
2  * Coefficient - facilitates project based collaboration
3  * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
4  * PO Box 395
5  * Pretoria 0001, RSA
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package za.org.coefficient.util.common;
21
22 import java.io.Serializable JavaDoc;
23
24 import java.util.Date JavaDoc;
25 import java.util.GregorianCalendar JavaDoc;
26 import java.text.SimpleDateFormat JavaDoc;
27
28 import za.org.coefficient.core.Constants;
29
30 /**
31  * This is a utility class that manipulates dates
32  */

33 public class DateUtil implements Serializable JavaDoc {
34     //~ Static fields/initializers =============================================
35

36     public static final long DEF_SPREAD = 60 * 60 * 24 * 7 * 1000;
37     static String JavaDoc[] months =
38     {
39         "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
40         "Nov", "Dec"
41     };
42
43     //~ Methods ================================================================
44

45     public static String JavaDoc getFormattedDate() {
46         SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc(Constants.SYSTEM_DATE_FORMAT);
47         return sdf.format(new Date JavaDoc(System.currentTimeMillis()));
48     }
49
50     public static String JavaDoc getFormattedFutureDate() {
51         SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc(Constants.SYSTEM_DATE_FORMAT);
52         Date JavaDoc future = getFutureDate(DEF_SPREAD);
53         return sdf.format(future);
54     }
55
56     public static Date JavaDoc setDate(String JavaDoc year, String JavaDoc month, String JavaDoc day) {
57         int i;
58         int intYear;
59         int intMonth;
60         int intDay;
61
62         try {
63             intYear = Integer.parseInt(year);
64         } catch (Throwable JavaDoc t) {
65             intYear = 1900;
66         }
67
68         try {
69             intDay = Integer.parseInt(day);
70         } catch (Throwable JavaDoc t) {
71             intDay = 1;
72         }
73
74         intMonth = 0;
75         for (i = 0; i < months.length; i++) {
76             if (month.equals(months[i])) {
77                 intMonth = i;
78
79                 break;
80             }
81         }
82
83         GregorianCalendar JavaDoc gc = new GregorianCalendar JavaDoc(intYear, intMonth, intDay);
84
85         return gc.getTime();
86     }
87
88     public static String JavaDoc getDate(Date JavaDoc date) {
89         try {
90             GregorianCalendar JavaDoc gc = new GregorianCalendar JavaDoc();
91             gc.setTime(date);
92             String JavaDoc st =
93                 new String JavaDoc(gc.get(gc.DAY_OF_MONTH) + " "
94                     + months[gc.get(gc.MONTH)] + " " + gc.get(gc.YEAR));
95
96             return st;
97         } catch (Throwable JavaDoc t) {
98             return "bad date!";
99         }
100     }
101
102     public static String JavaDoc getDay(Date JavaDoc date) {
103         try {
104             GregorianCalendar JavaDoc gc = new GregorianCalendar JavaDoc();
105             gc.setTime(date);
106             String JavaDoc st = new String JavaDoc("" + gc.get(gc.DATE));
107
108             return st;
109         } catch (Throwable JavaDoc t) {
110             return "bad date!";
111         }
112     }
113
114     public static String JavaDoc getDefaultFutureDay() {
115         Date JavaDoc future = getFutureDate(DEF_SPREAD);
116
117         return getDay(future);
118     }
119
120     public static String JavaDoc getDefaultFutureMonth() {
121         Date JavaDoc future = getFutureDate(DEF_SPREAD);
122
123         return getMonth(future);
124     }
125
126     public static String JavaDoc getDefaultFutureYear() {
127         Date JavaDoc future = getFutureDate(DEF_SPREAD);
128
129         return getYear(future);
130     }
131
132     public static Date JavaDoc getFutureDate(long spread) {
133         GregorianCalendar JavaDoc gc = new GregorianCalendar JavaDoc();
134         Date JavaDoc future = gc.getTime();
135         long t = future.getTime();
136         future.setTime(t + spread);
137
138         return future;
139     }
140
141     public static String JavaDoc getMonth(Date JavaDoc date) {
142         try {
143             GregorianCalendar JavaDoc gc = new GregorianCalendar JavaDoc();
144             gc.setTime(date);
145             String JavaDoc st = new String JavaDoc(months[gc.get(gc.MONTH)]);
146
147             return st;
148         } catch (Throwable JavaDoc t) {
149             return "bad date!";
150         }
151     }
152
153     public static String JavaDoc getTodayDay() {
154         GregorianCalendar JavaDoc gc = new GregorianCalendar JavaDoc();
155
156         return getDay(gc.getTime());
157     }
158
159     public static String JavaDoc getTodayMonth() {
160         GregorianCalendar JavaDoc gc = new GregorianCalendar JavaDoc();
161
162         return getMonth(gc.getTime());
163     }
164
165     public static String JavaDoc getTodayYear() {
166         GregorianCalendar JavaDoc gc = new GregorianCalendar JavaDoc();
167
168         return getYear(gc.getTime());
169     }
170
171     public static String JavaDoc getYear(Date JavaDoc date) {
172         try {
173             GregorianCalendar JavaDoc gc = new GregorianCalendar JavaDoc();
174             gc.setTime(date);
175             String JavaDoc st = new String JavaDoc("" + gc.get(gc.YEAR));
176
177             return st;
178         } catch (Throwable JavaDoc t) {
179             return "bad date!";
180         }
181     }
182 }
183
Popular Tags