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