View Javadoc
1   /**
2    * Copyright 2004-2014 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  package org.kuali.kpme.core;
17  
18  import java.sql.Time;
19  
20  import org.joda.time.LocalTime;
21  import org.joda.time.format.DateTimeFormat;
22  import org.joda.time.format.DateTimeFormatter;
23  import org.kuali.rice.core.web.format.FormatException;
24  import org.kuali.rice.core.web.format.Formatter;
25  
26  public class SqlTimeFormatter extends Formatter {
27  
28  	private static final long serialVersionUID = -6187456990309386413L;
29  	
30  	private static final DateTimeFormatter sdFormat = DateTimeFormat.forPattern("hh:mm aa");
31  
32  	static {
33  		registerFormatter(Time.class, SqlTimeFormatter.class);
34  		
35  		//sdFormat.setLenient(false);
36  	}
37  
38  	/**
39      */
40  	@Override
41  	protected Object convertToObject(String source) {
42  		Object o = null;
43  
44  		try {
45              o = new Time(LocalTime.parse(source, sdFormat).toDateTimeToday().getMillis());
46  		} catch (Exception e) {
47  			throw new FormatException("parsing", "error.invalidTime", source, e);
48  		}
49  
50  		return o;
51  	}
52  
53  	@Override
54  	public Object format(Object source) {
55  		if (source != null && source instanceof Time) {
56  			Time time = (Time) source;
57  			return sdFormat.print(new LocalTime(time));
58  		}
59  
60  		return null;
61  	}
62  }