📌  相关文章
📜  Java程序,无需 calendar.get()函数即可生成任何年份的日历

📅  最后修改于: 2022-05-13 01:54:49.803000             🧑  作者: Mango

Java程序,无需 calendar.get()函数即可生成任何年份的日历

产生任何所需的年份和月份的日历Java程序让我们先通过一个型插画在逻辑和程序的一部分降落前。

程序:



步骤1:将年和月作为用户的整数输入

第 2 步:按如下顺序创建 2 个数组,一个用于存储天数,另一个用于存储月份。

String day[]   = { "SUN","MON","TUE","WED","THU","FRI","SAT" } ;  
String month[] = { "JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER" } ; 

步骤3:初始化一个计数器变量和三个变量,分别代表日、月、年为1,以及一个单独的数组,用于存储可以找到月份的不同天数组合。例如。 31、30、29

int ar[] = { 31,29,31,30,31,30,31,31,30,31,30,31 } ;

第 4 步:检查闰年条件并重新初始化上述数组的值。

if(y%4==0&&y%100!=0||y%100==0)
ar[1]=29;                       // if the year is a leap year then store 29 for the month of february
else  
ar[1]=28;                       // else 28 

第 5 步:当月数达到 12 时增加年数,当日数达到大于相应索引数组中的值时增加月数

第六步:打印结果。

执行:

例子

Java
// Java Program to Generate Desired Calendar
// Without calendar.get() function or
// Inputting the Year and the Month
  
// Importing required classes
import java.io.*;
import java.util.Scanner;
  
// Main class
public class GFG {
  
    // Main driver method
    public static void main(String a[])
    {
  
        // Reading input by creating object of Scanner class
        Scanner sc = new Scanner(System.in);
  
        // Display message only
        System.out.print("Enter the year : ");
  
        // Reading integer input value
        int yy = sc.nextInt();
  
        // Display message only
        System.out.print("Enter month : ");
  
        // Reading integer input value
        int mm = sc.nextInt();
  
        int d = 1;
        int m = 1;
        int y = 1;
        int dy = 1;
  
        // Storing data and months as input
        String day[] = { "SUN", "MON", "TUE", "WED",
                         "THU", "FRI", "SAT" };
        String month[]
            = { "JANUARY", "FEBRUARY", "MARCH",
                "APRIL",   "MAY",      "JUNE",
                "JULY",    "AUGUST",   "SEPTEMBER",
                "OCTOBER", "NOVEMBER", "DECEMBER" };
  
        // Custom array as input
        int ar[] = { 31, 29, 31, 30, 31, 30,
                     31, 31, 30, 31, 30, 31 };
  
        // Till condition holds true
        while (true) {
  
            if (d == 1 && m == mm && y == yy) {
                break;
            }
  
            if (y % 4 == 0 && y % 100 != 0
                || y % 100 == 0) {
                ar[1] = 29;
            }
  
            else {
                ar[1] = 28;
            }
            dy++;
            d++;
  
            if (d > ar[m - 1]) {
                m++;
                d = 1;
            }
  
            if (m > 12) {
                m = 1;
                y++;
            }
  
            if (dy == 7) {
                dy = 0;
            }
        }
  
        int c = dy;
  
        if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) {
            ar[1] = 29;
        }
        else {
            ar[1] = 28;
        }
  
        // Print the desired month of input year
        System.out.println("MONTH:" + month[mm - 1]);
  
        for (int k = 0; k < 7; k++) {
            System.out.print("   " + day[k]);
        }
  
        System.out.println();
  
        for (int j = 1; j <= (ar[mm - 1] + dy); j++) {
            if (j > 6) {
                dy = dy % 6;
            }
        }
  
        int spaces = dy;
        if (spaces < 0)
            spaces = 6;
  
        // Printing the calendar
        for (int i = 0; i < spaces; i++)
            System.out.print("      ");
        for (int i = 1; i <= ar[mm - 1]; i++) {
            System.out.printf(" %4d ", i);
  
            if (((i + spaces) % 7 == 0)
                || (i == ar[mm - 1]))
                System.out.println();
        }
    }
}


输出: