栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java实现打印日历

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java实现打印日历

本文实例为大家分享了java实现打印日历的具体代码,供大家参考,具体内容如下

效果图

代码:


//引入Scanner类,用于从控制台输入年月
import java.util.Scanner;
//程序开始
class Rili{
 //主方法,程序执行的入口
 public static void main(String[] args){
 
 inputYearAndMonth();
 }
 
 public static void inputYearAndMonth(){
 
 Scanner sc = new Scanner(System.in);
 System.out.println("请输入年");
 int year = sc.nextInt();
 System.out.println("请输入月");
 int month = sc.nextInt();
 
 printRiLi(year , month);
 
 }
 
 
 public static void printRiLi(int year,int month){
 //一周七天,定义一个计数器,打印一天加1(包括空白)如果%7等于0的话就需要换行
 int count = 0;
 System.out.println("t---下面打印的是:"+year+"年"+month+"月的日历表---");
 System.out.println();
 System.out.println("星期一t星期二t星期三t星期四t星期五t星期六t星期日");
 for(int i = 1;i <= getSpace(year,month);i++){
 
 System.out.print("t");
 count +=1;
 }
 for(int i = 1;i <= getDaysOfMonth(year ,month);i++){
 
 System.out.print(i+"t");
 count +=1;
 if(count % 7 ==0){
 System.out.println(); 
 }
 }
 
 }
 
 
 
 public static boolean isLoopYear(int year){
 
 return (year %4 ==0 && year % 100 !=0) || (year % 400 == 0);
 }
 
 
 public static int getDaysOfMonth(int year ,int month){
 
 int days = 0;
 switch(month){
 case 1:
 case 3:
 case 5:
 case 7:
 case 8:
 case 10:
 case 12:
 days = 31;
 break;
 case 4:
 case 6:
 case 9:
 case 11:
 days = 30;
 break;
 case 2:
 days = isLoopYear(year)? 29:28;
 break;
 }
 return days;
 }
 
 
 public static int getTotalDays(int year , int month){
 int daysofyear = 0;
 int daysofmonth = 0;
 for(int i = 1900;i < year;i++){
 
 daysofyear += isLoopYear(i)? 366:365;
 }
 for(int i = 1; i < month; i++){
 
 daysofmonth += getDaysOfMonth(year,i);
 }
 return daysofyear+daysofmonth;
 }
 
 
 public static int getSpace(int year,int month){
 
 int allSpace = getTotalDays(year,month) % 7;
 return allSpace;
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/129211.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号