import java.util.Scanner;
public class BookDemo6_12 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// 提示输入一个年份
System.out.println(“请输入一个完整的年份(e.g., 2012):”);
int year = input.nextInt();
// 提示输入一个月份
System.out.println(“请输入一个1~12的月份”);
int month = input.nextInt();
// 打印这年这个月的日历的方法
printMonth(year, month);
}
public static void printMonth(int year, int month) {
// 打印日历的标题
printMonthTitle(year, month);
// 打印日历的内容
printMonthBody(year, month);
}
// 显示日历标题的方法
public static void printMonthTitle(int year, int month) {
System.out.println(” ” + getMonthName(month) + ” ” + year);
System.out.println(” —————————”);
System.out.println(” Sun Mon Tue Wed Thu Fri Sat”);
}
// 获取月份的英文名
public static String getMonthName(int month) {
String monthName = “”;
switch (month) {
case 1:
monthName = “January”;
break;
case 2:
monthName = “February”;
break;
case 3:
monthName = “March”;
break;
case 4:
monthName = “April”;
break;
case 5:
monthName = “May”;
break;
case 6:
monthName = “June”;
break;
case 7:
monthName = “July”;
break;
case 8:
monthName = “August”;
break;
case 9:
monthName = “September”;
break;
case 10:
monthName = “October”;
break;
case 11:
monthName = “November”;
break;
case 12:
monthName = “December”;
}
return monthName;
}
// 显示日历内容
public static void printMonthBody(int year, int month) {
// 获取这个月最开始的一天是星期几
int startDay = getStartDay(year, month);
// 获取这个月有几天
int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month);
// 打印月份表
int i = 0;
for (i = 0; i < startDay; i++) {
System.out.print(” “);
}
for (i = 1; i <= numberOfDaysInMonth; i++) {
System.out.printf(“%4d”, i);
if ((i + startDay) % 7 == 0) {
System.out.println();
}
}
System.out.println();
}
// 获取这个月的最开始一天是星期几
public static int getStartDay(int year, int month) {
// 1800年一月一日是星期三
final int START_DAY_FOR_JAN_1_1800 = 3;
// 获取1800年1月1日和输入月份相差的总天数
int totalNumberOfDays = getTotalNumberOfDays(year, month);
// 返回这年这个月的第一天是星期几
return (totalNumberOfDays + START_DAY_FOR_JAN_1_1800) % 7;
}
// 获取相差总天数的方法
public static int getTotalNumberOfDays(int year, int month) {
int total = 0;
// 获取相差天数
for (int i = 1800; i < year; i++) {
if (isLeapYear(i)) {
total = total + 366;
} else {
total = total + 365;
}
}
for (int i = 1; i < month; i++) {
total = total + getNumberOfDaysInMonth(year, i);
}
return total;
}
// 获取这个月有几天的方法 31 30 29 28
public static int getNumberOfDaysInMonth(int year, int month) {
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
return 31;
}
if (month == 4 || month == 9 || month == 9 || month == 11) {
return 30;
}
if (month == 2) {
return isLeapYear(year) ? 29 : 28;
}
return 0;
}
// 判断是否为闰年的方法
public static boolean isLeapYear(int year) {
return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
}
}

显示2012年之前的月份日历代码相关推荐

  1. linux显示2012年11月的日历,LINUX命令cal-系统管理-显示当前日历或指定日期的日历...

    cal命令 用于显示当前日历,或者指定日期的日历. 语法 cal(选项)(参数) 选项 -l:显示单月输出: -3:显示临近三个月的日历: -s:将星期日作为月的第一天: -m:将星期一作为月的第一天 ...

  2. 日历代码 谷歌无法显示当日_在Windows日历中显示您的Google日历

    日历代码 谷歌无法显示当日 Windows Vista includes a built-in calendar application that's pretty slick, but as an ...

  3. c语言编程显示单月日历,C语言日历显示日历显示br/编程反复显示2013年各月份日历 爱问知识人...

    日历显示 编程反复显示2013年各月份日历.(程序可以设定2013年1月1号为星期二). #include void main() { int t,h,day,i,y; char c; while(1 ...

  4. asp前端日历_ASP完成的日历代码实例程序

    ASP完成的日历代码实例程序 td { font-family: "宋体"; font-size:9pt} '以下为ASP中经过该日历算法完成的详细代码 '先判别能否指定了一个年份 ...

  5. python打印日历_python输出指定月份日历的方法

    python输出指定月份日历的方法 本文实例讲述了python输出指定月份日历的方法.分享给大家供大家参考.具体实现方法如下: #!/usr/bin/python import calendar ca ...

  6. asp前端日历_asp日历代码

    asp日历代码 > ' 要调用的函数声明 '根据年份及月份得到每月的总天数 Function GetDaysInMonth(iMonth, iYear) Select Case iMonth C ...

  7. vb如何调用计算机日历,VB日历代码.doc

    VB日历代码 Type LunarInfo iLorSMonth(13) As Integer '定义阴历大小月标志数组 iTotalDays As Integer '定义春节开始的天数 iLeapM ...

  8. 小程序swiper月份日历滑动

    在小程序里用swiper做了一个月份日历,先上图叭直接贴代码叭: <view class="container"><view class="calend ...

  9. java如何打印当前月份日历_java打印指定年月份的日历

    本文实例为大家分享了java打印指定年月份日历的具体代码,供大家参考,具体内容如下 做了一个小的日历打印,没有使用数组,所以不能对日期进行操作,用了单个for循环打印,对初学者来说应该好理解点 工具类 ...

最新文章

  1. Cocos2d-JS事件处理机制
  2. linux下常用文件传输命令(转)
  3. 随手记:Ubuntu16.04.1安装Chrome浏览器以及解决root下无法启动的问题
  4. 数据库变为可疑_SQL SERVER 数据库被标记为“可疑”的解决办法
  5. 天空之城:拉马努金式思维训练法
  6. pycharm 添加已有文件
  7. 【立项建议书】苏科大校园app立项建议书
  8. Android android-times-square一款好用的日历控件
  9. 华硕ASUS 笔记本 改WIN7 BIOS 设置详解
  10. R语言数据可视化包ggplot2画图之散点图
  11. Layabox开发微信小游戏好友排行榜功能流程
  12. 笔记本亮度无法调节的可能原因
  13. 带你了解云计算的优势
  14. 区块链量化投资系列课程(4) - 动态平衡策略
  15. OpenCV探索之路(二十六):如何去除票据上的印章
  16. 基于javaweb的房地产客户关系管理系统(java+jsp+javascript+servlet+mysql)
  17. Linux shell 常用代码片断
  18. C# 实现人员信息管理软件(增删查改操作)
  19. 计算机为什么检测不到u盘启动项,bios设置u盘为第一启动项后检测不到怎么办
  20. 怎么画动漫人物握剑姿势?该怎么画?

热门文章

  1. armlink 第四章 scatter文件举例
  2. iPod nano 7代转存歌曲
  3. 6种实现前端下载图片的方法汇总
  4. 京东云部署Javaweb项目和搭建个人博客
  5. angular控制器是什么?
  6. uni-app小程序无感登录逻辑实现
  7. Udesk工单SDK(五):【管理】-【我的工单】详解
  8. 卷积神经网络知识点总结
  9. matlab如何造带限高斯白噪声,如何用Matlab产生高斯白噪?
  10. PS如何制作动态图烟花效果GIF礼花