This article describes the Java implementation of the function of printing calendars by year and month. Share it for your reference, 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 to convert it to yyyyMMdd format if (month < 10) { monthStr = "0" + month; } else { monthStr = month + ""; // Convert numbers and strings into string format} String yearMonthStr = year + monthStr; SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); Calendar calendarEnd = Calendar.getInstance(); Calendar calendarStart = Calendar.getInstance(); // get how many days the month has been entered based on the year and month int monthDays = getMonthLastDay(year, month); // the date string at the beginning of the month String dateStartStr = yearMonthStr + "01"; // the date string at the end of the month String dateEndStr = yearMonthStr + monthDays; Date startDate = sdf.parse(dateStartStr); Date endDate = sdf.parse(dateEndStr); calendarStart.setTime(startDate); calendarEnd.setTime(endDate); // Get how many weeks the month has int weeks = calendarEnd.get(Calendar.WEEK_OF_MONTH); // Get the first day of the month is the day of the week, here Sunday is the first day, starting from 1, and Monday is 2 int dayOfWeek = calendarStart.get(Calendar.DAY_OF_WEEK); int day = 1; // Special processing is done in the first week of the month, and a line is printed separately for (int i = 1; i <= 7; i++) { if (i >= dayOfWeek) { System.out.print(" + day + " "); day++; } else { System.out.print(" "); } } System.out.println(); // Start printing the date starting from the second week for (int week = 1; week < weeks; week++) { for (int i = 1; i <= 7; i++) { if (day > monthsDays) { break; } if (day < 10) { System.out.print(" " + day + " "); } else { System.out.print(day + " "); } day++; } System.out.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, 30, 31, 30, 31 }, { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } }; if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { // Leap year monthDay = day[1][month]; } else { monthDay = day[0][month]; } return monthDay; } public void printWeekTitle() { System.out.println("Day" + " " + "One" + " " + "Two" + " " + "Three" + " " + "Four" + " " + " Five" + " " + " Six"); }}Screenshot of the running result (running effect, font size 5 is the best):
PS: Here are a few online tools for your reference:
Online Date/Day Calculator:
http://tools.VeVB.COM/jisuanqi/date_jisuanqi
Online Perpetual Calendar:
http://tools.VeVB.COM/bianmin/wannianli
Online Lunar/Gregorian calendar conversion tool:
http://tools.VeVB.COM/bianmin/yinli2yangli
Unix timestamp conversion tool:
http://tools.VeVB.COM/code/unixtime
For more information about Java related content, please check out the topics of this site: "Summary of Java Date and Time Operation Skills", "Tutorial on Java Data Structure and Algorithm", "Summary of Java Operation DOM Node Skills" and "Summary of Java Cache Operation Skills"
I hope this article will be helpful to everyone's Java programming.