01: package ru.emdev.EmForge.web.converter;
02:
03: import java.util.Date;
04: import java.sql.Timestamp;
05:
06: import javax.faces.component.UIComponent;
07: import javax.faces.context.FacesContext;
08: import javax.faces.convert.Converter;
09: import javax.faces.convert.ConverterException;
10:
11: import org.apache.commons.lang.StringUtils;
12: import org.apache.commons.logging.Log;
13: import org.apache.commons.logging.LogFactory;
14:
15: public class CommentTimeConverter implements Converter {
16:
17: protected final Log logger = LogFactory.getLog(getClass());
18:
19: public Object getAsObject(FacesContext arg0, UIComponent arg1,
20: String arg2) throws ConverterException {
21: return null;
22: }
23:
24: public String getAsString(FacesContext arg0, UIComponent arg1,
25: Object arg2) throws ConverterException {
26: try {
27: if (arg2 instanceof Timestamp) {
28: return StringUtils.substringBeforeLast(
29: ((Timestamp) arg2).toString(), ":");
30: } else if (arg2 instanceof Date) {
31: String tmp = ((Date) arg2).toString();
32: String str = StringUtils.substringAfterLast(tmp, " ");
33: tmp = StringUtils.substringBeforeLast(tmp, ":");
34: tmp = StringUtils.substringAfter(tmp, " ");
35: String month = StringUtils.substringBefore(tmp, " ");
36:
37: if (month.equals("Jan")) {
38: month = "01";
39: } else if (month.equals("Feb")) {
40: month = "02";
41: } else if (month.equals("Mar")) {
42: month = "03";
43: } else if (month.equals("Apr")) {
44: month = "04";
45: } else if (month.equals("May")) {
46: month = "05";
47: } else if (month.equals("Jun")) {
48: month = "06";
49: } else if (month.equals("Jul")) {
50: month = "07";
51: } else if (month.equals("Aug")) {
52: month = "08";
53: } else if (month.equals("Sep")) {
54: month = "09";
55: } else if (month.equals("Oct")) {
56: month = "10";
57: } else if (month.equals("Nov")) {
58: month = "11";
59: } else {
60: month = "12";
61: }
62:
63: return str + "-" + month + "-"
64: + StringUtils.substringAfter(tmp, " ");
65: } else {
66: return null;
67: }
68: } catch (Exception ex) {
69: logger.error("Cannot format date:", ex);
70: return null;
71: }
72: }
73:
74: }
|