01: /*
02: * Copyright 2006 The Kuali Foundation.
03: *
04: * Licensed under the Educational Community License, Version 1.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.opensource.org/licenses/ecl1.php
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.kuali.core.web.format;
18:
19: import java.text.ParseException;
20: import java.util.Date;
21:
22: import org.apache.commons.lang.StringUtils;
23: import org.kuali.RiceKeyConstants;
24: import org.kuali.rice.KNSServiceLocator;
25:
26: /**
27: * This class is used to format timestamp objects.
28: */
29:
30: public class TimestampAMPMFormatter extends Formatter {
31: private static final long serialVersionUID = 7612442662886603084L;
32:
33: /**
34: * Unformats its argument and return a java.util.Date instance initialized with the resulting string.
35: *
36: * @return a java.util.Date intialized with the provided string
37: */
38: public Object convertToObject(String target) {
39: try {
40: return KNSServiceLocator.getDateTimeService()
41: .convertToSqlTimestamp(target);
42: } catch (ParseException e) {
43: throw new FormatException("parsing",
44: RiceKeyConstants.ERROR_DATE_TIME, target, e);
45: }
46: }
47:
48: /**
49: * Returns a string representation of its argument, formatted as a date with the "MM/dd/yyyy h:mm a" format.
50: *
51: * @return a formatted String
52: */
53: public Object format(Object value) {
54: if (value == null) {
55: return null;
56: }
57: if (value instanceof String
58: && StringUtils.isEmpty((String) value)) {
59: return null;
60: }
61: return KNSServiceLocator.getDateTimeService().toDateTimeString(
62: (Date) value);
63: }
64: }
|