Implement Calendar printing by year and month in Java [calendar based] AND Calendar calendar

Source: Internet
Author: User

Implement Calendar printing by year and month in Java [calendar based] AND Calendar calendar

This example describes how to print a calendar by year and month in Java. We will share this with you for your reference. The details are as follows:

Import java. text. parseException; import java. text. simpleDateFormat; import java. util. calendar; import java. util. date; public class CalendarBook {public static void main (String [] args) throws ParseException {CalendarBook cb = new CalendarBook (); cb. printWeekTitle (); cb. printCalendar (2018, 3);} public void printCalendar (int year, int month) throws ParseException {String monthStr; // format the month because it is converted to yyyyMM If (month <10) {monthStr = "0" + month;} else {monthStr = month + ""; // concatenate digits and strings into the String format} String yearMonthStr = year + monthStr; SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMdd"); Calendar calendarEnd = Calendar. getInstance (); Calendar calendarStart = Calendar. getInstance (); // obtain the number of days of the input month based on the year and month int monthDays = getMonthLastDay (year, month); // The date String dateStartStr = year at the beginning of the month MonthStr + "01"; // date String dateEndStr = yearMonthStr + monthDays; Date startDate = sdf. parse (dateStartStr); Date endDate = sdf. parse (dateEndStr); calendarStart. setTime (startDate); calendarEnd. setTime (endDate); // obtain the number of weeks in the input month int weeks = calendarEnd. get (Calendar. WEEK_OF_MONTH); // obtain the day of the week on the first day of the month. Here, Sunday is the first day, starting from 1, and Monday is 2 int dayOfWeek = calendarStart. get (Calendar. DAY_OF_WEEK); int day = 1; // the day of the current month Perform special processing in one week and print a single row for (int I = 1; I <= 7; I ++) {if (I> = dayOfWeek) {System. out. print ("" + day + ""); day ++;} else {System. out. print ("") ;}} System. out. println (); // start to print the date for (int week = 1; week <weeks; week ++) {for (int I = 1; I <= 7; I ++) {if (day> monthDays) {break;} if (day <10) {System. out. print ("" + day + "");} else {System. out. print (day + "");} day ++;} System. ou T. println () ;}} public int getMonthLastDay (int year, int month) {int monthDay; int [] [] day = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 31, 31, 30, 31 }}; if (year % 4 = 0 & year % 100! = 0 | year % 400 = 0) {// monthDay of a leap year = day [1] [month];} else {monthDay = day [0] [month];} return monthDay;} public void printWeekTitle () {System. out. println ("day" + "" + "one" + "" + "two" + "+" three "+" "+" four "+" "+" five" + "" + "6 ");}}

Running result (running effect, the best font size on the 5th ):

PS: Here are some online tools for date and time computing for your reference:

Online date/day calculator:
Http://tools.jb51.net/jisuanqi/date_jisuanqi

Online calendar:
Http://tools.jb51.net/bianmin/wannianli

Online calendar/Calendar Conversion Tool:
Http://tools.jb51.net/bianmin/yinli2yangli

Unix timestamp Conversion Tool:
Http://tools.jb51.net/code/unixtime

Related Article

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.