| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| TimestampAMPMFormatter |
|
| 5.0;5 |
| 1 | /* | |
| 2 | * Copyright 2006-2007 The Kuali Foundation | |
| 3 | * | |
| 4 | * Licensed under the Educational Community License, Version 2.0 (the "License"); | |
| 5 | * you may not use this file except in compliance with the License. | |
| 6 | * You may obtain a copy of the License at | |
| 7 | * | |
| 8 | * http://www.opensource.org/licenses/ecl2.php | |
| 9 | * | |
| 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.rice.kns.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.rice.kns.service.KNSServiceLocator; | |
| 24 | import org.kuali.rice.kns.util.RiceKeyConstants; | |
| 25 | ||
| 26 | /** | |
| 27 | * This class is used to format timestamp objects. | |
| 28 | */ | |
| 29 | ||
| 30 | 0 | 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 | 0 | return KNSServiceLocator.getDateTimeService().convertToSqlTimestamp(target); |
| 41 | } | |
| 42 | 0 | catch (ParseException e) { |
| 43 | 0 | throw new FormatException("parsing", RiceKeyConstants.ERROR_DATE_TIME, target, e); |
| 44 | } | |
| 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 | 0 | if (value == null) { |
| 55 | 0 | return null; |
| 56 | } | |
| 57 | 0 | if (value instanceof String && StringUtils.isEmpty((String) value)) { |
| 58 | 0 | return null; |
| 59 | } | |
| 60 | 0 | return KNSServiceLocator.getDateTimeService().toDateTimeString((Date)value); |
| 61 | } | |
| 62 | } |