2 *******************************************************************************
\r
3 * Copyright (C) 2007-2009, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.text;
\r
9 import java.text.FieldPosition;
\r
10 import java.text.ParsePosition;
\r
11 import java.util.Date;
\r
13 import com.ibm.icu.impl.duration.BasicDurationFormat;
\r
14 import com.ibm.icu.util.ULocale;
\r
17 * This class implements a formatter over a duration in time
\r
18 * such as "2 days from now" or "3 hours ago".
\r
22 public abstract class DurationFormat extends UFormat {
\r
24 private static final long serialVersionUID = -2076961954727774282L;
\r
27 * Construct a duration format for the specified locale
\r
30 public static DurationFormat getInstance(ULocale locale) {
\r
31 return BasicDurationFormat.getInstance(locale);
\r
36 * Subclass interface
\r
38 * @deprecated This API is ICU internal only.
\r
40 protected DurationFormat() {
\r
44 * Subclass interface
\r
46 * @deprecated This API is ICU internal only.
\r
48 protected DurationFormat(ULocale locale) {
\r
49 setLocale(locale,locale);
\r
53 * Format an arbitrary object.
\r
54 * Defaults to a call to formatDurationFromNow() for either Long or Date objects.
\r
55 * @param object the object to format. Should be either a Long, Date, or javax.xml.datatype.Duration object.
\r
56 * @param toAppend the buffer to append to
\r
57 * @param pos the field position, may contain additional error messages.
\r
58 * @return the toAppend buffer
\r
61 public abstract StringBuffer format(Object object, StringBuffer toAppend,
\r
65 * DurationFormat cannot parse, by default. This method will throw an UnsupportedOperationException.
\r
68 public Object parseObject(String source, ParsePosition pos) {
\r
69 throw new UnsupportedOperationException();
\r
73 * Formats the duration between now and a target date.
\r
75 * This is a convenience method that calls
\r
76 * formatDurationFrom(long, long) using now
\r
77 * as the reference date, and the difference between now and
\r
78 * <code>targetDate.getTime()</code> as the duration.
\r
80 * @param targetDate the ending date
\r
81 * @return the formatted time
\r
84 public abstract String formatDurationFromNowTo(Date targetDate);
\r
87 * Formats a duration expressed in milliseconds.
\r
89 * This is a convenience method that calls formatDurationFrom
\r
90 * using the current system time as the reference date.
\r
92 * @param duration the duration in milliseconds
\r
93 * @return the formatted time
\r
96 public abstract String formatDurationFromNow(long duration);
\r
99 * Formats a duration expressed in milliseconds from a reference date.
\r
101 * The reference date allows formatters to use actual durations of
\r
102 * variable-length periods (like months) if they wish.
\r
104 * The duration is expressed as the number of milliseconds in the
\r
105 * past (negative values) or future (positive values) with respect
\r
106 * to a reference date (expressed as milliseconds in epoch).
\r
108 * @param duration the duration in milliseconds
\r
109 * @param referenceDate the date from which to compute the duration
\r
110 * @return the formatted time
\r
113 public abstract String formatDurationFrom(long duration, long referenceDate);
\r