spreadsheetdate.java

来自「JfreeChart 常用图表例子」· Java 代码 · 共 496 行 · 第 1/2 页

JAVA
496
字号
/* ======================================================================== * JCommon : a free general purpose class library for the Java(tm) platform * ======================================================================== * * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors. *  * Project Info:  http://www.jfree.org/jcommon/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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. * * [Java is a trademark or registered trademark of Sun Microsystems, Inc.  * in the United States and other countries.] * * -------------------- * SpreadsheetDate.java * -------------------- * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors. * * Original Author:  David Gilbert (for Object Refinery Limited); * Contributor(s):   -; * * $Id: SpreadsheetDate.java,v 1.6 2005/06/01 14:12:28 taqua Exp $ * * Changes * ------- * 11-Oct-2001 : Version 1 (DG); * 05-Nov-2001 : Added getDescription() and setDescription() methods (DG); * 12-Nov-2001 : Changed name from ExcelDate.java to SpreadsheetDate.java (DG); *               Fixed a bug in calculating day, month and year from serial  *               number (DG); * 24-Jan-2002 : Fixed a bug in calculating the serial number from the day,  *               month and year.  Thanks to Trevor Hills for the report (DG); * 29-May-2002 : Added equals(Object) method (SourceForge ID 558850) (DG); * 03-Oct-2002 : Fixed errors reported by Checkstyle (DG); * 13-Mar-2003 : Implemented Serializable (DG); * 04-Sep-2003 : Completed isInRange() methods (DG); * 05-Sep-2003 : Implemented Comparable (DG); * 21-Oct-2003 : Added hashCode() method (DG); * */package org.jfree.date;import java.io.Serializable;import java.util.Calendar;import java.util.Date;/** * Represents a date using an integer, in a similar fashion to the * implementation in Microsoft Excel.  The range of dates supported is * 1-Jan-1900 to 31-Dec-9999. * <P> * Be aware that there is a deliberate bug in Excel that recognises the year * 1900 as a leap year when in fact it is not a leap year. You can find more * information on the Microsoft website in article Q181370: * <P> * http://support.microsoft.com/support/kb/articles/Q181/3/70.asp * <P> * Excel uses the convention that 1-Jan-1900 = 1.  This class uses the * convention 1-Jan-1900 = 2. * The result is that the day number in this class will be different to the * Excel figure for January and February 1900...but then Excel adds in an extra * day (29-Feb-1900 which does not actually exist!) and from that point forward * the day numbers will match. * * @author David Gilbert */public class SpreadsheetDate extends SerialDate implements Serializable {    /** For serialization. */    private static final long serialVersionUID = -2039586705374454461L;        /**      * The day number (1-Jan-1900 = 2, 2-Jan-1900 = 3, ..., 31-Dec-9999 =      * 2958465).      */    private int serial;    /** The day of the month (1 to 28, 29, 30 or 31 depending on the month). */    private int day;    /** The month of the year (1 to 12). */    private int month;    /** The year (1900 to 9999). */    private int year;    /** An optional description for the date. */    private String description;    /**     * Creates a new date instance.     *     * @param day  the day (in the range 1 to 28/29/30/31).     * @param month  the month (in the range 1 to 12).     * @param year  the year (in the range 1900 to 9999).     */    public SpreadsheetDate(final int day, final int month, final int year) {        if ((year >= 1900) && (year <= 9999)) {            this.year = year;        }        else {            throw new IllegalArgumentException(                "The 'year' argument must be in range 1900 to 9999."            );        }        if ((month >= MonthConstants.JANUARY)                 && (month <= MonthConstants.DECEMBER)) {            this.month = month;        }        else {            throw new IllegalArgumentException(                "The 'month' argument must be in the range 1 to 12."            );        }        if ((day >= 1) && (day <= SerialDate.lastDayOfMonth(month, year))) {            this.day = day;        }        else {            throw new IllegalArgumentException("Invalid 'day' argument.");        }        // the serial number needs to be synchronised with the day-month-year...        this.serial = calcSerial(day, month, year);        this.description = null;    }    /**     * Standard constructor - creates a new date object representing the     * specified day number (which should be in the range 2 to 2958465.     *     * @param serial  the serial number for the day (range: 2 to 2958465).     */    public SpreadsheetDate(final int serial) {        if ((serial >= SERIAL_LOWER_BOUND) && (serial <= SERIAL_UPPER_BOUND)) {            this.serial = serial;        }        else {            throw new IllegalArgumentException(                "SpreadsheetDate: Serial must be in range 2 to 2958465.");        }        // the day-month-year needs to be synchronised with the serial number...        calcDayMonthYear();    }    /**     * Returns the description that is attached to the date.  It is not      * required that a date have a description, but for some applications it      * is useful.     *     * @return The description that is attached to the date.     */    public String getDescription() {        return this.description;    }    /**     * Sets the description for the date.     *     * @param description  the description for this date (<code>null</code>      *                     permitted).     */    public void setDescription(final String description) {        this.description = description;    }    /**     * Returns the serial number for the date, where 1 January 1900 = 2     * (this corresponds, almost, to the numbering system used in Microsoft     * Excel for Windows and Lotus 1-2-3).     *     * @return The serial number of this date.     */    public int toSerial() {        return this.serial;    }    /**     * Returns a <code>java.util.Date</code> equivalent to this date.     *     * @return The date.     */    public Date toDate() {        final Calendar calendar = Calendar.getInstance();        calendar.set(getYYYY(), getMonth() - 1, getDayOfMonth(), 0, 0, 0);        return calendar.getTime();    }    /**     * Returns the year (assume a valid range of 1900 to 9999).     *     * @return The year.     */    public int getYYYY() {        return this.year;    }    /**     * Returns the month (January = 1, February = 2, March = 3).     *     * @return The month of the year.     */    public int getMonth() {        return this.month;    }    /**     * Returns the day of the month.     *     * @return The day of the month.     */    public int getDayOfMonth() {        return this.day;    }    /**     * Returns a code representing the day of the week.     * <P>     * The codes are defined in the {@link SerialDate} class as:      * <code>SUNDAY</code>, <code>MONDAY</code>, <code>TUESDAY</code>,      * <code>WEDNESDAY</code>, <code>THURSDAY</code>, <code>FRIDAY</code>, and      * <code>SATURDAY</code>.     *     * @return A code representing the day of the week.     */    public int getDayOfWeek() {        return (this.serial + 6) % 7 + 1;    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?