Core Java (6) print the calendar of a specified month

Source: Internet
Author: User

A program of the changed core Java utilizes the gregoriancalendar class.

Enter the year and month to print the calendar of the specified month.

Package COM. xujin; import Java. text. dateformatsymbols; import Java. util. calendar; import Java. util. gregoriancalendar; import Java. util. locale; import Java. util. failed; public class test {static partition CIN = new partition (system. in); public static void main (string [] ARGs) {locale. setdefault (locale. US); // set it to the US region system. out. print ("Please input the year and month (like 1999 9):"); int year = cin. nextint (); int mon Th = cin. nextint (); gregoriancalendar day = new gregoriancalendar (); // It is accurate after October 4, March 1. // set day to the first day of the month. set (calendar. year, year); Day. set (calendar. month, month-1); Day. set (calendar. day_of_month, 1); int weekday = Day. get (calendar. day_of_week); int firstdayofweek = Day. getfirstdayofweek (); int indent = 0; while (weekday! = Firstdayofweek) {indent ++; Day. add (calendar. day_of_month,-1); weekday = Day. get (calendar. day_of_week);} string [] weekdaynames = new dateformatsymbols (). getaskweekdays (); do {system. out. printf ("% 4 s", weekdaynames [weekday]); Day. add (calendar. day_of_month, 1); weekday = Day. get (calendar. day_of_week);} while (weekday! = Firstdayofweek); system. out. println (); For (INT I = 0; I <indent; I ++) system. out. print (""); Day. set (calendar. day_of_month, 1); do {int d = Day. get (calendar. day_of_month); system. out. printf ("% 4 s", d); Day. add (calendar. day_of_month, 1); weekday = Day. get (calendar. day_of_week); If (weekday = firstdayofweek) system. out. println ();} while (month-1 = Day. get (calendar. month); If (weekday! = Firstdayofweek) system. Out. println ();}}

Result:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.