📄 month.java
字号:
}
}
return result;
}
/**
* Returns a serial index number for the month.
*
* @return The serial index number.
*/
public long getSerialIndex() {
return this.year * 12L + this.month;
}
/**
* Returns a string representing the month (e.g. "January 2002").
* <P>
* To do: look at internationalisation.
*
* @return A string representing the month.
*/
public String toString() {
return SerialDate.monthCodeToString(this.month) + " " + this.year;
}
/**
* Tests the equality of this Month object to an arbitrary object.
* Returns true if the target is a Month instance representing the same
* month as this object. In all other cases, returns false.
*
* @param obj the object (<code>null</code> permitted).
*
* @return <code>true</code> if month and year of this and object are the
* same.
*/
public boolean equals(Object obj) {
if (obj != null) {
if (obj instanceof Month) {
Month target = (Month) obj;
return (this.month == target.getMonth()
&& (this.year == target.getYearValue()));
}
else {
return false;
}
}
else {
return false;
}
}
/**
* Returns a hash code for this object instance. The approach described by
* Joshua Bloch in "Effective Java" has been used here:
* <p>
* <code>http://developer.java.sun.com/developer/Books/effectivejava
* /Chapter3.pdf</code>
*
* @return A hash code.
*/
public int hashCode() {
int result = 17;
result = 37 * result + this.month;
result = 37 * result + this.year;
return result;
}
/**
* Returns an integer indicating the order of this Month object relative to
* the specified
* object: negative == before, zero == same, positive == after.
*
* @param o1 the object to compare.
*
* @return negative == before, zero == same, positive == after.
*/
public int compareTo(Object o1) {
int result;
// CASE 1 : Comparing to another Month object
// --------------------------------------------
if (o1 instanceof Month) {
Month m = (Month) o1;
result = this.year - m.getYearValue();
if (result == 0) {
result = this.month - m.getMonth();
}
}
// CASE 2 : Comparing to another TimePeriod object
// -----------------------------------------------
else if (o1 instanceof RegularTimePeriod) {
// more difficult case - evaluate later...
result = 0;
}
// CASE 3 : Comparing to a non-TimePeriod object
// ---------------------------------------------
else {
// consider time periods to be ordered after general objects
result = 1;
}
return result;
}
/**
* Returns the first millisecond of the month, evaluated using the supplied
* calendar (which determines the time zone).
*
* @param calendar the calendar (<code>null</code> not permitted).
*
* @return The first millisecond of the month.
*
* @throws NullPointerException if <code>calendar</code> is
* <code>null</code>.
*/
public long getFirstMillisecond(Calendar calendar) {
calendar.set(this.year, this.month - 1, 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 month, evaluated using the supplied
* calendar (which determines the time zone).
*
* @param calendar the calendar (<code>null</code> not permitted).
*
* @return The last millisecond of the month.
*
* @throws NullPointerException if <code>calendar</code> is
* <code>null</code>.
*/
public long getLastMillisecond(Calendar calendar) {
int eom = SerialDate.lastDayOfMonth(this.month, this.year);
calendar.set(this.year, this.month - 1, eom, 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();
}
/**
* Parses the string argument as a month.
* <P>
* This method is required to accept the format "YYYY-MM". It will also
* accept "MM-YYYY". Anything else, at the moment, is a bonus.
*
* @param s the string to parse.
*
* @return <code>null</code> if the string is not parseable, the month
* otherwise.
*/
public static Month parseMonth(String s) {
Month result = null;
if (s != null) {
// trim whitespace from either end of the string
s = s.trim();
int i = Month.findSeparator(s);
if (i != -1) {
String s1 = s.substring(0, i).trim();
String s2 = s.substring(i + 1, s.length()).trim();
Year year = Month.evaluateAsYear(s1);
int month;
if (year != null) {
month = SerialDate.stringToMonthCode(s2);
if (month == -1) {
throw new TimePeriodFormatException(
"Can't evaluate the month."
);
}
result = new Month(month, year);
}
else {
year = Month.evaluateAsYear(s2);
if (year != null) {
month = SerialDate.stringToMonthCode(s1);
if (month == -1) {
throw new TimePeriodFormatException(
"Can't evaluate the month."
);
}
result = new Month(month, year);
}
else {
throw new TimePeriodFormatException(
"Can't evaluate the year."
);
}
}
}
else {
throw new TimePeriodFormatException(
"Could not find separator."
);
}
}
return result;
}
/**
* Finds the first occurrence of ' ', '-', ',' or '.'
*
* @param s the string to parse.
*
* @return <code>-1</code> if none of the characters where found, the
* position of the first occurence otherwise.
*/
private static int findSeparator(String s) {
int result = s.indexOf('-');
if (result == -1) {
result = s.indexOf(',');
}
if (result == -1) {
result = s.indexOf(' ');
}
if (result == -1) {
result = s.indexOf('.');
}
return result;
}
/**
* Creates a year from a string, or returns null (format exceptions
* suppressed).
*
* @param s the string to parse.
*
* @return <code>null</code> if the string is not parseable, the year
* otherwise.
*/
private static Year evaluateAsYear(String s) {
Year result = null;
try {
result = Year.parseYear(s);
}
catch (TimePeriodFormatException e) {
// suppress
}
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -