001: /*
002: * Copyright 2004 Jonathan M. Lehr
003: *
004: * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
011: * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
012: * governing permissions and limitations under the License.
013: *
014: * MODIFIED BY THE KUALI FOUNDATION
015: */
016: // begin Kuali Foundation modification
017: package org.kuali.core.web.format;
018:
019: // end Kuali Foundation modification
020:
021: // begin Kuali Foundation modification
022: // import order changed, and java.util.Calendar, org.kuali.KeyConstants and org.kuali.rice.KNSServiceLocator added
023: import java.sql.Date;
024: import java.text.ParseException;
025: import java.util.Calendar;
026:
027: import org.kuali.RiceKeyConstants;
028: import org.kuali.rice.KNSServiceLocator;
029:
030: /**
031: * begin Kuali Foundation modification
032: * This class is used to format Date objects.
033: * end Kuali Foundation modification
034: */
035: public class DateFormatter extends Formatter {
036: // begin Kuali Foundation modification
037: // serialVersionUID changed from 1L
038: private static final long serialVersionUID = 7612442662886603084L;
039:
040: // end Kuali Foundation modification
041:
042: // begin Kuali Foundation modification
043: // static variables DATE_ERROR_KEY and PARSE_MSG removed
044: // method public String getErrorKey() removed
045: // end Kuali Foundation modification
046:
047: // begin Kuali Foundation modification
048: // added this method
049: /**
050: *
051: * For a given user input date, this method returns the exact string the user entered after the last slash. This allows the
052: * formatter to distinguish between ambiguous values such as "/06" "/6" and "/0006"
053: *
054: * @param date
055: * @return
056: */
057: private String verbatimYear(String date) {
058: String result = "";
059:
060: int pos = date.lastIndexOf("/");
061: if (pos >= 0) {
062: result = date.substring(pos);
063: }
064:
065: return result;
066: }
067:
068: // end Kuali Foundation modification
069:
070: /**
071: * Unformats its argument and return a java.util.Date instance initialized with the resulting string.
072: *
073: * @return a java.util.Date intialized with the provided string
074: */
075: protected Object convertToObject(String target) {
076: // begin Kuali Foundation modification
077: try {
078: Date result = KNSServiceLocator.getDateTimeService()
079: .convertToSqlDate(target);
080: Calendar calendar = Calendar.getInstance();
081: calendar.setTime(result);
082: if (calendar.get(Calendar.YEAR) < 1000
083: && verbatimYear(target).length() < 4) {
084: throw new FormatException("illegal year format",
085: RiceKeyConstants.ERROR_DATE, target);
086: }
087: return result;
088: } catch (ParseException e) {
089: throw new FormatException("parsing",
090: RiceKeyConstants.ERROR_DATE, target, e);
091: }
092: // end Kuali Foundation modification
093: }
094:
095: /**
096: * Returns a string representation of its argument, formatted as a date with the "MM/dd/yyyy" format.
097: *
098: * @return a formatted String
099: */
100: public Object format(Object value) {
101: if (value == null)
102: return null;
103: // begin Kuali Foundation modification
104: return KNSServiceLocator.getDateTimeService().toDateString(
105: (java.util.Date) value);
106: // end Kuali Foundation modification
107: }
108: }
|