输入年份月份,打印输出当月日历 用java

如题所述

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class java {

public static void main(String[] args) throws IOException {

BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入年份:");
String s1=buf.readLine();
System.out.println("请输入月份: ");
String s2=buf.readLine();
int year=Integer.parseInt(s1);
int month=Integer.parseInt(s2);
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 13:
System.out.println("在"+year+"年的"+month+"月有:31天");
break;
case 4:
case 6:
case 9:
case 11:
System.out.println("在"+year+"年的"+month+"月有:30天");
break;
case 2:
if(year%4==0&&year%100!=0||year%400==0)
{
System.out.println("在"+year+"年的"+month+"月有:29天");
}
else
{
System.out.println("在"+year+"年的"+month+"月有:28天");
}
break;
}
}

}
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2020-12-31
/** * * @param year 年份 * @return */ public static boolean isLoopYear(int year){ return ((year%4==0 && year%100!=0)||(year%400==0));
} /** * * @param year 年份 * @return 获得当前日期的第一天是星期几 */ public static int getWeek(int year,int month){ long days=0,sum=0; final int MAX=366,MIN=365,Day_OF_WEEK=7; for(int y=1;y<year;y++){ days=isLoopYear(y)?MAX:MIN; sum+=days; } for(int mon=1;mon<month;mon++){ if(mon!=2){ int dy=isBig(mon)?31:30; sum+=dy; } else if(mon==2){ sum+=isLoopYear(year)?29:28; } }
int week=(int) ((sum+1)%Day_OF_WEEK); return week; } /** * * @param month月份 * 判断是大月还是小月 */ public static boolean isBig(int month){ int []mon={1,3,5,7,8,10,12}; for(int i=0,size=mon.length,y=size-1;i<=size-4;i++,y--){ if(mon[i]==month||mon[y]==month) return true; } return false; } public static int getHowDay(int currMonth,int year){ if(currMonth==2){ if(isLoopYear(year)){ return 29; } else return 28; } return isBig(currMonth)?31:30; }
public static void main(final String[] args) { Scanner sc=new Scanner(System.in); System.out.println("请输入年份"); int year=sc.nextInt(); System.out.println("请输入月份"); int month=sc.nextInt(); //cal.set(2012, 9, 1); int howWeek=getWeek(year, month); System.out.println(howWeek);
String []weeks={"日","一","二","三","四","五","六"}; for(String s:weeks){ System.out.print(s+" "); } System.out.println(); int howDays=getHowDay(month,year); for(int spc=0;spc<howWeek*3-2;spc++){ System.out.print(" "); } for(int day=1;day<=howDays;day++){ System.out.print(day+" "); if((howWeek+1)%7==0){ System.out.println(); } howWeek++; } System.out.println( ); for(String s:weeks){ System.out.print(s+" "); }
}
第2个回答  2012-03-24
nhftghfhj
相似回答