Create a calendar from the Calendar class by specifying the year and month

CalendarSample.java



import java.util.Calendar;

public class CalendarSample {

	public static void main(String[] args) {

		int year = 2018;
		int month = 8;

		//Calendar instance creation
		Calendar cal = Calendar.getInstance();

		//Calendar settings (month- 1)
		cal.set(year, month - 1, 1);
		//Day of the week index at the beginning of the month (Sunday 1 to Saturday 7)
		int weekIndex = cal.get(Calendar.DAY_OF_WEEK);
		//Seeking the end of the month
		int monthEndDay = cal.getActualMaximum(Calendar.DATE);

		//Calendar creation
		System.out.printf("   << %4d years%2d month>>\n", year, month);
		System.out.println("Sunday Monday Tuesday Wednesday Thursday Friday Saturday");

		//Day of the week offset space
		for (int i = 1; i < weekIndex; i++) {
			System.out.print("   ");
		}

		//Output from January to the end of the month
		for (int day = 1; day <= monthEndDay; day++) {
			System.out.printf("%3d", day);

			//Line break on saturday
			if ((day + weekIndex - 1) % 7 == 0) {
				System.out.println();
			}
		}
	}
}

Recommended Posts

Create a calendar from the Calendar class by specifying the year and month
How to create a form to select a date from the calendar
Create an app by specifying the Rails version
Call a method of the parent class by explicitly specifying the name in Ruby
3. Create a database to access from the web module
What is the difference between a class and a struct? ?? ??
21 Load the script from a file and execute it
Getting a little stuck on the last day of the month using Java's Calendar class
Find the address class and address type from the IP address with Java
[Swift5] How to create a .gitignore file and the code that should be written by default
Create a private repository in Amazon ECR and push/pull the image
[Ruby] Relationship between parent class and child class. The relationship between a class and an instance.
Create a static file that expands variables using the ERB class
Get the current date and time by specifying the time zone in Thymeleaf
Check the contents by docker pull from ECR and start the container
Easy way to create a mapping class when using the API
Install Rails in the development environment and create a new application
[Android] Create a calendar using GridView
Get TypeElement and TypeMirror from Class
A murmur about the utility class
Find the address class and address type from the IP address with Java [No. 2 decoction]
Create a Tokyo subway map from the CSV file of station data.jp
The value is taken out from the double hash and output by the getter.
[Java] Create a jar file with both compressed and uncompressed with the jar command
[Rails] How to create a table, add a column, and change the column type
I made a class that can use JUMAN and KNP from Java
Display Japanese calendar and days of the week using java8 standard class