?? date.java
字號(hào):
package myPackage;
import java.io.*;
public class Date {
private int year; // 年
private int month; // 月
private int day; // 日
private int amount ; // 用于 日期+整數(shù)
private int count; // 日期減日期得到的天數(shù)
public Date( ){ //定義缺省值的構(gòu)造方法
setDate(2004,10,5);
}
//定義具有指定值的構(gòu)造方法
public Date(int year,int month,int day){
setDate(year,month,day);
}
public void setDate(int year,int month,int day){ //用于設(shè)置年月日
this.year=year;
this.month=month;
this.day=day;
}
public int getYear( ){ //獲取年
return year;
}
public int getMonth(){ //獲取月
return month ;
}
public int getDay( ){ //獲取日
return day ;
}
public void displayDate(){ // 輸出年月日
System.out.println(year+"年"+month+"月"+day+"日");
}
public void DayOfMonth(int month,int year){ //判斷某年某月的天數(shù)
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
amount=31;
if(month==4||month==6||month==9||month==11)
amount=30;
if(month==2)
{ if(year%4==0) //判斷是否為閏年
{ if(year%100!=0)
amount=29;
else
{if(year%400==0)
amount=29;
else
amount=28;
}
}
else
amount=28;
}
}
public void Add (int days) { //在某個(gè)日期上加上一整數(shù),計(jì)算其后的日期
while(days!=0){
DayOfMonth(month,year); // 將天數(shù)一天一天加到原日期里,
if(amount!=day) // 若滿一個(gè)月則月份加一
day++; // 若滿一年則年份加一
else
{ day=1; // 直到要加的整數(shù)減為零
if(month!=12)
month++;
else
{ month=1;
year++;
}
}
days--;
}
}
public void Sub(int a,int b,int c){ //計(jì)算兩個(gè)日期相差的天數(shù)
int temp; //將a,b,c設(shè)置為較大的日期,以便于計(jì)算
if(a<year||(a==year&&b<month)||(a==year&&b==month&&c<day))
{ temp=a;a=year;year=temp;
temp=b;b=month;month=temp;
temp=c;c=day;day=temp;
}
System.out.print(year+"年"+month+"月"+day+"日與"+a+"年"+b+"月"+c+"日");
count=0;
while(a!=year||b!=month||c!=day) // 用較小的日期連續(xù)加一,直到兩個(gè)日期相等
{ count++;
DayOfMonth(month,year); //它是加法的逆運(yùn)算,算法一樣,只是控制循環(huán)的條件不同
if(amount!=day)
day++;
else
{ day=1;
if(month!=12)
month++;
else
{ month=1;
year++;
}
}
}
System.out.println("相隔"+count+"天");
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -