|
jfreechart example source code file (Year.java)
The jfreechart Year.java source code
/* ===========================================================
* 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.]
*
* ---------
* Year.java
* ---------
* (C) Copyright 2001-2008, by Object Refinery Limited.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): -;
*
* Changes
* -------
* 11-Oct-2001 : Version 1 (DG);
* 14-Nov-2001 : Override for toString() method (DG);
* 19-Dec-2001 : Added a new constructor as suggested by Paul English (DG);
* 29-Jan-2002 : Worked on parseYear() method (DG);
* 14-Feb-2002 : Fixed bug in Year(Date) constructor (DG);
* 26-Feb-2002 : Changed getStart(), getMiddle() and getEnd() methods to
* evaluate with reference to a particular time zone (DG);
* 19-Mar-2002 : Changed API for TimePeriod classes (DG);
* 10-Sep-2002 : Added getSerialIndex() method (DG);
* 04-Oct-2002 : Fixed errors reported by Checkstyle (DG);
* 10-Jan-2003 : Changed base class and method names (DG);
* 05-Mar-2003 : Fixed bug in getFirstMillisecond() picked up in JUnit
* tests (DG);
* 13-Mar-2003 : Moved to com.jrefinery.data.time package, and implemented
* Serializable (DG);
* 21-Oct-2003 : Added hashCode() method (DG);
* ------------- JFREECHART 1.0.x ---------------------------------------------
* 05-Oct-2006 : Updated API docs (DG);
* 06-Oct-2006 : Refactored to cache first and last millisecond values (DG);
* 16-Sep-2008 : Extended range of valid years, and deprecated
* DEFAULT_TIME_ZONE (DG);
* 25-Nov-2008 : Added new constructor with Locale (DG);
*
*/
package org.jfree.data.time;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
/**
* Represents a year in the range -9999 to 9999. This class is immutable,
* which is a requirement for all {@link RegularTimePeriod} subclasses.
*/
public class Year extends RegularTimePeriod implements Serializable {
/**
* The minimum year value.
*
* @since 1.0.11
*/
public static final int MINIMUM_YEAR = -9999;
/**
* The maximum year value.
*
* @since 1.0.11
*/
public static final int MAXIMUM_YEAR = 9999;
/** For serialization. */
private static final long serialVersionUID = -7659990929736074836L;
/** The year. */
private short year;
/** The first millisecond. */
private long firstMillisecond;
/** The last millisecond. */
private long lastMillisecond;
/**
* Creates a new <code>Year, based on the current system date/time.
*/
public Year() {
this(new Date());
}
/**
* Creates a time period representing a single year.
*
* @param year the year.
*/
public Year(int year) {
if ((year < Year.MINIMUM_YEAR) || (year > Year.MAXIMUM_YEAR)) {
throw new IllegalArgumentException(
"Year constructor: year (" + year + ") outside valid range.");
}
this.year = (short) year;
peg(Calendar.getInstance());
}
/**
* Creates a new <code>Year, based on a particular instant in time,
* using the default time zone.
*
* @param time the time (<code>null not permitted).
*
* @see #Year(Date, TimeZone)
*/
public Year(Date time) {
this(time, TimeZone.getDefault());
}
/**
* Constructs a year, based on a particular instant in time and a time zone.
*
* @param time the time (<code>null not permitted).
* @param zone the time zone.
*
* @deprecated Since 1.0.12, use {@link #Year(Date, TimeZone, Locale)}
* instead.
*/
public Year(Date time, TimeZone zone) {
this(time, zone, Locale.getDefault());
}
/**
* Creates a new <code>Year instance, for the specified time zone
* and locale.
*
* @param time the current time (<code>null not permitted).
* @param zone the time zone.
* @param locale the locale.
*
* @since 1.0.12
*/
public Year(Date time, TimeZone zone, Locale locale) {
Calendar calendar = Calendar.getInstance(zone, locale);
calendar.setTime(time);
this.year = (short) calendar.get(Calendar.YEAR);
peg(calendar);
}
/**
* Returns the year.
*
* @return The year.
*/
public int getYear() {
return this.year;
}
/**
* Returns the first millisecond of the year. This will be determined
* relative to the time zone specified in the constructor, or in the
* calendar instance passed in the most recent call to the
* {@link #peg(Calendar)} method.
*
* @return The first millisecond of the year.
*
* @see #getLastMillisecond()
*/
public long getFirstMillisecond() {
return this.firstMillisecond;
}
/**
* Returns the last millisecond of the year. This will be
* determined relative to the time zone specified in the constructor, or
* in the calendar instance passed in the most recent call to the
* {@link #peg(Calendar)} method.
*
* @return The last millisecond of the year.
*
* @see #getFirstMillisecond()
*/
public long getLastMillisecond() {
return this.lastMillisecond;
}
/**
* Recalculates the start date/time and end date/time for this time period
* relative to the supplied calendar (which incorporates a time zone).
*
* @param calendar the calendar (<code>null not permitted).
*
* @since 1.0.3
*/
public void peg(Calendar calendar) {
this.firstMillisecond = getFirstMillisecond(calendar);
this.lastMillisecond = getLastMillisecond(calendar);
}
/**
* Returns the year preceding this one.
*
* @return The year preceding this one (or <code>null if the
* current year is -9999).
*/
public RegularTimePeriod previous() {
if (this.year > Year.MINIMUM_YEAR) {
return new Year(this.year - 1);
}
else {
return null;
}
}
/**
* Returns the year following this one.
*
* @return The year following this one (or <code>null if the current
* year is 9999).
*/
public RegularTimePeriod next() {
if (this.year < Year.MAXIMUM_YEAR) {
return new Year(this.year + 1);
}
else {
return null;
}
}
/**
* Returns a serial index number for the year.
* <P>
* The implementation simply returns the year number (e.g. 2002).
*
* @return The serial index number.
*/
public long getSerialIndex() {
return this.year;
}
/**
* Returns the first millisecond of the year, evaluated using the supplied
* calendar (which determines the time zone).
*
* @param calendar the calendar (<code>null not permitted).
*
* @return The first millisecond of the year.
*
* @throws NullPointerException if <code>calendar is
* <code>null.
*/
public long getFirstMillisecond(Calendar calendar) {
calendar.set(this.year, Calendar.JANUARY, 1, 0, 0, 0);
calendar.set(Calendar.MILLISECOND, 0);
// in the following line, we'd rather call calendar.getTimeInMillis()
// to avoid object creation, but that isn't supported in Java 1.3.1
return calendar.getTime().getTime();
}
/**
* Returns the last millisecond of the year, evaluated using the supplied
* calendar (which determines the time zone).
*
* @param calendar the calendar (<code>null not permitted).
*
* @return The last millisecond of the year.
*
* @throws NullPointerException if <code>calendar is
* <code>null.
*/
public long getLastMillisecond(Calendar calendar) {
calendar.set(this.year, Calendar.DECEMBER, 31, 23, 59, 59);
calendar.set(Calendar.MILLISECOND, 999);
// in the following line, we'd rather call calendar.getTimeInMillis()
// to avoid object creation, but that isn't supported in Java 1.3.1
return calendar.getTime().getTime();
}
/**
* Tests the equality of this <code>Year object to an arbitrary
* object. Returns <code>true if the target is a
Other jfreechart examples (source code examples)Here is a short list of links related to this jfreechart Year.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.