Java实现打印日历的功能
编写一个程序,显示给定年月的日历。程序提示用户输入年份和月份,然后显示该月的整个日历。
代码:
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实现打印日历的功能的更多相关文章
- 使用java 打印日历
package hangshu; /* * 打印从1900年到2.year年的日历 */ import java.util.Scanner; public class Calender { publi ...
- Atitit.java swing打印功能 api attilax总结
Atitit.java swing打印功能 api attilax总结 1. 打印方式有三种:2 1.1. 一是不经过任何修改,直接调用javascript中的window.print()打印.2 ...
- Java实现打印功能-AWT Graphics2D
Java实现打印功能 用java实现打印,java.awt中提供了一些打印的API,要实现打印,首先要获得打印对象,然后继承Printable实现接口方法print,以便打印机进行打印,最后用用Gra ...
- Oracle打印日历功能
Oracle用SQL打印日历 1.1 打印当月日历 , D, NULL)) SUN, , D, NULL)) MON, , D, NULL)) TUE, , D, NULL)) WED, , D, ...
- Java实现打印功能
用java实现打印,java.awt中提供了一些打印的API,要实现打印,首先要获得打印对象,然后继承Printable实现接口方法print,以便打印机进行打印,最后用Graphics2D直接输出直 ...
- 【java】java自带的java.util.logging.Logger日志功能
偶然翻阅到一篇文章,注意到Java自带的Logger日志功能,特地来细细的看一看,记录一下. 1.Java自带的日志功能,默认的配置 ①Logger的默认配置,位置在JRE安装目录下lib中的logg ...
- 常用Java API之Scanner:功能与使用方法
Scanner 常用Java API之Scanner:功能与使用方法 Scanner类的功能:可以实现键盘输入数据到程序当中. 引用类型的一般使用步骤:(Scanner是引用类型的) 1.导包 imp ...
- Java如何打印日志
以下为<正确的打日志姿势>学习笔记. 什么时候打日志 1.程序出现问题,只能通过 debug 功能来定位问题,很大程度是日志没打好.良好的系统,通过日志就能进行问题定位. 2.if-els ...
- Python学习实践-----打印日历
使用python语言实现在控制台打印日历 输入年.月.日 输出对应日历,指定的日数输出为'--' 程序没有做严格的输入验证,故输入整数即可. 以下为没有优化的源码: print_calendar.py ...
随机推荐
- MySQL拓展操作
MySQL除了基本的增删该查功能,还有以下拓展功能: create table t1( id int ...., num int, xx int, unique 唯一索引名称 (列名,列名), con ...
- VIPKID 内推---开发工程师
VIPKID 目前是K12教育领域最大的一家公司,目前已发展到6w名北美外教,服务于中国50w的小朋友,每天数十万节视频课程在线上进行. 有兴趣加入VIPKID的程序员小伙伴,请发简历到 gloryz ...
- c# 关于Threading.ApartmentState
今天在做一个需求 就是 客户端的注销重新登录的操作,想必大家很清楚这个逻辑应该怎么去做, 在主线程里面去调用这个注销的方法 然后关闭当前应用域,重新开一个线程 让应用域在上面执行. STA(singl ...
- Java——重写
重写面向对象编程的三大特征之一 1.子类重写了父类的方法,则使用子类创建的对象调用该方法时,调用的是重写后的方法,即子类中的方法 2.子类重写父类方法需满足以下条件: (1)方法名和参数列表: 子类重 ...
- 魅族pro 7详细打开Usb调试模式的方法
经常我们使用安卓手机链上Pc的时候,或者使用的有些APP比如我们公司营销小组经常使用的APP引号精灵,之前老版本就需要开启usb开发者调试模式下使用,现经常新版本不需要了,如果手机没有开启usb开发者 ...
- vim学习纪要
普通模式 根据屏幕行上下移动. gj gk g0 g^ g$ 移动到行首第一个非空字符 ^ 反向移动到上一单词的词尾 ge 插入模式 粘贴寄存器中内容 <C-r> 可视模式 移动光标的起始 ...
- 制作voc2007数据格式的数据集
最近按照博主分享的流程操作,将自己遇到的难题记录下来,附上原博文链接:https://blog.csdn.net/jx232515/article/details/78680724 使用SSD训练自己 ...
- 三星S8相机黑画面解决
最近手机刷机卸载系统软件后有遇到相机黑化面不能工作的情况,同时闪光灯也无法开启,人脸识别也无法使用.具体情况如下图. 后来接上电脑打开logcat,发现相机相关的错误,说libquramresize. ...
- c#计算器
代码没有大的问题,但是起初点击控件无反应,原因是事件代码要自己敲,不能直接粘贴. using System;using System.Collections.Generic;using System. ...
- JavaScript几种常见的继承方法
1.call() 方法 call() 方法是与经典的对象冒充方法最相似的方法.它的第一个参数用作 this 的对象.其他参数都直接传递给函数自身 function Huster(name,idNum, ...