KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > format > DateHandler


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.format;
14
15 import java.text.ParsePosition JavaDoc;
16 import java.text.SimpleDateFormat JavaDoc;
17 import java.util.Date JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Locale JavaDoc;
20
21
22 /**
23  * @author av
24  *
25  * To change this generated comment edit the template variable "typecomment":
26  * Window>Preferences>Java>Templates.
27  * To enable and disable the creation of type comments go to
28  * Window>Preferences>Java>Code Generation.
29  */

30 public class DateHandler extends FormatHandlerSupport {
31
32   public String JavaDoc format(Object JavaDoc o, String JavaDoc userPattern) {
33     if (o == null) {
34       return "";
35     }
36
37     Locale JavaDoc locale = getLocale();
38     SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc(findPattern(userPattern), locale);
39
40     return sdf.format(o);
41   }
42
43   public Object JavaDoc parse(String JavaDoc s, String JavaDoc userPattern) throws FormatException {
44     if (s == null) {
45       return null;
46     }
47
48     s = s.trim();
49     if (s.length() == 0) {
50       return null;
51     }
52
53     SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc(findPattern(userPattern), getLocale());
54     ParsePosition JavaDoc pos = new ParsePosition JavaDoc(0);
55     Object JavaDoc o = sdf.parse(s, pos);
56
57     if ((o == null) || (pos.getIndex() != s.length())) {
58       throw new FormatException(getErrorMessage(s));
59     }
60
61     return o;
62   }
63   
64   public boolean canHandle(Object JavaDoc value) {
65     return value instanceof Date JavaDoc;
66   }
67  
68  
69   public Object JavaDoc toNativeArray(List JavaDoc list) {
70     Date JavaDoc[] array = new Date JavaDoc[list.size()];
71     for (int i = 0; i < array.length; i++)
72       array[i] = (Date JavaDoc)list.get(i);
73     return array;
74   }
75   
76   public Object JavaDoc[] toObjectArray(Object JavaDoc value) {
77     if (value instanceof Date JavaDoc)
78       return new Date JavaDoc[]{(Date JavaDoc)value};
79     return (Date JavaDoc[])value;
80   }
81   
82 }
Popular Tags