输出某年某月的日历页,通过 键盘输入将年份和月份时间传递到程序中。

输出某年某月的日历页,通过 键盘输入将年份和月份时间传递到程序中。

 

import java.util.Calendar;


public class Test01_GetDate {

/**
* 1、输出某年某月的日历页,通过 键盘输入将年份和月份时间传递到程序中。
*/
public static void main(String[] args) {
System.out.println(getCalendar(2017, 7));
}

public static String getCalendar(int year,int month){
StringBuffer sb = new StringBuffer();
sb.append("一\t二\t三\t四\t五\t六\t日\n");
//月份要减一
month -=1;

Calendar cr = Calendar.getInstance();
//设置时间  本月第一天
cr.set(year, month, 0);

//设置一个星期的第一天为星期日
//cr.setFirstDayOfWeek(Calendar.MONDAY);

//本月有多少天
int days = 0;
switch (month) {
case Calendar.JANUARY://1
case Calendar.MARCH://3
case Calendar.MAY://5
case Calendar.JULY://7
case Calendar.AUGUST://8
case Calendar.OCTOBER://10
case Calendar.DECEMBER://12
days = 31;
break;
case Calendar.APRIL://4
case Calendar.JUNE://6
case Calendar.SEPTEMBER://9
case Calendar.NOVEMBER://11
days = 30;
break;
case Calendar.FEBRUARY://2
days = 28;
//能被4整除且又能不能被100整除 是闰年 能直接被400整除也是闰年
if((year % 4 == 0 && year%100 !=0) || year%400==0){
days = 29;
}
break;
default:
break;
}

//当前月份第一天是星期几
int week = cr.get(Calendar.DAY_OF_WEEK);
System.out.println(week);
//打印空出来的日期
for (int i = 1; i < week; i++) {
sb.append("\t");
}
//打印实际日期
for (int i = 1; i <= days; i++) {
sb.append(i+"\t");
if((week+i-1)%7 == 0){
sb.append("\n");
}
}
return sb.toString();
}
}

 

  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是Java代码实现: ``` import java.util.Scanner; public class Calendar { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入年份:"); int year = scanner.nextInt(); System.out.print("请输入月份:"); int month = scanner.nextInt(); // 计算当月第一天是星期几 int firstDayOfWeek = getFirstDayOfWeek(year, month); // 打印日历头部 printCalendarHeader(year, month); // 打印日历主体 printCalendarBody(firstDayOfWeek, getDaysOfMonth(year, month)); } /** * 获取某年某月的天数 */ private static int getDaysOfMonth(int year, int month) { int[] monthDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (month == 2 && isLeapYear(year)) { return 29; } return monthDays[month - 1]; } /** * 判断某年是否为闰年 */ private static boolean isLeapYear(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } /** * 获取某年某月第一天是星期几 */ private static int getFirstDayOfWeek(int year, int month) { int[] monthDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // 计算1900年1月1日至当年当月1日的天数 int days = 0; for (int i = 1900; i < year; i++) { days += isLeapYear(i) ? 366 : 365; } for (int i = 1; i < month; i++) { days += monthDays[i - 1]; if (i == 2 && isLeapYear(year)) { days++; } } // 计算当月1日是星期几 return (days + 1) % 7; } /** * 打印日历头部 */ private static void printCalendarHeader(int year, int month) { System.out.println(String.format("%d年%d月", year, month)); System.out.println("日 一 二 三 四 五 六"); } /** * 打印日历主体 */ private static void printCalendarBody(int firstDayOfWeek, int daysOfMonth) { int dayCount = 1; // 打印第一行 for (int i = 0; i < firstDayOfWeek; i++) { System.out.print(" "); } for (int i = firstDayOfWeek; i < 7; i++) { System.out.print(String.format("%2d ", dayCount)); dayCount++; } System.out.println(); // 打印其它行 while (dayCount <= daysOfMonth) { for (int i = 0; i < 7; i++) { if (dayCount <= daysOfMonth) { System.out.print(String.format("%2d ", dayCount)); dayCount++; } else { System.out.print(" "); } } System.out.println(); } } } ``` 使用时,通过键盘输入年份月份程序输出该月的日历
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值