📄 relativedateformat.java
字号:
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2008, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* -----------------------
* RelativeDateFormat.java
* -----------------------
* (C) Copyright 2006-2008, by Object Refinery Limited and Contributors.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): Michael Siemer;
*
* Changes:
* --------
* 01-Nov-2006 : Version 1 (DG);
* 23-Nov-2006 : Added argument checks, updated equals(), added clone() and
* hashCode() (DG);
* 15-Feb-2008 : Applied patch 1873328 by Michael Siemer, with minor
* modifications (DG);
* 01-Sep-2008 : Added new fields for hour and minute formatting, based on
* patch 2033092 (DG);
*
*/
package org.jfree.chart.util;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.FieldPosition;
import java.text.NumberFormat;
import java.text.ParsePosition;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* A formatter that formats dates to show the elapsed time relative to some
* base date.
*
* @since 1.0.3
*/
public class RelativeDateFormat extends DateFormat {
/** The base milliseconds for the elapsed time calculation. */
private long baseMillis;
/**
* A flag that controls whether or not a zero day count is displayed.
*/
private boolean showZeroDays;
/**
* A flag that controls whether or not a zero hour count is displayed.
*
* @since 1.0.10
*/
private boolean showZeroHours;
/**
* A formatter for the day count (most likely not critical until the
* day count exceeds 999).
*/
private NumberFormat dayFormatter;
/**
* A prefix prepended to the start of the format if the relative date is
* positive.
*
* @since 1.0.10
*/
private String positivePrefix;
/**
* A string appended after the day count.
*/
private String daySuffix;
/**
* A formatter for the hours.
*
* @since 1.0.11
*/
private NumberFormat hourFormatter;
/**
* A string appended after the hours.
*/
private String hourSuffix;
/**
* A formatter for the minutes.
*
* @since 1.0.11
*/
private NumberFormat minuteFormatter;
/**
* A string appended after the minutes.
*/
private String minuteSuffix;
/**
* A formatter for the seconds (and milliseconds).
*/
private NumberFormat secondFormatter;
/**
* A string appended after the seconds.
*/
private String secondSuffix;
/**
* A constant for the number of milliseconds in one hour.
*/
private static long MILLISECONDS_IN_ONE_HOUR = 60 * 60 * 1000L;
/**
* A constant for the number of milliseconds in one day.
*/
private static long MILLISECONDS_IN_ONE_DAY = 24 * MILLISECONDS_IN_ONE_HOUR;
/**
* Creates a new instance with base milliseconds set to zero.
*/
public RelativeDateFormat() {
this(0L);
}
/**
* Creates a new instance.
*
* @param time the date/time (<code>null</code> not permitted).
*/
public RelativeDateFormat(Date time) {
this(time.getTime());
}
/**
* Creates a new instance.
*
* @param baseMillis the time zone (<code>null</code> not permitted).
*/
public RelativeDateFormat(long baseMillis) {
super();
this.baseMillis = baseMillis;
this.showZeroDays = false;
this.showZeroHours = true;
this.positivePrefix = "";
this.dayFormatter = NumberFormat.getNumberInstance();
this.daySuffix = "d";
this.hourFormatter = NumberFormat.getNumberInstance();
this.hourSuffix = "h";
this.minuteFormatter = NumberFormat.getNumberInstance();
this.minuteSuffix = "m";
this.secondFormatter = NumberFormat.getNumberInstance();
this.secondFormatter.setMaximumFractionDigits(3);
this.secondFormatter.setMinimumFractionDigits(3);
this.secondSuffix = "s";
// we don't use the calendar or numberFormat fields, but equals(Object)
// is failing without them being non-null
this.calendar = new GregorianCalendar();
this.numberFormat = new DecimalFormat("0");
}
/**
* Returns the base date/time used to calculate the elapsed time for
* display.
*
* @return The base date/time in milliseconds since 1-Jan-1970.
*
* @see #setBaseMillis(long)
*/
public long getBaseMillis() {
return this.baseMillis;
}
/**
* Sets the base date/time used to calculate the elapsed time for display.
* This should be specified in milliseconds using the same encoding as
* <code>java.util.Date</code>.
*
* @param baseMillis the base date/time in milliseconds.
*
* @see #getBaseMillis()
*/
public void setBaseMillis(long baseMillis) {
this.baseMillis = baseMillis;
}
/**
* Returns the flag that controls whether or not zero day counts are
* shown in the formatted output.
*
* @return The flag.
*
* @see #setShowZeroDays(boolean)
*/
public boolean getShowZeroDays() {
return this.showZeroDays;
}
/**
* Sets the flag that controls whether or not zero day counts are shown
* in the formatted output.
*
* @param show the flag.
*
* @see #getShowZeroDays()
*/
public void setShowZeroDays(boolean show) {
this.showZeroDays = show;
}
/**
* Returns the flag that controls whether or not zero hour counts are
* shown in the formatted output.
*
* @return The flag.
*
* @see #setShowZeroHours(boolean)
*
* @since 1.0.10
*/
public boolean getShowZeroHours() {
return this.showZeroHours;
}
/**
* Sets the flag that controls whether or not zero hour counts are shown
* in the formatted output.
*
* @param show the flag.
*
* @see #getShowZeroHours()
*
* @since 1.0.10
*/
public void setShowZeroHours(boolean show) {
this.showZeroHours = show;
}
/**
* Returns the string that is prepended to the format if the relative time
* is positive.
*
* @return The string (never <code>null</code>).
*
* @see #setPositivePrefix(String)
*
* @since 1.0.10
*/
public String getPositivePrefix() {
return this.positivePrefix;
}
/**
* Sets the string that is prepended to the format if the relative time is
* positive.
*
* @param prefix the prefix (<code>null</code> not permitted).
*
* @see #getPositivePrefix()
*
* @since 1.0.10
*/
public void setPositivePrefix(String prefix) {
if (prefix == null) {
throw new IllegalArgumentException("Null 'prefix' argument.");
}
this.positivePrefix = prefix;
}
/**
* Sets the formatter for the days.
*
* @param formatter the formatter (<code>null</code> not permitted).
*
* @since 1.0.11
*/
public void setDayFormatter(NumberFormat formatter) {
if (formatter == null) {
throw new IllegalArgumentException("Null 'formatter' argument.");
}
this.dayFormatter = formatter;
}
/**
* Returns the string that is appended to the day count.
*
* @return The string.
*
* @see #setDaySuffix(String)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -