public class MyTime {
//final 靜態常數不會修改的
public static final int HOURS_PER_DAY = 24;
public static final int MINUTES_PER_HOUR = 60;
private int mHour;
private int mMinute;
public void setMytime(int inHour, int inMinute) {
if( inHour < 0 && inHour >= HOURS_PER_DAY)
return;
if(inMinute < 0 && inMinute >= HOURS_PER_DAY)
return;
mHour = inHour;
mMinute = inMinute;
}
public void passMinute(int inMinute) {
// (原本小時 + (分+傳入的分)/24小時的餘數)/24小時的餘數
mHour = (mHour + (mMinute + inMinute) / MINUTES_PER_HOUR)
% HOURS_PER_DAY;
//(分+傳入的分)/60分的餘數
mMinute = (mMinute + inMinute)%MINUTES_PER_HOUR;
}
public String toString() {
return (mHour >= 10 ? "": "0" )+ mHour + ":" + (mMinute >= 10 ? "" : "0") + mMinute;
}
}
//呼叫方式
MyTime mTime = new MyTime();
mTime.setMytime(6, 30);
System.out.println(mTime);
mTime.setMytime(8, 50);
System.out.println(mTime);
mTime.passMinute(120);
System.out.println(mTime.toString());