这个是从1900年1月1日 开始推算的...只要你输入年份与月份 就能输出 那年那月的日历....
比较考验你的逻辑运算能力,已经java基本语法的运用...
[Java] 纯文本查看 复制代码 import java.util.Scanner;
public class Date {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份");
int year = sc.nextInt()-1;
System.out.println("请输入月份");
int num = sc.nextInt();
int dayDay=dayYue(year,num)+dayYear(year);
int sum =dayMon(num,year);
riLi(dayDay,sum);
}
//打印日历
public static void riLi(int dayDay,int sum){
String[] str ={"星期天","星期一","星期二","星期三","星期四","星期五","星期六"};
for(int x=0;x<7;x++){
System.out.print(str[x]+"\t");
}
System.out.println();
int space=dayDay%7;
for(int x=1;x<=sum+space;x++){
if(x<=space){
System.out.print(" "+"\t");
}else{
System.out.print((x-space)+"\t");
}
if(x%7==0){
System.out.println();
}
}
}
//输出当年每月几天
public static int dayMon(int num,int year){
int dayY=0;
if(num>2&&num<8){
if(num%2==0){
dayY+=30;
}else{
dayY+=31;
}
}else if(num>=8){
if(num%2==0){
dayY+=31;
}else{
dayY+=30;
}
}else if(num==1){
dayY+=31;
}else if(num==2){
if((year+1)%4==0){
dayY+=29;
}else{
dayY+=28;
}
}
return dayY;
}
//输入几几年几月份离离当年1月1日多少天
public static int dayYue(int year,int num){
int day =0;
if(num>7){
int x=num-8;
for(int i =0;i<x;i++){
if(i%2==0){
day+=31;
}else{
day+=30;
}
}
if((year+1)%4==0){
day+=213;
}else{
day+=212;
}
}else if(num>2){
int x=num-3;
for(int i =0;i<x;i++){
if(i%2==0){
day+=31;
}else{
day+=30;
}
}
if((year+1)%4==0){
day+=29+31;
}else{
day+=28+31;
}
}else if(num>1){
day+=31;
}
return day;
}
//输入几几年1月1日离1900年1月1日多少天
public static int dayYear(int year){
int y = 1900;
int juY=year-y;
int runY=juY/4+1;
int puY=juY-runY+1;
int day = runY*366+puY*365;
int yuandan=day%7;
return day;
}
}
后面学Calendar类是,做起来会简单一些.....这个逻辑推算.花了4个小时...好累..
|