001 /**
002 * Copyright 2004-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.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.kpme.core;
017
018 import java.sql.Time;
019
020 import org.joda.time.LocalTime;
021 import org.joda.time.format.DateTimeFormat;
022 import org.joda.time.format.DateTimeFormatter;
023 import org.kuali.rice.core.web.format.FormatException;
024 import org.kuali.rice.core.web.format.Formatter;
025
026 public class SqlTimeFormatter extends Formatter {
027
028 private static final long serialVersionUID = -6187456990309386413L;
029
030 private static final DateTimeFormatter sdFormat = DateTimeFormat.forPattern("hh:mm aa");
031
032 static {
033 registerFormatter(Time.class, SqlTimeFormatter.class);
034
035 //sdFormat.setLenient(false);
036 }
037
038 /**
039 */
040 @Override
041 protected Object convertToObject(String source) {
042 Object o = null;
043
044 try {
045 o = new Time(LocalTime.parse(source, sdFormat).toDateTimeToday().getMillis());
046 } catch (Exception e) {
047 throw new FormatException("parsing", "error.invalidTime", source, e);
048 }
049
050 return o;
051 }
052
053 @Override
054 public Object format(Object source) {
055 if (source != null && source instanceof Time) {
056 Time time = (Time) source;
057 return sdFormat.print(new LocalTime(time));
058 }
059
060 return null;
061 }
062 }