2 ******************************************************************************
\r
3 * Copyright (C) 2007-2008, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 ******************************************************************************
\r
8 package com.ibm.icu.impl.duration.impl;
\r
10 import com.ibm.icu.impl.duration.DateFormatter;
\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
18 * A DateFormatter that formats the requested date fields.
\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
27 * Creates a new formatter that formats the requested
\r
28 * fields. The formatter defaults to the current locale
\r
31 * @param requestedFields the requested fields
\r
33 public YMDDateFormatter(String requestedFields) {
\r
34 this(requestedFields, Locale.getDefault().toString(),
\r
35 TimeZone.getDefault());
\r
39 * Creates a new formatter that formats the requested
\r
40 * fields using the provided locale and time zone.
\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
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
52 Locale locale = Utils.localeFromString(localeName);
\r
53 this.df = new SimpleDateFormat("yyyy/mm/dd", locale);
\r
54 this.df.setTimeZone(timeZone);
\r
58 * Returns a string representing the formatted date.
\r
59 * @param date the date in milliseconds
\r
61 public String format(long date) {
\r
62 return format(new Date(date));
\r
66 * Returns a string representing the formatted date.
\r
67 * @param date the date
\r
69 public String format(Date date) {
\r
70 synchronized (this) {
\r
72 // ignores requested fields
\r
73 // todo: make this really work
\r
76 return df.format(date);
\r
80 * Returns a version of this formatter customized to the provided locale.
\r
82 public DateFormatter withLocale(String locName) {
\r
83 if (!locName.equals(localeName)) {
\r
84 return new YMDDateFormatter(requestedFields, locName, timeZone);
\r
90 * Returns a version of this formatter customized to the provided time zone.
\r
92 public DateFormatter withTimeZone(TimeZone tz) {
\r
93 if (!tz.equals(timeZone)) {
\r
94 return new YMDDateFormatter(requestedFields, localeName, tz);
\r