]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/classes/core/src/com/ibm/icu/impl/duration/impl/YMDDateFormatter.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / classes / core / src / com / ibm / icu / impl / duration / impl / YMDDateFormatter.java
1 /*\r
2 ******************************************************************************\r
3 * Copyright (C) 2007-2008, International Business Machines Corporation and   *\r
4 * others. All Rights Reserved.                                               *\r
5 ******************************************************************************\r
6 */\r
7 \r
8 package com.ibm.icu.impl.duration.impl;\r
9 \r
10 import  com.ibm.icu.impl.duration.DateFormatter;\r
11 \r
12 import java.text.SimpleDateFormat;\r
13 import java.util.Date;\r
14 import java.util.Locale;\r
15 import java.util.TimeZone;\r
16 \r
17 /**\r
18  * A DateFormatter that formats the requested date fields.\r
19  */\r
20 public class YMDDateFormatter implements DateFormatter {\r
21   private String requestedFields;\r
22   private String localeName;\r
23   private TimeZone timeZone;\r
24   private SimpleDateFormat df; // cache\r
25 \r
26   /**\r
27    * Creates a new formatter that formats the requested \r
28    * fields.  The formatter defaults to the current locale\r
29    * and time zone.\r
30    *\r
31    * @param requestedFields the requested fields\r
32    */\r
33   public YMDDateFormatter(String requestedFields) {\r
34     this(requestedFields, Locale.getDefault().toString(),\r
35          TimeZone.getDefault());\r
36   }\r
37 \r
38   /**\r
39    * Creates a new formatter that formats the requested \r
40    * fields using the provided locale and time zone.\r
41    *\r
42    * @param requestedFields the requested fields\r
43    * @param localeName the locale to use\r
44    * @param timeZone the time zone to use\r
45    */\r
46   public YMDDateFormatter(String requestedFields, String localeName, \r
47                              TimeZone timeZone) {\r
48     this.requestedFields = requestedFields;\r
49     this.localeName = localeName;\r
50     this.timeZone = timeZone;\r
51 \r
52     Locale locale = Utils.localeFromString(localeName);\r
53     this.df = new SimpleDateFormat("yyyy/mm/dd", locale);\r
54     this.df.setTimeZone(timeZone);\r
55   }\r
56 \r
57   /**\r
58    * Returns a string representing the formatted date.\r
59    * @param date the date in milliseconds\r
60    */\r
61   public String format(long date) {\r
62     return format(new Date(date));\r
63   }\r
64 \r
65   /**\r
66    * Returns a string representing the formatted date.\r
67    * @param date the date\r
68    */\r
69   public String format(Date date) {\r
70     synchronized (this) {\r
71       if (df == null) {\r
72         // ignores requested fields\r
73         // todo: make this really work\r
74       }\r
75     }\r
76     return df.format(date);\r
77   }\r
78 \r
79   /**\r
80    * Returns a version of this formatter customized to the provided locale.\r
81    */\r
82   public DateFormatter withLocale(String locName) {\r
83     if (!locName.equals(localeName)) {\r
84       return new YMDDateFormatter(requestedFields, locName, timeZone);\r
85     }\r
86     return this;\r
87   }\r
88 \r
89   /**\r
90    * Returns a version of this formatter customized to the provided time zone.\r
91    */\r
92   public DateFormatter withTimeZone(TimeZone tz) {\r
93     if (!tz.equals(timeZone)) {\r
94       return new YMDDateFormatter(requestedFields, localeName, tz);\r
95     }\r
96     return this;\r
97   }\r
98 }\r