2 ******************************************************************************
3 * Copyright (C) 2007-2013, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 ******************************************************************************
8 package com.ibm.icu.impl.duration.impl;
10 import java.text.SimpleDateFormat;
11 import java.util.Date;
12 import java.util.Locale;
13 import java.util.TimeZone;
15 import com.ibm.icu.impl.duration.DateFormatter;
18 * A DateFormatter that formats the requested date fields.
20 public class YMDDateFormatter implements DateFormatter {
21 private String requestedFields;
22 private String localeName;
23 private TimeZone timeZone;
24 private SimpleDateFormat df; // cache
27 * Creates a new formatter that formats the requested
28 * fields. The formatter defaults to the current locale
31 * @param requestedFields the requested fields
33 public YMDDateFormatter(String requestedFields) {
34 this(requestedFields, Locale.getDefault().toString(),
35 TimeZone.getDefault());
39 * Creates a new formatter that formats the requested
40 * fields using the provided locale and time zone.
42 * @param requestedFields the requested fields
43 * @param localeName the locale to use
44 * @param timeZone the time zone to use
46 public YMDDateFormatter(String requestedFields, String localeName,
48 this.requestedFields = requestedFields;
49 this.localeName = localeName;
50 this.timeZone = timeZone;
52 Locale locale = Utils.localeFromString(localeName);
53 this.df = new SimpleDateFormat("yyyy/mm/dd", locale);
54 this.df.setTimeZone(timeZone);
58 * Returns a string representing the formatted date.
59 * @param date the date in milliseconds
61 public String format(long date) {
62 return format(new Date(date));
66 * Returns a string representing the formatted date.
67 * @param date the date
69 public String format(Date date) {
70 // synchronized (this) {
72 // // ignores requested fields
73 // // todo: make this really work
76 return df.format(date);
80 * Returns a version of this formatter customized to the provided locale.
82 public DateFormatter withLocale(String locName) {
83 if (!locName.equals(localeName)) {
84 return new YMDDateFormatter(requestedFields, locName, timeZone);
90 * Returns a version of this formatter customized to the provided time zone.
92 public DateFormatter withTimeZone(TimeZone tz) {
93 if (!tz.equals(timeZone)) {
94 return new YMDDateFormatter(requestedFields, localeName, tz);