引相关工具包:
import java.sql.SQLOutput;
import java.util.Scanner;
/**
* java.util:java的工具包
*/
键盘输入年份和月份:
public class Test1AndPerpetualCalendar {
public static void main(String[] args) {
//接收用户输入的年和月
System.out.println("请输入一个年份");
Scanner input = new Scanner(System.in);
int year = input.nextInt();//用next接受的数字等都会同化为字符串。
System.out.println("请输入月份");
int month = input.nextInt();
System.out.println("你输入的时间是" + year + "年" + month + "月");
求键盘输入的日期共有多少天(判断平润年):
判断平润年
公式:(year % 4 ==0 && year%100!=0 ) ||(year%400==0)
满足为闰年,2月有29天。
求输入日期距离万年历起点1900年的整年一共的天数:
//求整年天数
int yearSum = 0;
for (int i = 1900; i < year; i++) {
if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0) {
yearSum += 366;
} else {
yearSum += 365;
}
}
System.out.println(year + "距离1900.1.1号间隔" + yearSum + "天");
求输入日期距离万年历起点1900年1月1日的整月一共的天数:
//求整月天数
int monthSum=0;
for (int i = 1; i < month; i++) {//1开始month结束,不包括month
if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 ) {//判断是否为大月
monthSum+= 31;
} else if (i == 4 || i == 6 || i == 9 || i == 11) { //判断是否为小月
monthSum+= 30;
} else {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
monthSum+= 29;
} else {
monthSum+= 28;
}
}
求输入日期当前月总共天数:
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {//判断是否为大月
days = 31;
} else if (month == 4 || month == 6 || month == 9 || month == 11) { //判断是否为小月
days = 30;
} else {//判断是否为润年
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
days = 29;
} else {
days = 28;
}
}
System.out.println(year + "年" + month + "月有" + days + "天");
判断输入日期月份的1号为星期几:
//求键盘输入月的1号为星期几
int week=(yearSum+monthSum+1)%7;//或者写int week =(yearSum+monthSum+1)%7==0?7:(yearSum+monthSum+1)%7;
if(week==0){
week=7;
}
System.out.println("1号是星期"+week);
打印万年历:
//打印日历
System.out.println("一\t二\t三\t四\t五\t六\t日");
//打印空格
for(int i =0;i<week-1;i++){
System.out.print("\t");
}
for(int i=1;i<=days;i++){
System.out.print(i+"\t");//print后打印对象字符且需要转移符时需要+“\t”
if((yearSum+monthSum+i)%7==0){
System.out.println();
}
}
}
}
完整代码:
import java.sql.SQLOutput;
import java.util.Scanner;
/**
* java.util:java的工具包
*/
public class Test1AndPerpetualCalendar {
public static void main(String[] args) {
//接收用户输入的年和月
System.out.println("请输入一个年份");
Scanner input = new Scanner(System.in);
int year = input.nextInt();//用next接受的数字等都会同化为字符串。
System.out.println("请输入月份");
int month = input.nextInt();
System.out.println("你输入的时间是" + year + "年" + month + "月");
//辨别用户输入的月份总共有多少天
/**
* 大月 :1 3 5 7 8 10 12 31天
* 小月: 4 6 9 11 30天
* 特殊2月 : 闰月29天 平年28天
*/
int days = 0;//创建全局变量days存储总共天数
//选择结构判断输入月份属于什么
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {//判断是否为大月
days = 31;
} else if (month == 4 || month == 6 || month == 9 || month == 11) { //判断是否为小月
days = 30;
} else {//判断是否为润年
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
days = 29;
} else {
days = 28;
}
}
System.out.println(year + "年" + month + "月有" + days + "天");
//计算输入年分的月分的1号是星期几
//求整年天数
int yearSum = 0;
for (int i = 1900; i < year; i++) {
if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0) {
yearSum += 366;
} else {
yearSum += 365;
}
}
System.out.println(year + "距离1900.1.1号间隔" + yearSum + "天");
//求整月天数
int monthSum=0;
for (int i = 1; i < month; i++) {//1开始month结束,不包括month
if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 ) {//判断是否为大月
monthSum+= 31;
} else if (i == 4 || i == 6 || i == 9 || i == 11) { //判断是否为小月
monthSum+= 30;
} else {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
monthSum+= 29;
} else {
monthSum+= 28;
}
}
}
//求键盘输入月的1号为星期几
int week=(yearSum+monthSum+1)%7;//或者写int week =(yearSum+monthSum+1)%7==0?7:(yearSum+monthSum+1)%7;
if(week==0){
week=7;
}
System.out.println("1号是星期"+week);
//打印日历
System.out.println("一\t二\t三\t四\t五\t六\t日");
//打印空格
for(int i =0;i<week-1;i++){
System.out.print("\t");
}
for(int i=1;i<=days;i++){
System.out.print(i+"\t");//print后打印对象字符且需要转移符时需要+“\t”
if((yearSum+monthSum+i)%7==0){
System.out.println();
}
}
}
}