001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: /*
017: * Created on Apr 6, 2004
018: *
019: * To change the template for this generated file go to
020: * Window>Preferences>Java>Code Generation>Code and Comments
021: */
022: package org.kuali.module.pdp.utilities;
023:
024: import java.text.ParseException;
025: import java.text.SimpleDateFormat;
026:
027: import org.apache.struts.action.ActionErrors;
028: import org.apache.struts.action.ActionMessage;
029:
030: /**
031: * Title: PS Purchasing Web Application Description: Purchasing Web Application Copyright: Copyright (c) 2001 Company: Indiana
032: * University
033: *
034: * @author Ailish Byrne
035: * @version 1.0
036: */
037:
038: public class DateHandler {
039: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
040: .getLogger(DateHandler.class);
041:
042: private static final String DATEFORMAT = "MM/dd/yyyy";
043: private static final String DATEFORMAT2 = "dd-MMM-yyyy hh:mm:ss";
044:
045: public DateHandler() {
046: }
047:
048: public static String getTodayString() {
049: return (new SimpleDateFormat(DateHandler.DATEFORMAT))
050: .format(new java.util.Date());
051: }
052:
053: public static java.sql.Timestamp getTodayTimestamp() {
054: return new java.sql.Timestamp((new java.util.Date()).getTime());
055: }
056:
057: public static java.sql.Date getTodaySqlDate() {
058: return new java.sql.Date((new java.util.Date()).getTime());
059: }
060:
061: public static String makeTimestampString(
062: java.sql.Timestamp timestamp) {
063: return (new SimpleDateFormat(DateHandler.DATEFORMAT))
064: .format(new java.util.Date(timestamp.getTime()));
065: }
066:
067: public static String makeSqlDateString(java.sql.Date date) {
068: return (new SimpleDateFormat(DateHandler.DATEFORMAT))
069: .format(new java.util.Date(date.getTime()));
070: }
071:
072: public static String sqlDateToString(java.sql.Date date) {
073: return (new SimpleDateFormat(DateHandler.DATEFORMAT2))
074: .format(new java.util.Date(date.getTime()));
075: }
076:
077: public static String makeDateString(java.util.Date date) {
078: return (new SimpleDateFormat(DateHandler.DATEFORMAT))
079: .format(date);
080: }
081:
082: public static java.sql.Timestamp makeStringTimestamp(String string)
083: throws Exception {
084: java.sql.Timestamp timestamp = null;
085: if ((string != null) && !(string.trim().equals(""))) {
086: try {
087: timestamp = new java.sql.Timestamp(
088: ((new SimpleDateFormat(DateHandler.DATEFORMAT))
089: .parse(string)).getTime());
090: } catch (ParseException pE) {
091: throw new Exception(pE.getMessage());
092: }
093: }
094: return timestamp;
095: }
096:
097: public static java.sql.Date makeStringSqlDate(String string)
098: throws Exception {
099: java.sql.Date date = null;
100: if ((string != null) && !(string.trim().equals(""))) {
101: try {
102: date = new java.sql.Date(((new SimpleDateFormat(
103: DateHandler.DATEFORMAT)).parse(string))
104: .getTime());
105: } catch (ParseException pE) {
106: throw new Exception(pE.getMessage());
107: }
108: }
109: return date;
110: }
111:
112: public static java.sql.Date makeStringFormat2ToSqlDate(String string) {
113: java.sql.Date date = null;
114: if ((string != null) && !(string.trim().equals(""))) {
115: try {
116: long val = ((new SimpleDateFormat(
117: DateHandler.DATEFORMAT2)).parse(string))
118: .getTime();
119: date = new java.sql.Date(val);
120: } catch (ParseException pE) {
121: return null;
122: }
123: }
124: return date;
125: }
126:
127: public static java.util.Date makeStringDate(String string)
128: throws Exception {
129: java.util.Date date;
130: try {
131: date = (new SimpleDateFormat(DateHandler.DATEFORMAT))
132: .parse(string);
133: } catch (ParseException pE) {
134: throw new Exception(pE.getMessage());
135: }
136: return date;
137: }
138:
139: public static java.sql.Date utilDate2sqlDate(java.util.Date uDate) {
140: if (uDate == null) {
141: return null;
142: }
143: return new java.sql.Date(uDate.getTime());
144: }
145:
146: /**
147: * Converts a String to an int.
148: */
149: public static int convertToNbr(String myString) throws Exception {
150: // convert the String to an Integer
151: Integer myInt = new Integer(0);
152: int myNbr = 0;
153: try {
154: // myNbr = myInt.parseInt(myString);
155: myNbr = Integer.parseInt(myString);
156: } catch (NumberFormatException e) {
157: throw new Exception("You must use integers: mm/dd/yyyy.");
158: }
159: return myNbr;
160: }
161:
162: /**
163: * Takes a variable name and a date String, and an ActionErrors object and returns the ActionErrors object, containing errors
164: * detected during date validation. This method should be called from the validate method of your forms.
165: */
166: public static ActionErrors validDate(ActionErrors errors,
167: String variableName, String date) {
168: date = date.trim();
169:
170: int firstSlash = date.indexOf("/");
171: int secondSlash = date.indexOf("/", firstSlash + 1);
172:
173: // format validation
174: // check for slashes
175: if (firstSlash <= 0 && secondSlash <= 0) {
176: errors.add(variableName, new ActionMessage(
177: "error.blank.br", "Invalid Date: mm/dd/yyyy."));
178: } else if (date.length() - secondSlash != 5) {
179: // validate a 4 digit year and use of '/'
180: errors.add(variableName, new ActionMessage(
181: "error.blank.br", "Invalid Date: mm/dd/yyyy."));
182: }
183: // validate a 1 or 2 digit month
184: if (firstSlash > 2 || firstSlash == 0) {
185: errors.add(variableName, new ActionMessage(
186: "error.blank.br", "Invalid Month: mm/dd/yyyy."));
187: }
188: // validate a 1 or 2 digit day
189: if (secondSlash - firstSlash > 3) {
190: errors.add(variableName, new ActionMessage(
191: "error.blank.br", "Invalid Day: mm/dd/yyyy."));
192: }
193:
194: // check the validity of the day, month, and year integers
195: if (firstSlash > 0 && secondSlash > 0) {
196: String month = date.substring(0, firstSlash);
197: String day = date.substring(firstSlash + 1, secondSlash);
198: String year = date.substring(secondSlash + 1);
199:
200: int monthNbr = 0;
201: int dayNbr = 0;
202: int yearNbr = 0;
203:
204: // Convert a String of Date into integer numbers for month, day, and year
205: try {
206: monthNbr = convertToNbr(month);
207: dayNbr = convertToNbr(day);
208: yearNbr = convertToNbr(year);
209: } catch (Exception aE) {
210: errors.add(variableName, new ActionMessage(
211: "error.blank.br",
212: "You have an invalid number in a date field"));
213: }
214:
215: // check year for positive, non-zero digit
216: if (yearNbr < 1) {
217: errors
218: .add(variableName, new ActionMessage(
219: "error.blank.br",
220: "Invalid Year: mm/dd/yyyy."));
221: }
222: // check month for digit between 1 and 12
223: if (monthNbr < 1 || monthNbr > 12) {
224: errors.add(variableName, new ActionMessage(
225: "error.blank.br",
226: "Invalid Month 1-12: mm/dd/yyyy."));
227: }
228:
229: // check for february
230: if (monthNbr == 2) {
231: // check for leap year
232: if (yearNbr % 400 == 0
233: || (yearNbr % 100 != 0 && yearNbr % 4 == 0)) {
234: // february with leap year
235: if (dayNbr < 1 || dayNbr > 29) {
236: errors.add(variableName, new ActionMessage(
237: "error.blank.br",
238: "Invalid Day 1-29: mm/dd/yyyy."));
239: }
240: } else {
241: // february without leap year
242: if (dayNbr < 1 || dayNbr > 28) {
243: errors.add(variableName, new ActionMessage(
244: "error.blank.br",
245: "Invalid Day 1-28: mm/dd/yyyy."));
246: }
247: }
248: }
249: // months with 30 days
250: else if (monthNbr == 4 || monthNbr == 6 || monthNbr == 9
251: || monthNbr == 11) {
252: if (dayNbr < 1 || dayNbr > 30) {
253: errors.add(variableName, new ActionMessage(
254: "error.blank.br",
255: "Invalid Day 1-30: mm/dd/yyyy."));
256: }
257: }
258: // months with 31 days
259: else {
260: if (dayNbr < 1 || dayNbr > 31) {
261: errors.add(variableName, new ActionMessage(
262: "error.blank.br",
263: "Invalid Day 1-31: mm/dd/yyyy."));
264: }
265: }
266: }
267: return errors;
268: }
269:
270: public static String getDateAsString(String date) {
271: int firstSlash = date.indexOf("/");
272: int secondSlash = date.indexOf("/", firstSlash + 1);
273: int month = Integer.parseInt(date.substring(0, firstSlash));
274: int day = Integer.parseInt(date.substring(firstSlash + 1,
275: secondSlash));
276: int year = Integer.parseInt(date.substring(secondSlash + 1));
277:
278: switch (month) {
279: case 1:
280: return "January " + day + ", " + year;
281: case 2:
282: return "February " + day + ", " + year;
283: case 3:
284: return "March " + day + ", " + year;
285: case 4:
286: return "April " + day + ", " + year;
287: case 5:
288: return "May " + day + ", " + year;
289: case 6:
290: return "June " + day + ", " + year;
291: case 7:
292: return "July " + day + ", " + year;
293: case 8:
294: return "August " + day + ", " + year;
295: case 9:
296: return "September " + day + ", " + year;
297: case 10:
298: return "October " + day + ", " + year;
299: case 11:
300: return "November " + day + ", " + year;
301: case 12:
302: return "December " + day + ", " + year;
303: default:
304: return "Date unknown";
305: }
306: }
307: }
|