Java实现打印日历的功能

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

  代码:

  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 }

执行效果:

原文地址:https://www.cnblogs.com/shuitailiang/p/10639982.html

时间: 2024-08-06 02:32:07

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

【Java自学】 打印日历信息

1 package codeTask_FangFa; 2 // 5.34 使用zeller公式,打印某年某月的日历信息. 3 import java.util.Scanner; 4 public class PrintRiLi { 5 public static void main(String[] args){ 6 Scanner input = new Scanner(System.in); 7 System.out.println("请输入需要打印日历的年份:"); 8 int

java打印日历方法

package cn.baokx.test; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class Test { public static void main(String ... args) { String dat

java 实现打印当前月份的日历

实现当前日历的打印,当前日期用*来表示. 关键得出这个月的第一天是星期几. 基姆拉尔森计算公式 W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7 在公式中d表示日期中的日数+1,m表示月份数,y表示年数. 注意1:在公式中有个与其他公式不同的地方: 把一月和二月看成是上一年的十三月和十四月, 例:如果是2004-1-10则换算成:2003-13-10来代入公式计算. 注意2:在大多数天主教国家的日历中,在1752年没有9.3-9.13,在这一年的日历中9月

Java实现打印功能-AWT Graphics2D

Java实现打印功能 用java实现打印,java.awt中提供了一些打印的API,要实现打印,首先要获得打印对象,然后继承Printable实现接口方法print,以便打印机进行打印,最后用用Graphics2D直接输出直接输出.下面代码实现了简单的打印功能: import java.awt.BasicStroke; import java.awt.Color; import java.awt.Component; import java.awt.Font; import java.awt.G

使用java 打印日历

package hangshu; /* * 打印从1900年到2.year年的日历 */ import java.util.Scanner; public class Calender { public static int year = Integer.MIN_VALUE; public static int month = Integer.MIN_VALUE; private static int[] m = {31,28,31,30,31,30,31,31,30,31,30,31}; pu

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

偶然翻阅到一篇文章,注意到Java自带的Logger日志功能,特地来细细的看一看,记录一下. 1.Java自带的日志功能,默认的配置 ①Logger的默认配置,位置在JRE安装目录下lib中的logging.properties中 ②logging.properties日志文件内容如下: ############################################################ # Default Logging Configuration File # # You

java初学打印星星以及九九乘法表

Java中打印星星,就是为了让初学者熟悉和掌握循环的使用方法: 给定一个图形如: * *** ***** ******* 第一种方式:由循环输出各个部分,再结合起来 for(int i=1;i<=4;i++){< p=""> for(int j=1;j<=7-i;j++){< p=""> system.out.print(" "); } for(int j=1;j<=2*i-1;j++){< p=&

lua中打印所以类型功能实现table嵌套table

lua中打印所以类型功能实现 本人测试 number.string.bool.nil.table嵌套table.userdata没问题 共享一下有什么问题请拍砖 代码如下 cclog = function( ... ) local tv = "\n" local xn = 0 local function tvlinet(xn) -- body for i=1,xn do tv = tv.."\t" end end local function printTab(i

C#实现打印与打印预览功能

原文:C#实现打印与打印预览功能 在windows应用程序中文档的打印是一项非常重要的功能,在以前一直是一个非常复杂的工作,Microsoft .Net Framework的打印功能都以组件的方式提供,为程序员提供了很大的方便,但是这几个组件的使用还是很复杂的,有必要解释一下. 打印操作通常包括以下四个功能: 1 打印设置 设置打印机的一些参数,比如更改打印机驱动程序等; 2 页面设置 设置页面大小纸张类型等 3 打印预览 类似于word中的打印预览 4 打印 实现打印功能的核心是PrintDo