1 今日任务日期类
题目要求:
程序源代码:
package Date1;
import java.util.Scanner;
public class Date1 {
int[] monthDay={31,28,31,30,31,30,31,31,30,31,30,31};
String[] weekName={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
String[] monthName={"Jan","Feb","Mar","Api","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
public static void main(String[] args){
int year,month,day;
Date1 d1 = new Date1();
Date1 d2 = new Date1(d1);
Scanner input = new Scanner(System.in);
year = input.nextInt();
month = input.nextInt();
day = input.nextInt();
d1.setYear(year);
d1.setMonth(month);
d1.setDay(day);
d1.yesterday();
d1.chineseFormat();
year = input.nextInt();
month = input.nextInt();
day = input.nextInt();
d2.setYear(year);
d2.setMonth(month);
d2.setDay(day);
d2.tomorrow();
d2.americaformat();
d2.printMonthCalendar();
input.close();
}
int year,month,day;
Date1()
{
System.out.println("Constructor run");
}
Date1(Date1 d1) //重载构造函数
{
System.out.println("CopyConstructor run");
}
void setYear(int Year)
{
year=Year;
}
int getYear()
{
return year;
}
void setMonth(int Month)
{
month=Month;
if(Month>12) month=12; //若month不在正确区间,大的设为12 小的设为1
if(Month<=1) month=1;
}
int getMonth()
{
return month;
}
void setDay(int Day)
{
int x=0;
day=Day;
if(isLeapYear()==1 && month == 2) x=1; //判断平闰年
if(Day>(monthDay[month-1]+x)) day=monthDay[month-1]+x; //润年二月,日期范围加1
if(Day<=1) day=1;
}
int getday()
{
return day;
}
void tomorrow( )
{
int y=0;
if(isLeapYear() == 1 && month == 2) y=1;
if(day==(monthDay[month-1]+y))
{
day=1;
if(month==12)
{
month=1; //如果是最后一个月,年份加1 月份为1
year++;
}
else month++;
}
else day++;
}
void yesterday( )
{
if(day==1)
{
int z=0;
if(month==1)
{
year--;
month=12; //如果是第一天的话,年份减1 月份为12
}
else month--;
int[] monthDay={31,28,31,30,31,30,31,31,30,31,30,31};
String[] weekName={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
String[] monthName={"Jan","Feb","Mar","Api","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
public static void main(String[] args){
int year,month,day;
Date1 d1 = new Date1();
Date1 d2 = new Date1(d1);
Scanner input = new Scanner(System.in);
year = input.nextInt();
month = input.nextInt();
day = input.nextInt();
d1.setYear(year);
d1.setMonth(month);
d1.setDay(day);
d1.yesterday();
d1.chineseFormat();
year = input.nextInt();
month = input.nextInt();
day = input.nextInt();
d2.setYear(year);
d2.setMonth(month);
d2.setDay(day);
d2.tomorrow();
d2.americaformat();
d2.printMonthCalendar();
input.close();
}
int year,month,day;
Date1()
{
System.out.println("Constructor run");
}
Date1(Date1 d1) //重载构造函数
{
System.out.println("CopyConstructor run");
}
void setYear(int Year)
{
year=Year;
}
int getYear()
{
return year;
}
void setMonth(int Month)
{
month=Month;
if(Month>12) month=12; //若month不在正确区间,大的设为12 小的设为1
if(Month<=1) month=1;
}
int getMonth()
{
return month;
}
void setDay(int Day)
{
int x=0;
day=Day;
if(isLeapYear()==1 && month == 2) x=1; //判断平闰年
if(Day>(monthDay[month-1]+x)) day=monthDay[month-1]+x; //润年二月,日期范围加1
if(Day<=1) day=1;
}
int getday()
{
return day;
}
void tomorrow( )
{
int y=0;
if(isLeapYear() == 1 && month == 2) y=1;
if(day==(monthDay[month-1]+y))
{
day=1;
if(month==12)
{
month=1; //如果是最后一个月,年份加1 月份为1
year++;
}
else month++;
}
else day++;
}
void yesterday( )
{
if(day==1)
{
int z=0;
if(month==1)
{
year--;
month=12; //如果是第一天的话,年份减1 月份为12
}
else month--;
if(isLeapYear()==1 && month==2) z=1;
day=monthDay[month-1]+z;
}
else day--;
}
void printMonthCalendar( )
{
int j=0,m=0,n;
if(isLeapYear()==1 && month==2) m=1;
n=monthDay[month-1]+m;
for(int i=0;i<7;i++)
{
System.out.print(weekName[i]+" "); //右对齐输出,三字符长度输出
}
System.out.println();
day=monthDay[month-1]+z;
}
else day--;
}
void printMonthCalendar( )
{
int j=0,m=0,n;
if(isLeapYear()==1 && month==2) m=1;
n=monthDay[month-1]+m;
for(int i=0;i<7;i++)
{
System.out.print(weekName[i]+" "); //右对齐输出,三字符长度输出
}
System.out.println();
int ww=weekDay();
for(int i=0;i<ww;i++)
{
System.out.printf("%4s"," "); //用空格填补空白
j++;
}
for(int i=1;i<=n;i++)
{
System.out.printf("%4s",i+" ");
j++;
if(j%7==0) System.out.println(); //每输出七次换行
}
System.out.println();
}
void chineseFormat( )
{
System.out.println(year+"年"+month+"月"+day+"日");
}
void americaformat( )
{
System.out.print(monthName[month-1]+" ");
System.out.print(day);
System.out.println(","+year);
}
int isLeapYear( )
{
if( (year%4)==0 && year%100 != 0|| (year%400)==0 ) return 1; //判断平闰年
else return 0;
}
int weekDay( )
{
int w,y,d=0;
if(year>2020)
{
y=year-2020; //以2020年1月1日为基准判断每月第一天为周几
d+=365*y+y/4+1;
for(int i=1;i<month;i++)
{
if(isLeapYear() == 1 && i==2) d++;
d+=monthDay[i-1];
}
d+=day;
w=d%7+3;
}
else if(year<2020)
{
y=2020-year-1;
d+=365*y+y/4;
for(int i=12;i>=month;i--)
{
if(isLeapYear()==1 && i==2) d++;
d+=monthDay[i-1];
}
d-=day-1;
w=10-d%7;
}
else
{
for(int i=1;i<month;i++)
{
if(isLeapYear()==1 && i==2) d++;
d+=monthDay[i-1];
}
w=d%7+3;
}
w%=7;
return w;
}
for(int i=0;i<ww;i++)
{
System.out.printf("%4s"," "); //用空格填补空白
j++;
}
for(int i=1;i<=n;i++)
{
System.out.printf("%4s",i+" ");
j++;
if(j%7==0) System.out.println(); //每输出七次换行
}
System.out.println();
}
void chineseFormat( )
{
System.out.println(year+"年"+month+"月"+day+"日");
}
void americaformat( )
{
System.out.print(monthName[month-1]+" ");
System.out.print(day);
System.out.println(","+year);
}
int isLeapYear( )
{
if( (year%4)==0 && year%100 != 0|| (year%400)==0 ) return 1; //判断平闰年
else return 0;
}
int weekDay( )
{
int w,y,d=0;
if(year>2020)
{
y=year-2020; //以2020年1月1日为基准判断每月第一天为周几
d+=365*y+y/4+1;
for(int i=1;i<month;i++)
{
if(isLeapYear() == 1 && i==2) d++;
d+=monthDay[i-1];
}
d+=day;
w=d%7+3;
}
else if(year<2020)
{
y=2020-year-1;
d+=365*y+y/4;
for(int i=12;i>=month;i--)
{
if(isLeapYear()==1 && i==2) d++;
d+=monthDay[i-1];
}
d-=day-1;
w=10-d%7;
}
else
{
for(int i=1;i<month;i++)
{
if(isLeapYear()==1 && i==2) d++;
d+=monthDay[i-1];
}
w=d%7+3;
}
w%=7;
return w;
}
}
运行截图:
2 学会设置字符宽度 System.out.printf("%3s",s+" ");
3 明天继续写题