[java]代码库
package com.days;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Test {
int startYear;
int endYear;
public Test(int startYear, int endYear){
this.startYear=startYear;
this.endYear=endYear;
}
public long howDays(){
long day=0;
for(int year=startYear;year<=endYear;year++){
if(year%4==0&&year%100!=0||year%100==0){
day+=366;
}else{
day+=365;
}
}
return day;
}
public long howYears(){
return endYear-startYear+1;
}
int direction=1;
String str="";
public void timeRili(){
long days=howDays();
int startWeek=startYearStartMonthStartDay();
for(int year=startYear;year<=endYear;year++){
System.out.println("\n"+year+"年");
int monthDay=0;
for(int month=1;month<=12;month++){
System.out.println("\n"+month+"月");
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
monthDay=31;
break;
case 4:
case 6:
case 9:
case 11:
monthDay=30;
break;
case 2:
if(year%4==0&&year%100!=0||year%100==0){
monthDay=29;
}else{
monthDay=28;
}
break;
default:
break;
}
if(year==startYear&&month==1){
System.out.println("日\t一\t二\t三\t四\t五\t六");
for(int i=1-startWeek;i<=monthDay;i++){
int kongQue=i;
if(i<=0){
str+="\t";
}else{
str+=kongQue+"\t";
}
if(direction==7){
System.out.println(str);
str="";
direction=0;
}
direction++;
}
}else{
System.out.println("日\t一\t二\t三\t四\t五\t六");
for(int i=1;i<=monthDay;i++){
int kongQue=i;
str+=kongQue+"\t";
if(direction==7){
System.out.println(str);
str="";
direction=0;
}else{
if(year==endYear&&month==12&&i==31){
if(direction<7){
System.out.println(str);
str="";
direction=0;
}
}
}
direction++;
}
}
}
}
}
int startYearStartMonthStartDay() {
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
Date date=null;
try{
date = simpleDateFormat.parse(startYear + "-01-01");
}catch (ParseException e){
e.printStackTrace();
}
Calendar ca = Calendar.getInstance();
ca.setTime(date);
return ca.get(Calendar.DAY_OF_WEEK) - 1;
}
}
package com.days;
import java.util.Scanner;
public class Main {
public static void main(String args[]){
System.out.println("请输入开始的年份:");
Scanner scanner=new Scanner(System.in);
int startYear=scanner.nextInt();
System.out.println("请输入结束的年份:");
int endYear=scanner.nextInt();
Test test=new Test(startYear, endYear);
System.out.println("多少年:"+test.howYears());
System.out.println("多少天:"+test.howDays());
System.out.println("开始年的第一天是星期几:"+test.startYearStartMonthStartDay());
test.timeRili();
Test test1=new Test(endYear, endYear);
}
}
by: 发表于:2017-08-04 14:55:03 顶(0) | 踩(0) 回复
??
回复评论