📄 serialdate.java
字号:
/**
* Returns a string representing the supplied month.
* <P>
* The string returned is the long form of the month name taken from the default locale.
*
* @param month the month.
*
* @return a string representing the supplied month.
*/
public static String monthCodeToString(int month) {
return monthCodeToString(month, false);
}
/**
* Returns a string representing the supplied month.
* <P>
* The string returned is the long or short form of the month name taken from the default
* locale.
*
* @param month the month.
* @param shortened if <code>true</code> return the abbreviation of the month.
*
* @return a string representing the supplied month.
*/
public static String monthCodeToString(int month, boolean shortened) {
// check arguments...
if (!isValidMonthCode(month)) {
throw new IllegalArgumentException(
"SerialDate.monthCodeToString: month outside valid range.");
}
String[] months;
if (shortened) {
months = DATE_FORMAT_SYMBOLS.getShortMonths();
}
else {
months = DATE_FORMAT_SYMBOLS.getMonths();
}
return months[month - 1];
}
/**
* Converts a string to a month code.
* <P>
* This method will return one of the constants JANUARY, FEBRUARY, ..., DECEMBER that
* corresponds to the string. If the string is not recognised, this method returns -1.
*
* @param s the string to parse.
*
* @return <code>-1</code> if the string is not parseable, the month of the year otherwise.
*/
public static int stringToMonthCode(String s) {
String[] shortMonthNames = DATE_FORMAT_SYMBOLS.getShortMonths();
String[] monthNames = DATE_FORMAT_SYMBOLS.getMonths();
int result = -1;
s = s.trim();
// first try parsing the string as an integer (1-12)...
try {
result = Integer.parseInt(s);
}
catch (NumberFormatException e) {
// suppress
}
// now search through the month names...
if ((result < 1) || (result > 12)) {
for (int i = 0; i < monthNames.length; i++) {
if (s.equals(shortMonthNames[i])) {
result = i + 1;
break;
}
if (s.equals(monthNames[i])) {
result = i + 1;
break;
}
}
}
return result;
}
/**
* Returns true if the supplied integer code represents a valid week-in-the-month, and
* false otherwise.
*
* @param code the code being checked for validity.
* @return <code>true</code> if the supplied integer code represents a valid week-in-the-month.
*/
public static boolean isValidWeekInMonthCode(int code) {
switch(code) {
case FIRST_WEEK_IN_MONTH: return true;
case SECOND_WEEK_IN_MONTH: return true;
case THIRD_WEEK_IN_MONTH: return true;
case FOURTH_WEEK_IN_MONTH: return true;
case LAST_WEEK_IN_MONTH: return true;
default: return false;
}
}
/**
* Determines whether or not the specified year is a leap year.
*
* @param yyyy the year (in the range 1900 to 9999).
*
* @return <code>true</code> if the specified year is a leap year.
*/
public static boolean isLeapYear(int yyyy) {
if ((yyyy % 4) != 0) {
return false;
}
else if ((yyyy % 400) == 0) {
return true;
}
else if ((yyyy % 100) == 0) {
return false;
}
else {
return true;
}
}
/**
* Returns the number of leap years from 1900 to the specified year INCLUSIVE.
* <P>
* Note that 1900 is not a leap year.
*
* @param yyyy the year (in the range 1900 to 9999).
*
* @return the number of leap years from 1900 to the specified year.
*/
public static int leapYearCount(int yyyy) {
int leap4 = (yyyy - 1896) / 4;
int leap100 = (yyyy - 1800) / 100;
int leap400 = (yyyy - 1600) / 400;
return leap4 - leap100 + leap400;
}
/**
* Returns the number of the last day of the month, taking into account leap years.
*
* @param month the month.
* @param yyyy the year (in the range 1900 to 9999).
*
* @return the number of the last day of the month.
*/
public static int lastDayOfMonth(int month, int yyyy) {
int result = LAST_DAY_OF_MONTH[month];
if (month != FEBRUARY) {
return result;
}
else if (isLeapYear(yyyy)) {
return result + 1;
}
else {
return result;
}
}
/**
* Creates a new date by adding the specified number of days to the base date.
*
* @param days the number of days to add (can be negative).
* @param base the base date.
*
* @return a new date.
*/
public static SerialDate addDays(int days, SerialDate base) {
int serialDayNumber = base.toSerial() + days;
return SerialDate.createInstance(serialDayNumber);
}
/**
* Creates a new date by adding the specified number of months to the base date.
* <P>
* If the base date is close to the end of the month, the day on the result
* may be adjusted slightly: 31 May + 1 month = 30 June.
*
* @param months the number of months to add (can be negative).
* @param base the base date.
*
* @return a new date.
*/
public static SerialDate addMonths(int months, SerialDate base) {
int yy = (12 * base.getYYYY() + base.getMonth() + months - 1) / 12;
int mm = (12 * base.getYYYY() + base.getMonth() + months - 1) % 12 + 1;
int dd = Math.min(base.getDayOfMonth(), SerialDate.lastDayOfMonth(mm, yy));
return SerialDate.createInstance(dd, mm, yy);
}
/**
* Creates a new date by adding the specified number of years to the base date.
*
* @param years the number of years to add (can be negative).
* @param base the base date.
*
* @return a new date.
*/
public static SerialDate addYears(int years, SerialDate base) {
int baseY = base.getYYYY();
int baseM = base.getMonth();
int baseD = base.getDayOfMonth();
int targetY = baseY + years;
int targetD = Math.min(baseD, SerialDate.lastDayOfMonth(baseM, baseY));
return SerialDate.createInstance(targetD, baseM, targetY);
}
/**
* Returns the latest date that falls on the specified day-of-the-week and is BEFORE the
* base date.
*
* @param targetWeekday a code for the target day-of-the-week.
* @param base the base date.
*
* @return the latest date that falls on the specified day-of-the-week and is BEFORE the
* base date.
*/
public static SerialDate getPreviousDayOfWeek(int targetWeekday, SerialDate base) {
// check arguments...
if (!SerialDate.isValidWeekdayCode(targetWeekday)) {
throw new IllegalArgumentException(
"SerialDate.getPreviousDayOfWeek(...): invalid day-of-the-week code.");
}
// find the date...
int adjust;
int baseDOW = base.getDayOfWeek();
if (baseDOW > targetWeekday) {
adjust = Math.min(0, targetWeekday - baseDOW);
}
else {
adjust = -7 + Math.max(0, targetWeekday - baseDOW);
}
return SerialDate.addDays(adjust, base);
}
/**
* Returns the earliest date that falls on the specified day-of-the-week
* and is AFTER the base date.
*
* @param targetWeekday a code for the target day-of-the-week.
* @param base the base date.
*
* @return the earliest date that falls on the specified day-of-the-week and is AFTER the
* base date.
*/
public static SerialDate getFollowingDayOfWeek(int targetWeekday, SerialDate base) {
// check arguments...
if (!SerialDate.isValidWeekdayCode(targetWeekday)) {
throw new IllegalArgumentException(
"SerialDate.getFollowingDayOfWeek(...): invalid day-of-the-week code.");
}
// find the date...
int adjust;
int baseDOW = base.getDayOfWeek();
if (baseDOW > targetWeekday) {
adjust = 7 + Math.min(0, targetWeekday - baseDOW);
}
else {
adjust = Math.max(0, targetWeekday - baseDOW);
}
return SerialDate.addDays(adjust, base);
}
/**
* Returns the date that falls on the specified day-of-the-week and is
* CLOSEST to the base date.
*
* @param targetDOW a code for the target day-of-the-week.
* @param base the base date.
*
* @return the date that falls on the specified day-of-the-week and is CLOSEST to the
* base date.
*/
public static SerialDate getNearestDayOfWeek(int targetDOW, SerialDate base) {
// check arguments...
if (!SerialDate.isValidWeekdayCode(targetDOW)) {
throw new IllegalArgumentException(
"SerialDate.getNearestDayOfWeek(...): invalid day-of-the-week code.");
}
// find the date...
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -