Java print calendar

Today Tucao about the use of Java's Calendar class, anyway, I get a long time.

First, encountered a problem, month and year, need to print this month's calendar, the Internet has a lot of code, but I feel with a few not very reliable.

Then after some exploration, finalized the following program:

import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.Scanner;

public class MyCal {

    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            System.out.println("please input the year:");
            int year = sc.nextInt();
            System.out.println("please input the month:");
            int month = sc.nextInt();
            List<String> MONTH_LIST = Arrays.asList(
                    "Jan", "Feb", "Mar", "Apr", "May", "Jun",
                    "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"
            );
            System.out.printf("         %s, %4d\n", MONTH_LIST.get(month - 1), year);
            System.out.println("-----------------------------");
            System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
            Calendar c = Calendar.getInstance();
            c.set(year, month-1, 1);
            int totalDay = c.getActualMaximum(Calendar.DATE);
            for (int i = 1; i < c.get(Calendar.DAY_OF_WEEK); i++)
                System.out.print("    ");
            for (int i = 1; i <= totalDay; i++) {
                c.set(year, month-1, i);
                System.out.printf("%4d", i);
                if (c.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY)
                    System.out.println("");
            }
        }
    }

}

set (year, month, date) problems

c.set(year, month-1, 1);

Online code really do not fly, not a few of the API to explain.

I finally look through the official API only to find that the second argument of the month is zero, and the third argument is the date from the beginning, really fucked me one.

getActualMaximum usage

int totalDay = c.getActualMaximum(Calendar.DATE);

getActualMaximumThe method can obtain directly Calendarthe maximum value in a field, getActualMaximum(Calendar.DATE)the meaning of this month is to get the maximum date.

Also you can use this:
getActualMaximum(Calendar.MONTH)get month maximum
getActualMaximum(Calendar.HOUR)to obtain the maximum number of hours

Whether plus get

To talk about c.get(Calendar.DAY_OF_WEEK)and Calendar.DAY_OF_WEEKdifferent, plus without geta big difference, I did not pay attention to the beginning, Calendarthe internal implementation and get the return value is biased, like 1.

If you pay attention, you may like me, the date format tune tune for a long time.

Calendar.DAY_OF_WEEK

Calendar.DAY_OF_WEEKThis function online blog many did not make it clear, in fact, its function is to get the current date is the day of the week.
But like the above said the same, if not outside a set get, and you actually get Calendar.SATURDAYis biased, for example, Calendar.DAY_OF_WEEK==Calendar.SATURDAYand c.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAYis not the same.

Program logic

First printing space outside the body of the loop, analyze, it should be filled in get(Calendar.DAY_OF_WEEK)a Tab(four spaces).

Inside the loop logic is very simple, each time re-set about the date, from No. 1 is set to begin this month on the last day, if that day happens to be a Saturday, so the next time you need to change the line output.

test

The end result might look like this:

please input the year:
2019
please input the month:
11
         Nov, 2019
-----------------------------
 Sun Mon Tue Wed Thu Fri Sat
                       1   2
   3   4   5   6   7   8   9
  10  11  12  13  14  15  16
  17  18  19  20  21  22  23
  24  25  26  27  28  29  30

Guess you like

Origin www.cnblogs.com/nonlinearthink/p/11922368.html