KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > betwixt > introspection > DateFormatterBean


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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
17
18 package org.apache.commons.betwixt.introspection;
19
20 import java.text.DateFormat JavaDoc;
21 import java.text.SimpleDateFormat JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Locale JavaDoc;
25 import java.util.Map JavaDoc;
26
27 /**
28  * @author <a HREF='http://jakarta.apache.org/'>Jakarta Commons Team</a>
29  * @version $Revision: 1.2 $
30  */

31 public class DateFormatterBean {
32     
33     private Map JavaDoc formatsByLocale = new HashMap JavaDoc();
34     
35     //TODO: want to be able to match iterators from maps...
36
// but this mean ammending the descriptors when the property type
37
// is discovered
38
public Map JavaDoc getFormats() {
39         return formatsByLocale;
40     }
41     
42     //TODO: should be able to use another verb eg register
43
//TODO: support for specifying adders in the dot betwixt file
44
//TODO: support for Simple types so that we can use Locale -> String
45
public void addFormat(Locale JavaDoc locale, SimpleDateFormat JavaDoc format) {
46         formatsByLocale.put(locale, format);
47     }
48     
49     public String JavaDoc format(Date JavaDoc date, Locale JavaDoc locale) {
50         String JavaDoc result = "";
51         SimpleDateFormat JavaDoc simpleDateFormat = (SimpleDateFormat JavaDoc) formatsByLocale.get(locale);
52         if (simpleDateFormat == null)
53         {
54              result = DateFormat.getDateInstance(DateFormat.SHORT, locale).format(date);
55         }
56         else
57         {
58             result = simpleDateFormat.format(date);
59         }
60         return result;
61     }
62     
63 }
64
Popular Tags