So easy! Simple "java realizes printing calendar" (with code)

This article mainly introduces the implementation code of java printing calendar in detail for everyone. The sample code in the article is very detailed and has certain reference value. Interested friends can refer to it.

The specific content is as follows

Effect picture

image

Code:

/**
*需要实现的目标:根据输入的年月打印出本月的日历表
*说明:1900年1月1日刚好是星期一,所以需要计算出从1900 年到当前年月的前一个月总
*共经历了几天,然后根据每周七天,用总天数除以7取余数,此余数就是上个月所占到星
*期几,也就是每月开头有的空格数,然后打印此空格数,在打印此空格数后再依次打印本
*月的各天数。
**/
//引入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(); 
 }
 }
 
 }
 
 
 /**
 *判断年份是平年还是闰年(用于判断一年有365天或366天,并用于判断2月有28天或29天),返回值是true(29天、366天)和flase(28天、365天)
 **/
 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;
 }
 
 /**
 * 获得自1900年至当前年、月经过的总天数
 * 实现:1900年到year - 1 年的总天数
 * 当年至month - 1 的总在数
 * 两个天数之和相加
 **/
 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;
 }
 
 /**
 * 利用总天数模7取余,得到所需要打印的空格数
 **/
 public static int getSpace(int year,int month){
    
    
 
 int allSpace = getTotalDays(year,month) % 7;
 return allSpace;
 }
}

The latest high-frequency interview questions collected in 2021 (all organized into documents), there are a lot of dry goods, including mysql, netty, spring, thread, spring cloud, jvm, source code, algorithm and other detailed explanations. There are also detailed learning plans and interviews. Questions, etc., friends who need to obtain these content, please add Q Junyang: 547998459

The above is the whole content of this article, please contact me for more information, I hope it will be helpful to everyone's study, and I hope you can support me

Guess you like

Origin blog.csdn.net/p1830095583/article/details/114527854
Recommended