📄 countdays.java~1~
字号:
/**
* <p>Title: 宾馆官理系统</p>
* <p>Description: 第七章实例</p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author 苏年乐
* @version 1.0
*/
/**
* 计算入住天数
*/
public class Countdays {
private int total = 0;
public Countdays(String indate, String outdate) {
//System.out.println("\nhahahahaha");
mission(indate, outdate);
}
public boolean isleapyear(int year) { //判断闰年
if (year % 4 == 0)return true;
else
return false;
}
public int yeardays(int a, int b) {
int yeardays = 0;
a++;
while (a < b) {
if (isleapyear(a))
yeardays = yeardays + 366;
else
yeardays = yeardays + 365;
a++;
}
return yeardays;
}
public int judge_february(int year) { //返回二月的天数
if (isleapyear(year))
return 29;
else
return 28;
}
public int toyearbeginning(int year, int month, int day) { //返回当前月月末到年初的天数
switch (month - 1) {
case 0:
return day;
case 1:
return 31 + day;
case 2:
return 31 + judge_february(year) + day;
case 3:
return 62 + judge_february(year) + day;
case 4:
return 92 + judge_february(year) + day;
case 5:
return 123 + judge_february(year) + day;
case 6:
return 153 + judge_february(year) + day;
case 7:
return 184 + judge_february(year) + day;
case 8:
return 215 + judge_february(year) + day;
case 9:
return 245 + judge_february(year) + day;
case 10:
return 276 + judge_february(year) + day;
case 11:
return 306 + judge_february(year) + day;
case 12:
return 337 + judge_february(year) + day;
default:
return 0;
}
}
public int toyearend(int year, int month, int day) { //算上当前天
if (isleapyear(year))
return 366 - toyearbeginning(year, month, day) + 1;
else
return 365 - toyearbeginning(year, month, day) + 1;
}
public void compute_total(int yeara, int yearb, int montha, int monthb,
int daya, int dayb) {
if (yeara != yearb)
total = yeardays(yeara, yearb) + toyearbeginning(yearb, monthb, dayb) +
toyearend(yeara, montha, daya);
else
total = toyearbeginning(yearb, monthb, dayb) -
toyearbeginning(yeara, montha, daya) + 1; //算上当前天
}
public int getdays() {
return total;
}
public void mission(String a, String b) {
int yeara;
int yearb;
int montha;
int monthb;
int daya;
int dayb;
yeara = Integer.parseInt(a.substring(0, 4));
yearb = Integer.parseInt(b.substring(0, 4));
montha = Integer.parseInt(getmonstr(a));
monthb = Integer.parseInt(getmonstr(b));
daya = Integer.parseInt(getdaystr(a));
dayb = Integer.parseInt(getdaystr(b));
compute_total(yeara, yearb, montha, monthb, daya, dayb);
}
public String getmonstr(String t) {
if (!t.substring(6, 7).equals("-"))
return t.substring(5, 7);
else
return t.substring(5, 6);
}
public String getdaystr(String t) {
int i = t.length() - 1;
while (t.charAt(i) != '-')
i--;
return t.substring(i + 1, t.length());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -