1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
19 package org.apache.commons.betwixt.introspection;
20
21 import java.text.DateFormat;
22 import java.text.SimpleDateFormat;
23 import java.util.Date;
24 import java.util.HashMap;
25 import java.util.Locale;
26 import java.util.Map;
27
28 /**
29 * @author <a href='http://commons.apache.org/'>Apache Commons Team</a>
30 * @version $Revision: 561314 $
31 */
32 public class DateFormatterBean {
33
34 private Map formatsByLocale = new HashMap();
35
36 //TODO: want to be able to match iterators from maps...
37 // but this mean ammending the descriptors when the property type
38 // is discovered
39 public Map getFormats() {
40 return formatsByLocale;
41 }
42
43 //TODO: should be able to use another verb eg register
44 //TODO: support for specifying adders in the dot betwixt file
45 //TODO: support for Simple types so that we can use Locale -> String
46 public void addFormat(Locale locale, SimpleDateFormat format) {
47 formatsByLocale.put(locale, format);
48 }
49
50 public String format(Date date, Locale locale) {
51 String result = "";
52 SimpleDateFormat simpleDateFormat = (SimpleDateFormat) formatsByLocale.get(locale);
53 if (simpleDateFormat == null)
54 {
55 result = DateFormat.getDateInstance(DateFormat.SHORT, locale).format(date);
56 }
57 else
58 {
59 result = simpleDateFormat.format(date);
60 }
61 return result;
62 }
63
64 }