Creating a calendar by using the basic syntax of Java

109 阅读2分钟

Introduction

I will write a small function related to the calendar using some basic Java syntax.

calendar.png


英语和Java初学者,现在用文章记录我的学习/练习过程,如果有人看到我的任何错误或者有建议的地方可以帮我指出来么🥺

Details & Steps

Details

  1. From January 01, 1900(Monday)
  2. When I input a year and a month, this program could output this monthly calendar

Steps

  1. Importing the package of inputs.
import java.util.Scanner;
  1. Calculating the total days, from 1900 to the previous years of the year of input (because the year of input isn't a complete year).
Scanner scanner = new Scanner(System.in);

System.out.println("Please input the year:");

int year = scanner.nextInt();

System.out.println("Please input the month:");

int month = scanner.nextInt();

final int initialYear = 1900;

int totalDays = 0;

// Judging this year and month are leap year and leap month or not

boolean isLeapYear = (year / 4 == 0 && year / 100 != 0) || (year / 400 == 0);

for (int i = initialYear; i < year; i++) {

    // Judging this year and month are leap year and leap month or not

    totalDays += ((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0)) ? 366 : 365;

}
  1. Calculating the total days, from the beginning of the year of input to the month of input.
for (int i = 1; i < month; i++) {

    if (i == 2) {

        totalDays += isLeapYear ? 29 : 28;

    } else if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) {

        totalDays += 31;

    } else {

        totalDays += 30;

    }
}

  1. Calculating the first day of the month of input (what day is it today: total days % 7 + 1).
int dayOfWeek = totalDays % 7 + 1;
  1. Calculating the number of days of the month of input.
int monthDays = 0;
if (month == 2) {

    monthDays = isLeapYear ? 29 : 28;

} else if (month == 4 || month == 6 || month == 9 || month == 11) {

    monthDays = 30;

} else {

    monthDays = 31;

}
  1. Outputting the header of table.
// I use ‘/t’ for adding spacing
System.out.println("Mon\t Tue\t Wed\t Thu\t Fri\t Sat\t Sun\t");
  1. Designing spacing.
// Matching date and week, aligning them

for (int i = 1; i < dayOfWeek; i++) {

    System.out.print("\t");

}


for (int i = 1; i <= monthDays; i++) {

    System.out.print(i + "\t");

    // At the end of week, new line

    if (((dayOfWeek - 1) + i) % 7 == 0) {

        System.out.println();

    }

}

Total

package com.practice;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("Please input the year:");

        int year = scanner.nextInt();

        System.out.println("Please input the month:");

        int month = scanner.nextInt();


        final int initialYear = 1900;

        int totalDays = 0;

        int monthDays = 0;


        boolean isLeapYear = (year / 4 == 0 && year / 100 != 0) || (year / 400 == 0);


        for (int i = initialYear; i < year; i++) {

            // Judging this year and month are leap year and leap month or not

            totalDays += ((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0)) ? 366 : 365;

        }




        for (int i = 1; i < month; i++) {

            if (i == 2) {

                totalDays += isLeapYear ? 29 : 28;

            } else if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) {

                totalDays += 31;

            } else {

                totalDays += 30;

            }

        }

        int dayOfWeek = totalDays % 7 + 1;

        if (month == 2) {

            monthDays = isLeapYear ? 29 : 28;

        } else if (month == 4 || month == 6 || month == 9 || month == 11) {

            monthDays = 30;

        } else {

            monthDays = 31;

        }

        // I use ‘/t’ for adding spacing

        System.out.println("Mon\t Tue\t Wed\t Thu\t Fri\t Sat\t Sun\t");



        // Matching date and week, aligning them

        for (int i = 1; i < dayOfWeek; i++) {

            System.out.print("\t");

        }



        for (int i = 1; i <= monthDays; i++) {

            System.out.print(i + "\t");

            // At the end of week, new line

            if (((dayOfWeek - 1) + i) % 7 == 0) {

                System.out.println();

            }

        }

    }

}