📄 serialdate.java
字号:
/* =================================================
* JWorkbook : data export from Java to spreadsheets
* =================================================
*
* Project Info: http://www.jfree.org/jworkbook/index.html;
* Project Lead: David Gilbert (david.gilbert@object-refinery.com);
*
* (C) Copyright 2001-2003, by Object Refinery Limited.
*
* 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.
*
* ---------------
* SerialDate.java
* ---------------
* (C) Copyright 2001-2003, by Object Refinery Limited.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): -;
*
* $Id: SerialDate.java,v 1.2 2003/05/29 09:46:19 mungady Exp $
*
* Changes
* -------
* 08-Jul-2003 : Version 1 copied from JCommon (DG);
*
*/
package org.jfree.workbook.date;
import java.io.Serializable;
import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
/**
* An abstract class that defines our requirements for manipulating dates,
* without tying down a particular implementation.
* <P>
* Requirement 1 : match at least what Excel does for dates;
* Requirement 2 : class is immutable;
* <P>
* Why not just use java.util.Date? We will, when it makes sense. At times,
* java.util.Date can be *too* precise - it represents an instant in time,
* accurate to 1/1000th of a second (with the date itself depending on the
* time-zone). Sometimes we just want to represent a particular day (e.g. 21
* January 2015) without concerning ourselves about the time of day, or the
* time-zone, or anything else. That's what we've defined SerialDate for.
* <P>
* You can call getInstance() to get a concrete subclass of SerialDate,
* without worrying about the exact implementation.
*
* @author David Gilbert
*/
public abstract class SerialDate implements Serializable, MonthConstants {
/** Date format symbols. */
public static final DateFormatSymbols
DATE_FORMAT_SYMBOLS = new SimpleDateFormat().getDateFormatSymbols();
/** The serial number for 1 January 1900. */
public static final int SERIAL_LOWER_BOUND = 2;
/** The serial number for 31 December 9999. */
public static final int SERIAL_UPPER_BOUND = 2958465;
/** The lowest year value supported by this date format. */
public static final int MINIMUM_YEAR_SUPPORTED = 1900;
/** The highest year value supported by this date format. */
public static final int MAXIMUM_YEAR_SUPPORTED = 9999;
/** Useful constant for Monday. Equivalent to java.util.Calendar.MONDAY. */
public static final int MONDAY = Calendar.MONDAY;
/** Useful constant for Tuesday. Equivalent to java.util.Calendar.TUESDAY. */
public static final int TUESDAY = Calendar.TUESDAY;
/** Useful constant for Wednesday. Equivalent to java.util.Calendar.WEDNESDAY. */
public static final int WEDNESDAY = Calendar.WEDNESDAY;
/** Useful constant for Thrusday. Equivalent to java.util.Calendar.THURSDAY. */
public static final int THURSDAY = Calendar.THURSDAY;
/** Useful constant for Friday. Equivalent to java.util.Calendar.FRIDAY. */
public static final int FRIDAY = Calendar.FRIDAY;
/** Useful constant for Saturday. Equivalent to java.util.Calendar.SATURDAY. */
public static final int SATURDAY = Calendar.SATURDAY;
/** Useful constant for Sunday. Equivalent to java.util.Calendar.SUNDAY. */
public static final int SUNDAY = Calendar.SUNDAY;
/** The number of days in each month in non leap years. */
public static final int[] LAST_DAY_OF_MONTH =
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
/** The number of days in a (non-leap) year up to the end of each month. */
public static final int[] AGGREGATE_DAYS_TO_END_OF_MONTH =
{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
/** The number of days in a year up to the end of the preceding month. */
public static final int[] AGGREGATE_DAYS_TO_END_OF_PRECEDING_MONTH =
{0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
/** The number of days in a leap year up to the end of each month. */
public static final int[] LEAP_YEAR_AGGREGATE_DAYS_TO_END_OF_MONTH =
{0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366};
/** The number of days in a leap year up to the end of the preceding month. */
public static final int[] LEAP_YEAR_AGGREGATE_DAYS_TO_END_OF_PRECEDING_MONTH =
{0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366};
/** A useful constant for referring to the first week in a month. */
public static final int FIRST_WEEK_IN_MONTH = 1;
/** A useful constant for referring to the second week in a month. */
public static final int SECOND_WEEK_IN_MONTH = 2;
/** A useful constant for referring to the third week in a month. */
public static final int THIRD_WEEK_IN_MONTH = 3;
/** A useful constant for referring to the fourth week in a month. */
public static final int FOURTH_WEEK_IN_MONTH = 4;
/** A useful constant for referring to the last week in a month. */
public static final int LAST_WEEK_IN_MONTH = 0;
/** Useful range constant. */
public static final int INCLUDE_NONE = 0;
/** Useful range constant. */
public static final int INCLUDE_FIRST = 1;
/** Useful range constant. */
public static final int INCLUDE_SECOND = 2;
/** Useful range constant. */
public static final int INCLUDE_BOTH = 3;
/** Useful constant for specifying a day of the week relative to a fixed date. */
public static final int PRECEDING = -1;
/** Useful constant for specifying a day of the week relative to a fixed date. */
public static final int NEAREST = 0;
/** Useful constant for specifying a day of the week relative to a fixed date. */
public static final int FOLLOWING = 1;
/** A description for the date. */
private String description;
/**
* Returns true if the supplied integer code represents a valid day-of-the-week, and
* false otherwise.
*
* @param code the code being checked for validity.
*
* @return true if the supplied integer code represents a valid day-of-the-week, and false
* otherwise.
*/
public static boolean isValidWeekdayCode(int code) {
switch(code) {
case SUNDAY: return true;
case MONDAY: return true;
case TUESDAY: return true;
case WEDNESDAY: return true;
case THURSDAY: return true;
case FRIDAY: return true;
case SATURDAY: return true;
default: return false;
}
}
/**
* Converts the supplied string to a day of the week.
*
* @param s a string representing the day of the week.
*
* @return <code>-1</code> if the string is not convertable, the day of the week otherwise.
*/
public static int stringToWeekdayCode(String s) {
String[] shortWeekdayNames = DATE_FORMAT_SYMBOLS.getShortWeekdays();
String[] weekDayNames = DATE_FORMAT_SYMBOLS.getWeekdays();
int result = -1;
s = s.trim();
for (int i = 0; i < weekDayNames.length; i++) {
if (s.equals(shortWeekdayNames[i])) {
result = i;
break;
}
if (s.equals(weekDayNames[i])) {
result = i;
break;
}
}
return result;
}
/**
* Returns a string representing the supplied day-of-the-week.
* <P>
* Need to find a better approach.
*
* @param weekday the day of the week.
*
* @return a string representing the supplied day-of-the-week.
*/
public static String weekdayCodeToString(int weekday) {
String[] weekdays = DATE_FORMAT_SYMBOLS.getWeekdays();
return weekdays[weekday];
}
/**
* Returns an array of month names.
*
* @return an array of month names.
*/
public static String[] getMonths() {
return getMonths(false);
}
/**
* Returns an array of month names.
*
* @param shortened a flag indicating that shortened month names should be returned.
*
* @return an array of month names.
*/
public static String[] getMonths(boolean shortened) {
if (shortened) {
return DATE_FORMAT_SYMBOLS.getShortMonths();
}
else {
return DATE_FORMAT_SYMBOLS.getMonths();
}
}
/**
* Returns true if the supplied integer code represents a valid month.
*
* @param code the code being checked for validity.
*
* @return <code>true</code> if the supplied integer code represents a valid month.
*/
public static boolean isValidMonthCode(int code) {
switch(code) {
case JANUARY: return true;
case FEBRUARY: return true;
case MARCH: return true;
case APRIL: return true;
case MAY: return true;
case JUNE: return true;
case JULY: return true;
case AUGUST: return true;
case SEPTEMBER: return true;
case OCTOBER: return true;
case NOVEMBER: return true;
case DECEMBER: return true;
default: return false;
}
}
/**
* Returns the quarter for the specified month.
*
* @param code the month code (1-12).
*
* @return the quarter that the month belongs to.
*/
public static int monthCodeToQuarter(int code) {
switch(code) {
case JANUARY: return 1;
case FEBRUARY: return 1;
case MARCH: return 1;
case APRIL: return 2;
case MAY: return 2;
case JUNE: return 2;
case JULY: return 3;
case AUGUST: return 3;
case SEPTEMBER: return 3;
case OCTOBER: return 4;
case NOVEMBER: return 4;
case DECEMBER: return 4;
default: throw new IllegalArgumentException(
"SerialDate.monthCodeToQuarter: invalid month code.");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -