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