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

  代码:

 import java.util.Scanner;
public class PrintCalendar{
public static void main(String[] args){
Scanner input=new Scanner(System.in); System.out.print("Enter full year(e.g.,2011): ");
int Year=input.nextInt(); System.out.print("Enter month as a number between 1 and 12: ");
int Month=input.nextInt(); // int year=inputYear();
//int month=inputMonth();
printMonth(Year, Month);
}
/*
public static int inputYear(){
boolean flag=true;
Scanner input=new Scanner(System.in);
while(flag){
System.out.print("Enter full year(e.g.,2011): ");
int iYear=input.nextInt();
if(iYear>=1800){
flag = false;
}
}
return iYear;
} public static int inputMonth(){
boolean flag=true;
Scanner input=new Scanner(System.in);
while(flag){
System.out.print("Enter month as a number between 1 and 12: ");
int iMonth=input.nextInt();
if(iMonth>=1 && iMonth<=12){
flag = false;
}
}
return iMonth;
}*/ 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("\t" + getMonthName(month)+"\t" +year);
System.out.println("-----------------------------------");
System.out.println("Mon Tue Wed Thu Fri Sat Sun");
}
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"; break;
} return monthName;
} public static void printMonthBody(int year, int month){ //Get start day of the week for the first date int the month
int startDay=getStartDay(year,month); //Get number of days in the month
int numberOfDaysInMonth = getNumberOfDaysInMonth(year,month); //Pad space before the first day of the month
int i=0;
for(i=1; i< startDay; i++){
System.out.print(" ");
}
for(i=1; i<=numberOfDaysInMonth;i++){
System.out.printf("%-4d", i); if((i+startDay-1)%7==0)
System.out.println();
}
System.out.println();
} /** Get the start day of month/1/year */
public static int getStartDay(int year, int month){
final int START_DAY_FOR_JAN_1_1800 = 3; //Get the total number of days from 1/1/1800 to month/1/year
int totalNumberOfDays= getTotalNumberOfDays(year,month); //Return the start day for month/1/year
return (totalNumberOfDays+START_DAY_FOR_JAN_1_1800)%7;
} /** Get the total number of days from January 1, 1800; */
public static int getTotalNumberOfDays(int year, int month){
int total = 0; //get total number of days from 1800 to 1/1/year
for(int i = 1800; i<year; i++){
if(isLeapYear(i))
total = total + 366;
else
total = total + 365;
} //add days from January to the month prior to the calendar month
for(int i = 0; i<month;i++)
total +=getNumberOfDaysInMonth(year, i); return total;
} /** Get the number of days in a month */
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==6||month==9||month==11)
return 30;
if(month==2)
return isLeapYear(year)?29:28;
return 0;
} /** Determine if it is a leap year */
public static boolean isLeapYear(int year){
return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
}
}

执行效果:

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. python format(格式化)

    自 python 2.6 开始,新增了一种格式化字符串的函数str.format(),可谓威力十足.那么,他跟之前的%型格式化字符串相比,有什么优越的存在呢?让我们来揭开它羞答答的面纱.#语法它通过{ ...

  2. Django基础-02

    django的介绍: Django 中提供了开发网站经常用到的模块,常见的代码都为你写好了,通过减少重复的代码,Django 使你能够专注于 web 应用上有 趣的关键性的东西.为了达到这个目标,Dj ...

  3. Python36 二进制文件读写问题

    在Python36中写如下代码: __author__ = '-------'#-*- coding: utf-8 -*-import struct fo = open("myfile.tx ...

  4. 阶段02JavaWeb基础day04mysql

    数据库--MySql 数据仓库.就与我们之前学过的纯文本,properties这些技术一样.用来保存数据.并提供对数据进行增删改查的操作.我们以后做项目时,项目中的数据都是保存在数据库中的.//--- ...

  5. paramiko__摘抄

    # -*- coding:utf-8 -*-# Author: Dennis Huang__Author__ = "Dennis" import paramiko # ssh = ...

  6. Oracle数据库各种名字的区别

    数据库名(DB_NAME).数据库实例名(INSTANCE_NAME).操作系统环境变量(ORACLE_SID).数据库服务名(SERVICE_NAME).数据库域名(DB_DOMAIN)以及全局数据 ...

  7. Oracle学习DaySix(PL/SQL续)

    一.游标 在 PL/SQL 程序中,对于处理多行记录的事务经常使用游标来实现.游标是一个指向上下文的句柄( handle)或指针.通过游标,PL/SQL 可以控制上下文区和处理语句时上 下文区会发生些 ...

  8. Vue的计算属性,监视属性代码理解

    1.计算属性:在computed属性对象中定义计算属性的方法,在页面中使用{{方法名}}来显示计算的结果 //计算属性 computed:{ // 计算属性值的一个方法,方法的返回值是属性值,初始化显 ...

  9. UITableView section 圆角 阴影

      在UITableView实现图片上面的效果,百度一下看了别人的实现方案有下面2种: 1.UITableView section里面嵌套UITableView然后在上面实现圆角和阴影,  弊端代码超 ...

  10. 2017-10-27模拟赛2-T1 选举(election.*)

    Description 题目描述 C国的总统选举委员会最近遇到了一些麻烦. 他们在统计各省对H先生的支持率(百分比)时,把支持率四舍五入到了整数.等他们公布结果后,该国媒体发现这些省份的支持率之和不等 ...