学习Java的第十二天
学习内容:实验一Date类
实验代码:
import java.util.Scanner;
public class Date {
public int monthDay[]={31,28,31,30,31,30,31,31,30,31,30,31};
public String weekName[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
public String monthName[]={"Jan","Feb","Mar","Api","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
private int year;
private int month;
private int day;
public Date(int year,int month,int day) { //构造方法
this.year=year;
this.month=month;
this.day=day;
System.out.println("Constructor run");
}
public void setMonth(int month) { //设置月份
this.month=month;
}
public int getMonth() { //返回月份
return month;
}
public void setDay(int day) { //设置天数
this.day=day;
}
public int getDay() { //返回天数
return day;
}
public void setYear(int year) { //设置年份
this.year=year;
}
public int getYear() { //返回年份
return year;
}
void tomorrow() { //日期加一
if(((year%4==0)&&(year%100!=0))||(year%400==0)) {//判断闰年
if(month==12&&day==31) {
year+=1;
month=1;
day=1;
}
else if((month==1||month==3||month==5||month==7||month==8||month==10)&&day==31) {
month+=1;
day=1;
year=year;
}
else if((month==4||month==6||month==9||month==11)&&day==30) {
month+=1;
day=1;
year=year;
}
else if(month==2&&day==29) {
month+=1;
day=1;
}
else day+=1;
}
else {
if(month==12&&day==31) {
year+=1;
month=1;
day=1;
}
else if((month==1||month==3||month==5||month==7||month==8||month==10)&&day==31) {
month+=1;
day=1;
year=year;
}
else if((month==4||month==6||month==9||month==11)&&day==30) {
month+=1;
day=1;
year=year;
}
else if(month==2&&day==28) {
month+=1;
day=1;
}
else {
day+=1;
}
}
}
public void yesterday() { //日期减一
if(((year%4==0)&&(year%100!=0))||(year%400==0)) {//判断闰年
if(day==1) {
if(month==1) {
year-=1;
month=12;
day=31;
}
else if(month==3) {
day=29;
month=2;
}
else if(month==2||month==4||month==6||month==8||month==9||month==11) {
day=31;
month-=1;
}
else if(month==5||month==7||month==10||month==12) {
day=30;
month-=1;
}
}
else day-=1;
}
else {
if(day==1) {
if(month==1) {
year-=1;
month=12;
day=31;
}
else if(month==3) {
day=28;
month=2;
}
else if(month==2||month==4||month==6||month==8||month==9||month==11) {
day=31;
month-=1;
}
else if(month==5||month==7||month==10||month==12) {
day=30;
month-=1;
}
}
else day-=1;
}
}
public int weekDay()
{
int a=month,b=year;
if(month==1||month==2)
{
a=month+12;
b=year-1;
}
return (day+2*a+3*(a+1)/5+b+b/4-b/100+b/400+1)%7;
}
public int isLeapYear() { //判断闰年
if(((year%4==0)&&(year%100!=0))||(year%400==0)) {
return 1;
}
else return 0;
}
public void printMonthCalendar() //打印日历
{
System.out.println("SunMonTueWedThuFriSat");
int b;
b=weekDay()-(day-1)%7;
for(int j=0;j<b;j++)
{
System.out.print(" ");
}
int a=0;
if(((year%4==0)&&(year%100!=0))||(year%400==0)&&month==2)
{
a=29;
}
else a=monthDay[month-1];
for(int h=1;h<=a;h++)
{
System.out.printf("%3d",h);
b++;
if(b==7)
{
System.out.println();
b=0;
}
}
System.out.println();
}
public void chineseFormat() //显示中式日期
{
System.out.println(year+"年"+month+"月"+day+"日");
}
public void americaformat() //显示美式日期
{
System.out.println(monthName[month-1]+" "+day+","+year);
}
public static void main(String[] args) { //主方法
// TODO 自动生成的方法存根
int year,month,day;
Date d1=new Date(0, 0, 0);
Date d2=d1;
System.out.println("请输入日期:");
Scanner sc=new Scanner(System.in);
year=sc.nextInt();
month=sc.nextInt();
if(month>12) {
month=12;
}
else if(month<1) {
month=1;
}
day=sc.nextInt();
if(((year%4==0)&&(year%100!=0))||(year%400==0)) { //控制日期合法输入
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) {
if(day<1) {
day=1;
}
else if(day>31) {
day=31;
}
}
if(month==2) {
if(day<1) {
day=1;
}
else if(day>29) {
day=29;
}
}
if(month==4||month==6||month==9||month==11) {
if(day<1) {
day=1;
}
else if(day>30) {
day=30;
}
}
}
else {
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) {
if(day<1) {
day=1;
}
else if(day>31) {
day=31;
}
}
if(month==2) {
if(day<1) {
day=1;
}
else if(day>28) {
day=28;
}
}
if(month==4||month==6||month==9||month==11) {
if(day<1) {
day=1;
}
else if(day>30) {
day=30;
}
}
}
d1.setYear(year);
d1.setMonth(month);
d1.setDay(day);
System.out.println("昨天为:");
d1.yesterday();
d1.chineseFormat();
System.out.println("请输入日期:");
Scanner sc1=new Scanner(System.in);
year=sc1.nextInt();
month=sc1.nextInt();
if(month>12) {
month=12;
}
else if(month<1) {
month=1;
}
day=sc1.nextInt();
if(((year%4==0)&&(year%100!=0))||(year%400==0)) {
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) {
if(day<1) {
day=1;
}
else if(day>31) {
day=31;
}
}
if(month==2) {
if(day<1) {
day=1;
}
else if(day>29) {
day=29;
}
}
if(month==4||month==6||month==9||month==11) {
if(day<1) {
day=1;
}
else if(day>30) {
day=30;
}
}
}
else {
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) {
if(day<1) {
day=1;
}
else if(day>31) {
day=31;
}
}
if(month==2) {
if(day<1) {
day=1;
}
else if(day>28) {
day=28;
}
}
if(month==4||month==6||month==9||month==11) {
if(day<1) {
day=1;
}
else if(day>30) {
day=30;
}
}
}
d2.setYear(year);
d2.setMonth(month);
d2.setDay(day);
System.out.println("明天为:");
d2.tomorrow();
d2.americaformat();
d2.printMonthCalendar();
}
}
运行截图:
遇到的问题:无
明天任务:包装类