001: /*
002: * Copyright 2005-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: package org.kuali.core.service.impl;
017:
018: import java.sql.Timestamp;
019: import java.text.DateFormat;
020: import java.text.ParseException;
021: import java.text.SimpleDateFormat;
022: import java.util.Calendar;
023: import java.util.Date;
024:
025: import org.apache.commons.lang.StringUtils;
026: import org.apache.commons.lang.time.DurationFormatUtils;
027: import org.kuali.core.service.DateTimeService;
028: import org.springframework.transaction.annotation.Transactional;
029:
030: /**
031: * This class is the service implementation for a DateTime structure. This is
032: * the default, Kuali delivered implementation.
033: */
034: @Transactional
035: public class DateTimeServiceImpl implements DateTimeService {
036: private String[] sqlDateFormats;
037:
038: private String stringDateFormat;
039:
040: private String stringDateTimeFormat;
041:
042: /**
043: * @see org.kuali.core.service.DateTimeService#toDateString(java.util.Date)
044: */
045: public String toDateString(Date date) {
046: return toString(date, stringDateFormat);
047: }
048:
049: /**
050: * @see org.kuali.core.service.DateTimeService#toDateTimeString(java.util.Date)
051: */
052: public String toDateTimeString(Date date) {
053: return toString(date, stringDateTimeFormat);
054: }
055:
056: /**
057: * @see org.kuali.core.service.DateTimeService#toString(java.util.Date,
058: * java.lang.String)
059: */
060: public String toString(Date date, String pattern) {
061: DateFormat dateFormat = new SimpleDateFormat(pattern);
062: dateFormat.setLenient(false);
063: return dateFormat.format(date);
064: }
065:
066: /**
067: * @see org.kuali.core.service.DateTimeService#getCurrentDate()
068: */
069: public Date getCurrentDate() {
070: Calendar c = Calendar.getInstance();
071: c.setTime(new Date());
072: return c.getTime();
073: }
074:
075: /**
076: * @see org.kuali.core.service.DateTimeService#getCurrentTimestamp()
077: */
078: public Timestamp getCurrentTimestamp() {
079: return new java.sql.Timestamp(getCurrentDate().getTime());
080: }
081:
082: /**
083: * @see org.kuali.core.service.DateTimeService#getCurrentSqlDate()
084: */
085: public java.sql.Date getCurrentSqlDate() {
086: return new java.sql.Date(getCurrentDate().getTime());
087: }
088:
089: /**
090: * @see org.kuali.core.service.DateTimeService#getCurrentSqlDateMidnight()
091: */
092: public java.sql.Date getCurrentSqlDateMidnight() {
093: // simple and not unduely inefficient way to truncate the time component
094: return java.sql.Date.valueOf(getCurrentSqlDate().toString());
095: }
096:
097: /**
098: * @see org.kuali.core.service.DateTimeService#getCurrentCalendar()
099: */
100: public Calendar getCurrentCalendar() {
101: return getCalendar(getCurrentDate());
102: }
103:
104: /**
105: * @see org.kuali.core.service.DateTimeService#getCalendar
106: */
107: public Calendar getCalendar(Date date) {
108: if (date == null) {
109: throw new IllegalArgumentException("invalid (null) date");
110: }
111:
112: Calendar currentCalendar = Calendar.getInstance();
113: currentCalendar.setTime(date);
114:
115: return currentCalendar;
116: }
117:
118: /**
119: * @see org.kuali.core.service.DateTimeService#convertToDate(java.lang.String)
120: */
121: public Date convertToDate(String dateString) throws ParseException {
122: return parse(dateString, stringDateFormat);
123: }
124:
125: /**
126: * @see org.kuali.core.service.DateTimeService#convertToDateTime(java.lang.String)
127: */
128: public Date convertToDateTime(String dateTimeString)
129: throws ParseException {
130: return parse(dateTimeString, stringDateTimeFormat);
131: }
132:
133: /**
134: * @see org.kuali.core.service.DateTimeService#convertToSqlTimestamp(java.lang.String)
135: */
136: public java.sql.Timestamp convertToSqlTimestamp(String timeString)
137: throws ParseException {
138: if (!StringUtils.isBlank(timeString)) {
139: return new java.sql.Timestamp(parse(timeString,
140: stringDateTimeFormat).getTime());
141: }
142: return null;
143: }
144:
145: /**
146: * @see org.kuali.core.service.DateTimeService#convertToSqlDate(java.lang.String)
147: */
148: public java.sql.Date convertToSqlDate(String dateString)
149: throws ParseException {
150: if (StringUtils.isBlank(dateString)) {
151: throw new IllegalArgumentException(
152: "invalid (blank) timeString");
153: }
154: dateString = dateString.trim();
155: java.sql.Date date = null;
156: StringBuffer exceptionMessage = new StringBuffer(
157: "Date string '")
158: .append(dateString)
159: .append(
160: "' could not be converted using any of the accepted formats: ");
161: for (String dateFormatString : sqlDateFormats) {
162: try {
163: return new java.sql.Date(parse(dateString,
164: dateFormatString).getTime());
165: } catch (ParseException e) {
166: exceptionMessage.append(dateFormatString).append(
167: " (error offset=").append(e.getErrorOffset())
168: .append("),");
169: }
170: }
171: if (date == null) {
172: throw new ParseException(exceptionMessage.toString()
173: .substring(0, exceptionMessage.length() - 1), 0);
174: }
175: return date;
176: }
177:
178: /**
179: * @throws ParseException
180: * @see org.kuali.core.service.DateTimeService#convertToSqlDate(java.sql.Timestamp)
181: */
182: public java.sql.Date convertToSqlDate(Timestamp timestamp)
183: throws ParseException {
184: return new java.sql.Date(timestamp.getTime());
185: }
186:
187: public int dateDiff(Date startDate, Date endDate, boolean inclusive) {
188: Calendar startDateCalendar = Calendar.getInstance();
189: startDateCalendar.setTime(startDate);
190:
191: Calendar endDateCalendar = Calendar.getInstance();
192: endDateCalendar.setTime(endDate);
193:
194: int startDateOffset = -(startDateCalendar
195: .get(Calendar.ZONE_OFFSET) + startDateCalendar
196: .get(Calendar.DST_OFFSET))
197: / (60 * 1000);
198:
199: int endDateOffset = -(endDateCalendar.get(Calendar.ZONE_OFFSET) + endDateCalendar
200: .get(Calendar.DST_OFFSET))
201: / (60 * 1000);
202:
203: if (startDateOffset > endDateOffset) {
204: startDateCalendar.add(Calendar.MINUTE, endDateOffset
205: - startDateOffset);
206: }
207:
208: if (inclusive) {
209: startDateCalendar.add(Calendar.DATE, -1);
210: }
211:
212: int dateDiff = Integer.parseInt(DurationFormatUtils
213: .formatDuration(endDateCalendar.getTimeInMillis()
214: - startDateCalendar.getTimeInMillis(), "d",
215: true));
216:
217: return dateDiff;
218: }
219:
220: private Date parse(String dateString, String pattern)
221: throws ParseException {
222: if (!StringUtils.isBlank(dateString)) {
223: DateFormat dateFormat = new SimpleDateFormat(pattern);
224: dateFormat.setLenient(false);
225: return dateFormat.parse(dateString);
226: }
227: return null;
228: }
229:
230: /**
231: * @param sqlDateFormats
232: * the sqlDateFormats to set
233: */
234: public void setSqlDateFormats(String[] sqlDateFormats) {
235: this .sqlDateFormats = sqlDateFormats;
236: }
237:
238: /**
239: * @param stringDateFormat
240: * the stringDateFormat to set
241: */
242: public void setStringDateFormat(String stringDateFormat) {
243: this .stringDateFormat = stringDateFormat;
244: }
245:
246: /**
247: * @param stringDateTimeFormat
248: * the stringDateTimeFormat to set
249: */
250: public void setStringDateTimeFormat(String stringDateTimeFormat) {
251: this.stringDateTimeFormat = stringDateTimeFormat;
252: }
253: }
|