日期类
package day01;
class Day {
private int year;
private int month;
private int data;
// 构造函数
Day(int year,int month,int data){
this.year = year;
this.month = month;
this.data = data;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public void setYear(int year) {
this.year = year;
}
public int getYear() {
return year;
}
void set(int year,int month,int data){
this.year = year;
this.month = month;
this.data = data;
}
int getBirthday(){ // 计算日期是星期几的方法
int y = year; // 0 为周末 1为周一 ......以此类推
int m = month;
if(m == 1 || m == 2){ // 若为一二月分时,年减一 月加上12个月
y--;
m += 12;
}
return (y + y / 4 - y / 100 +y / 400 +(13 * m + 8) / 5 + data) % 7;
}
}
Day使用示例
package day01;
import java.util.Scanner;
class DayTester {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] week = {"周日","周一","周二","周三","周四","周五","周六"};
System.out.println("请输入您的阳历生日日期!");
System.out.println("年:");
int y = scanner.nextInt();
System.out.println("月:");
int m = scanner.nextInt();
System.out.println("日:");
int d = scanner.nextInt();
Day birthday = new Day(y,m,d);
System.out.println("您的生日:" + birthday.getYear() + "月" + birthday.getMonth() + "日" + birthday.getData() + "是" + week[birthday.getBirthday()]);
}
}
|