Java uses calendar class to implement dynamic calendar

Time:2021-11-24

This example shares the specific code of Java using calendar class to realize dynamic calendar for your reference. The specific contents are as follows

Title:

Use the calendar class to implement a dynamic calendar. It is required to display the calendar layout of the current month by entering a year and month, and wrap Saturday and Sunday with “[]”. In addition, if it is displayed as the calendar of the current month, it is required to mark the date of the current day with “*”.

Step analysis:

1. Print calendar

1.1 total days of the month

1.2 the first day of the month is the day of the week (Note: the month starts from 0 and the week starts from Sunday (1))

2. Package with [] on weekends

3. Add * before the current date

Code implementation:

package com.softeem.j2016.example;
 
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
 
/**
 *1. Print calendar
 *1.1 total days of the month
 *1.2 what day of the week is the first day of the month
 *2. Package with [] on weekends
 *3. Add "*" to the current date
 * @author admin
 * 2021/7/23
 */
public class CalendarDemo {
    //Get a date instance
    static  Calendar c = Calendar.getInstance();
 
    public static void show(int year,int month){
        //Get the date of the day
        Date today = new Date();
        //Obtain the date of the system time
        int y = today.getYear() +1900;
        int m = today.getMonth() +1;
        int d = today.getDate() ;
 
        c.set(Calendar.YEAR,year);
        c.set(Calendar.MONTH,month-1);
        //Set the date of the current calendar to the first day of the month (7.1)
        c.set(Calendar.DAY_OF_MONTH,1);
        //Get the maximum possible value of the calendar represented by the current field (total days of the month)
        int days = c.getActualMaximum(Calendar.DAY_OF_MONTH);
        //Get the day of the week on the first day of the month (set as the first day of the month before 7.1), Sunday = 1;
        int week = c.get(Calendar.DAY_OF_WEEK)-1;
        //If the day is Sunday, set to 7
        week = week == 0?7 :week;
        //Print spaces
        System. Out. Printf ("% d month has% d days in total, week% d on the 1st \ n", month, days, week);
        System. Out. Printf ("=============================================== [% D] year, month]";
        System.out.println ("one \ t two \ t three \ t four \ t five \ t six days");
        System.out.println("================================");
        //Counter
        int count  = 0;
        //(output spaces) if the day is Thursday, it means there are three spaces and it needs to be cycled three times, then week-1
        for (int i = 0; i < week - 1; i++) {
            System.out.print("\t");
            count++;
        }
        for (int i = 1; i <=days; i++) {
            count++;
            //Judge whether it is a weekend according to the value of the date. If so, package with "[]"
            String date = (count % 7 ==6 || count % 7 == 0)?"["+i+"]\t":i+"\t";
            //Mark with "*" before the current date
            date = (y == year && m == month && d == i)?"*" + date:date;
            //Output date
            System.out.print(date);
            //Line breaks when seven digits are reached
            if (count%7==0){
                System.out.println();
            }
        }
    }
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println ("please enter the year");
        int year = sc.nextInt();
        System.out.println ("please enter the month");
        int month = sc.nextInt();
        show(year,month);
    }
}

Effect screenshot:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.