📄 minute.java
字号:
* @since 1.0.3
*/
public void peg(Calendar calendar) {
this.firstMillisecond = getFirstMillisecond(calendar);
this.lastMillisecond = getLastMillisecond(calendar);
}
/**
* Returns the minute preceding this one.
*
* @return The minute preceding this one.
*/
public RegularTimePeriod previous() {
Minute result;
if (this.minute != FIRST_MINUTE_IN_HOUR) {
result = new Minute(this.minute - 1, getHour());
}
else {
Hour h = (Hour) getHour().previous();
if (h != null) {
result = new Minute(LAST_MINUTE_IN_HOUR, h);
}
else {
result = null;
}
}
return result;
}
/**
* Returns the minute following this one.
*
* @return The minute following this one.
*/
public RegularTimePeriod next() {
Minute result;
if (this.minute != LAST_MINUTE_IN_HOUR) {
result = new Minute(this.minute + 1, getHour());
}
else { // we are at the last minute in the hour...
Hour nextHour = (Hour) getHour().next();
if (nextHour != null) {
result = new Minute(FIRST_MINUTE_IN_HOUR, nextHour);
}
else {
result = null;
}
}
return result;
}
/**
* Returns a serial index number for the minute.
*
* @return The serial index number.
*/
public long getSerialIndex() {
long hourIndex = this.day.getSerialIndex() * 24L + this.hour;
return hourIndex * 60L + this.minute;
}
/**
* Returns the first millisecond of the minute.
*
* @param calendar the calendar which defines the timezone
* (<code>null</code> not permitted).
*
* @return The first millisecond.
*
* @throws NullPointerException if <code>calendar</code> is
* <code>null</code>.
*/
public long getFirstMillisecond(Calendar calendar) {
int year = this.day.getYear();
int month = this.day.getMonth() - 1;
int day = this.day.getDayOfMonth();
calendar.clear();
calendar.set(year, month, day, this.hour, this.minute, 0);
calendar.set(Calendar.MILLISECOND, 0);
//return calendar.getTimeInMillis(); // this won't work for JDK 1.3
return calendar.getTime().getTime();
}
/**
* Returns the last millisecond of the minute.
*
* @param calendar the calendar / timezone (<code>null</code> not
* permitted).
*
* @return The last millisecond.
*
* @throws NullPointerException if <code>calendar</code> is
* <code>null</code>.
*/
public long getLastMillisecond(Calendar calendar) {
int year = this.day.getYear();
int month = this.day.getMonth() - 1;
int day = this.day.getDayOfMonth();
calendar.clear();
calendar.set(year, month, day, this.hour, this.minute, 59);
calendar.set(Calendar.MILLISECOND, 999);
//return calendar.getTimeInMillis(); // this won't work for JDK 1.3
return calendar.getTime().getTime();
}
/**
* Tests the equality of this object against an arbitrary Object.
* <P>
* This method will return true ONLY if the object is a Minute object
* representing the same minute as this instance.
*
* @param obj the object to compare (<code>null</code> permitted).
*
* @return <code>true</code> if the minute and hour value of this and the
* object are the same.
*/
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof Minute)) {
return false;
}
Minute that = (Minute) obj;
if (this.minute != that.minute) {
return false;
}
if (this.hour != that.hour) {
return false;
}
return true;
}
/**
* 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.minute;
result = 37 * result + this.hour;
result = 37 * result + this.day.hashCode();
return result;
}
/**
* Returns an integer indicating the order of this Minute object relative
* to the specified object:
*
* negative == before, zero == same, positive == after.
*
* @param o1 object to compare.
*
* @return negative == before, zero == same, positive == after.
*/
public int compareTo(Object o1) {
int result;
// CASE 1 : Comparing to another Minute object
// -------------------------------------------
if (o1 instanceof Minute) {
Minute m = (Minute) o1;
result = getHour().compareTo(m.getHour());
if (result == 0) {
result = this.minute - m.getMinute();
}
}
// 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;
}
/**
* Creates a Minute instance by parsing a string. The string is assumed to
* be in the format "YYYY-MM-DD HH:MM", perhaps with leading or trailing
* whitespace.
*
* @param s the minute string to parse.
*
* @return <code>null</code>, if the string is not parseable, the minute
* otherwise.
*/
public static Minute parseMinute(String s) {
Minute result = null;
s = s.trim();
String daystr = s.substring(0, Math.min(10, s.length()));
Day day = Day.parseDay(daystr);
if (day != null) {
String hmstr = s.substring(
Math.min(daystr.length() + 1, s.length()), s.length()
);
hmstr = hmstr.trim();
String hourstr = hmstr.substring(0, Math.min(2, hmstr.length()));
int hour = Integer.parseInt(hourstr);
if ((hour >= 0) && (hour <= 23)) {
String minstr = hmstr.substring(
Math.min(hourstr.length() + 1, hmstr.length()),
hmstr.length()
);
int minute = Integer.parseInt(minstr);
if ((minute >= 0) && (minute <= 59)) {
result = new Minute(minute, new Hour(hour, day));
}
}
}
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -