01: package ru.emdev.EmForge.web.converter;
02:
03: import java.text.DateFormat;
04: import java.text.ParseException;
05: import java.text.SimpleDateFormat;
06: import java.util.Calendar;
07: import java.util.Date;
08:
09: import javax.faces.component.UIComponent;
10: import javax.faces.context.FacesContext;
11: import javax.faces.convert.Converter;
12:
13: /** Special convertor for converting only time part date
14: *
15: * @note I tried to use <f:convertDateTime pattern="h:mm a"/> but without success
16: * @author akakunin
17: *
18: */
19: public class EmForgeTimeConverter implements Converter {
20: private static final DateFormat m_df = new SimpleDateFormat(
21: "h:mm a");
22:
23: public Object getAsObject(FacesContext context,
24: UIComponent component, String value) {
25: if (value == "") {
26: return null;
27: }
28:
29: // Create empty date
30: Calendar calendar = Calendar.getInstance();
31: calendar.clear();
32:
33: try {
34: Date parsedDate = m_df.parse(value);
35: Calendar parsedCalendar = Calendar.getInstance();
36: parsedCalendar.setTime(parsedDate);
37:
38: // copy only hours, minutes
39: calendar.set(Calendar.HOUR, parsedCalendar
40: .get(Calendar.HOUR));
41: calendar.set(Calendar.MINUTE, parsedCalendar
42: .get(Calendar.MINUTE));
43:
44: return calendar.getTime();
45: } catch (ParseException pex) {
46: return null;
47: }
48: }
49:
50: public String getAsString(FacesContext context,
51: UIComponent component, Object value) {
52: if (value == null) {
53: return "";
54: }
55:
56: if (Date.class.isInstance(value)) {
57: Date date = (Date) value;
58: return m_df.format(date);
59: }
60:
61: return "";
62: }
63:
64: }
|