编写一个程序,显示给定年月的日历。程序提示用户输入年份和月份,然后显示该月的整个日历。

  代码:

  1. import java.util.Scanner;
  2. public class PrintCalendar{
  3. public static void main(String[] args){
  4. Scanner input=new Scanner(System.in);
  5.  
  6. System.out.print("Enter full year(e.g.,2011): ");
  7. int Year=input.nextInt();
  8.  
  9. System.out.print("Enter month as a number between 1 and 12: ");
  10. int Month=input.nextInt();
  11.  
  12. // int year=inputYear();
  13. //int month=inputMonth();
  14. printMonth(Year, Month);
  15. }
  16. /*
  17. public static int inputYear(){
  18. boolean flag=true;
  19. Scanner input=new Scanner(System.in);
  20. while(flag){
  21. System.out.print("Enter full year(e.g.,2011): ");
  22. int iYear=input.nextInt();
  23. if(iYear>=1800){
  24. flag = false;
  25. }
  26. }
  27. return iYear;
  28. }
  29.  
  30. public static int inputMonth(){
  31. boolean flag=true;
  32. Scanner input=new Scanner(System.in);
  33. while(flag){
  34. System.out.print("Enter month as a number between 1 and 12: ");
  35. int iMonth=input.nextInt();
  36. if(iMonth>=1 && iMonth<=12){
  37. flag = false;
  38. }
  39. }
  40. return iMonth;
  41. }*/
  42.  
  43. public static void printMonth(int year, int month){
  44. printMonthTitle(year, month);
  45. printMonthBody(year,month);
  46. }
  47.  
  48. public static void printMonthTitle(int year, int month){
  49. System.out.println("\t" + getMonthName(month)+"\t" +year);
  50. System.out.println("-----------------------------------");
  51. System.out.println("Mon Tue Wed Thu Fri Sat Sun");
  52. }
  53. public static String getMonthName(int month){
  54. String monthName="";
  55. switch (month){
  56. case 1: monthName="January"; break;
  57. case 2: monthName="February"; break;
  58. case 3: monthName="March"; break;
  59. case 4: monthName="April"; break;
  60. case 5: monthName="May"; break;
  61. case 6: monthName="June"; break;
  62. case 7: monthName="July"; break;
  63. case 8: monthName="August"; break;
  64. case 9: monthName="September"; break;
  65. case 10: monthName="October"; break;
  66. case 11: monthName="November"; break;
  67. case 12: monthName="December"; break;
  68. }
  69.  
  70. return monthName;
  71. }
  72.  
  73. public static void printMonthBody(int year, int month){
  74.  
  75. //Get start day of the week for the first date int the month
  76. int startDay=getStartDay(year,month);
  77.  
  78. //Get number of days in the month
  79. int numberOfDaysInMonth = getNumberOfDaysInMonth(year,month);
  80.  
  81. //Pad space before the first day of the month
  82. int i=0;
  83. for(i=1; i< startDay; i++){
  84. System.out.print(" ");
  85. }
  86. for(i=1; i<=numberOfDaysInMonth;i++){
  87. System.out.printf("%-4d", i);
  88.  
  89. if((i+startDay-1)%7==0)
  90. System.out.println();
  91. }
  92. System.out.println();
  93. }
  94.  
  95. /** Get the start day of month/1/year */
  96. public static int getStartDay(int year, int month){
  97. final int START_DAY_FOR_JAN_1_1800 = 3;
  98.  
  99. //Get the total number of days from 1/1/1800 to month/1/year
  100. int totalNumberOfDays= getTotalNumberOfDays(year,month);
  101.  
  102. //Return the start day for month/1/year
  103. return (totalNumberOfDays+START_DAY_FOR_JAN_1_1800)%7;
  104. }
  105.  
  106. /** Get the total number of days from January 1, 1800; */
  107. public static int getTotalNumberOfDays(int year, int month){
  108. int total = 0;
  109.  
  110. //get total number of days from 1800 to 1/1/year
  111. for(int i = 1800; i<year; i++){
  112. if(isLeapYear(i))
  113. total = total + 366;
  114. else
  115. total = total + 365;
  116. }
  117.  
  118. //add days from January to the month prior to the calendar month
  119. for(int i = 0; i<month;i++)
  120. total +=getNumberOfDaysInMonth(year, i);
  121.  
  122. return total;
  123. }
  124.  
  125. /** Get the number of days in a month */
  126. public static int getNumberOfDaysInMonth(int year, int month){
  127. if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
  128. return 31;
  129. if(month==4||month==6||month==9||month==11)
  130. return 30;
  131. if(month==2)
  132. return isLeapYear(year)?29:28;
  133. return 0;
  134. }
  135.  
  136. /** Determine if it is a leap year */
  137. public static boolean isLeapYear(int year){
  138. return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
  139. }
  140. }

执行效果:

Java实现打印日历的功能的更多相关文章

  1. 使用java 打印日历

    package hangshu; /* * 打印从1900年到2.year年的日历 */ import java.util.Scanner; public class Calender { publi ...

  2. Atitit.java swing打印功能 api  attilax总结

    Atitit.java swing打印功能 api  attilax总结 1. 打印方式有三种:2 1.1. 一是不经过任何修改,直接调用javascript中的window.print()打印.2 ...

  3. Java实现打印功能-AWT Graphics2D

    Java实现打印功能 用java实现打印,java.awt中提供了一些打印的API,要实现打印,首先要获得打印对象,然后继承Printable实现接口方法print,以便打印机进行打印,最后用用Gra ...

  4. Oracle打印日历功能

    Oracle用SQL打印日历 1.1  打印当月日历 , D, NULL)) SUN, , D, NULL)) MON, , D, NULL)) TUE, , D, NULL)) WED, , D,  ...

  5. Java实现打印功能

    用java实现打印,java.awt中提供了一些打印的API,要实现打印,首先要获得打印对象,然后继承Printable实现接口方法print,以便打印机进行打印,最后用Graphics2D直接输出直 ...

  6. 【java】java自带的java.util.logging.Logger日志功能

    偶然翻阅到一篇文章,注意到Java自带的Logger日志功能,特地来细细的看一看,记录一下. 1.Java自带的日志功能,默认的配置 ①Logger的默认配置,位置在JRE安装目录下lib中的logg ...

  7. 常用Java API之Scanner:功能与使用方法

    Scanner 常用Java API之Scanner:功能与使用方法 Scanner类的功能:可以实现键盘输入数据到程序当中. 引用类型的一般使用步骤:(Scanner是引用类型的) 1.导包 imp ...

  8. Java如何打印日志

    以下为<正确的打日志姿势>学习笔记. 什么时候打日志 1.程序出现问题,只能通过 debug 功能来定位问题,很大程度是日志没打好.良好的系统,通过日志就能进行问题定位. 2.if-els ...

  9. Python学习实践-----打印日历

    使用python语言实现在控制台打印日历 输入年.月.日 输出对应日历,指定的日数输出为'--' 程序没有做严格的输入验证,故输入整数即可. 以下为没有优化的源码: print_calendar.py ...

随机推荐

  1. Nginx配置文档

    转https://www.cnblogs.com/hunttown/p/5759959.html

  2. 代码规范mark一下

    转自于:https://github.com/zh-google-styleguide/zh-google-styleguide/blob/master/google-python-styleguid ...

  3. 一个简单的Quartz定时任务

    package com.shuadan.quartz; import org.springframework.scheduling.annotation.Scheduled; import org.s ...

  4. OO第十二次作业

    规格设计的发展历史 随着计算机软硬件的发展,代码的复杂程度也在不短增加,随着计算机软件规模日渐庞大,结构化程序设计方法开始无法满足用户的需求,面向对象程序设计产生.面向对象程序设计是一场重大的革命,提 ...

  5. leetcode日记 HouseRobber I II

    House Robber I You are a professional robber planning to rob houses along a street. Each house has a ...

  6. Java继承2

    1.为什么使用继承 从已有的类派生出新的类,称为继承. 在不同的类中也可能会有共同的特征和动作,可以把这些共同的特征和动作放在一个类中,让其它类共享. 因此可以定义一个通用类,然后将其扩展为其它多个特 ...

  7. windows中obs源码编译的坑

    好用的版本: cmake-3.6.1-win64-x64  +  vs2015  + qt-opensource-windows-x86-msvc2015_64-5.7.0   +   obs-stu ...

  8. dubbo入门学习笔记之入门demo(基于普通maven项目)

    注:本笔记接dubbo入门学习笔记之环境准备继续记录; (四)开发服务提供者和消费者并让他们在启动时分别向注册中心注册和订阅服务 需求:订单服务中初始化订单功能需要调用用户服务的获取用户信息的接口(订 ...

  9. Linux:Gentoo系统的安装笔记(三)

    这期笔记将是gentoo安装的最后一期了,虽然已经配置内核了,但是也要完成剩下的安装步骤,这离安装完成已经不远了,继续加油!!! 如果中断了安装,请看第二期的笔记进行恢复安装,但请重新编译一次内核,否 ...

  10. 精进之路之HashMap

    HashMap本质的核心就是“数组+链表”,数组对于访问速度很快,而链表的优势在于插入速度快,HashMap集二者于一身. 提到HashMap,我们不得不提各个版本对于HashMap的不同.本文中先从 ...