How to make a Java calendar Summary

Introduction

I created a calendar in Java. It is designed to output a calendar for one year this year.

It's been two months since I started learning programming, and I'm going to break my heart, so I decided to start posting in detail. Look with warm eyes. I'm about to be frustrated, please.

table of contents

  1. What I wanted to do
  2. Calendar creation code
  3. Output result
  4. Supplement

What I wanted to do

・ Creating a calendar for one year ・ Adjust the number of days in February by distinguishing leap years -Unify the character format with 3 characters (to make it easier to see) -Override of toString method

Calendar creation code

import java.time.LocalDate;
import java.time.Month;
import java.time.Year;
public class Calendar_test {


	/*************************
	 *field
	 *************************/

	//Array for calendar matrix
	public int[][] calendarMatrix;

	//Calendar month year month
	private Year year;

	//Local Date for the beginning of the month
	LocalDate ld;

	/***********************
	 *constructor
	 ***********************/

	//Set this year
	public Calendar_test() {
		this.setYear();
	}


	/***********************
	 *Method
	 ***********************/

	//year setter
	private void setYear() {
		this.year = Year.now();
	}

	//Return this year's year
	private Year getYear() {
		return this.year;
	}

	//First setter of the specified month
	private void setLd(int month) {
		this.ld = LocalDate.of(this.getYear().getValue(), month, 1);
	}

	//Getter at the beginning of the specified month
	private LocalDate getLd() {
		return this.ld;
	}


	//Extract the length of the specified month
	private int getMonthLength() {
		Month thisMonth = Month.from(getLd());
		return thisMonth.length(this.ld.isLeapYear());
	}

	//Get the first day of the month
	private int getFirstDay() {
		return getLd().getDayOfWeek().getValue() - 1;		//Month:0 fire:1 water:2 ...
	}

	//Put the number of days in the array of each month
	public void calcFields() {

		int row = 0;
		int column = getFirstDay();

		for(int date = 1; date <= getMonthLength(); date++) {
			this.calendarMatrix[row][column] = date;
			if(column == 6) {
				row++;
				column = 0;
			} else {
				column++;
			}
		}
	}


	@Override
	public String toString() {

		//Builder for embedding
		StringBuilder sb = new StringBuilder();

		//Format specification for 3-digit display
		final String FORMAT = "%3d";

		for (int n = 1; n <= 12; n++) {

			this.calendarMatrix = new int[6][7];
			this.setLd(n);

			calcFields();
			sb.append(getYear() + "Year" + n + "Month");
			sb.append("\r\n");

			//Turn to insert/6 times vertically
			for (int i = 0; i < calendarMatrix.length; i++) {

				//Horizontal 7 times
				for (int j = 0; j < calendarMatrix[i].length; j++) {
					int date = calendarMatrix[i][j];

					//Fill in the empty string with the default value
					if (date == 0) {
						sb.append("   ");

					//Enter the numbers by specifying the format
					} else {
						sb.append(String.format(FORMAT,date));
					}
				}

				//Line breaks in a week
				sb.append("\r\n");
			}

			//Line breaks in January
			sb.append("\r\n");

		}

			return sb.toString();
	}





	public static void main(String[] args) {

		System.out.println(new Calendar_test());
	}

}

Output result

January 2020
        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 31      
                     

February 2020
                 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   
                     

March 2020
                    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 31               

April 2020
        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         
                     

May 2020
              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 31
                     

June 2020
  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               
                     

July 2020
        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 31      
                     

August 2020
                 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
 31                  

September 2020
     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            
                     

October 2020
           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 31   
                     

November 2020
                    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                  

December 2020
     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 31         
                     

Supplement

I think I'm a person in the so-called weakness category. I don't have a twitter account either.

However, when I started studying programming, I realized how amazing computers are. I have only been studying for two months, but I would like to deepen my knowledge and share it here. Please be kind to me.

Recommended Posts

How to make a Java calendar Summary
How to make a Java container
How to make a Java array
How to make a Discord bot (Java)
Java --How to make JTable
How to make a JDBC driver
[Java] How to create a folder
How to make a splash screen
How to make a Jenkins plugin
How to make a Maven project
[java] Summary of how to handle char
[Introduction to Java] How to write a Java program
[Java] [Maven3] Summary of how to use Maven3
How to print a Java Word document
[Java] How to use the Calendar class
How to make a groundbreaking diamond using Java for statement wwww
How to make shaded-jar
[java] Summary of how to handle character strings
How to display a web page in Java
[Java] Summary of how to abbreviate lambda expressions
I did Java to make (a == 1 && a == 2 && a == 3) always true
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
How to convert a solidity contract to a Java contract class
How to make a lightweight JRE for distribution
How to make a follow function in Rails
[Java] How to make multiple for loops single
[Java] How to use Calendar class and Date class
How to make an app with a plugin mechanism [C # and Java]
[Java] How to use Map
How to make a factory with a model with polymorphic association
How to create a Java environment in just 3 seconds
How to lower java version
[Java] How to use Map
How to uninstall Java 8 (Mac)
How to jump from Eclipse Java to a SQL file
How to use java Optional
java: How to write a generic type list [Note]
[Java] How to play rock-paper-scissors (equivalent to paiza rank A)
How to make Java unit tests (JUnit & Mockito & PowerMock)
How to minimize Java images
How to write java comments
How to leave a comment
How to make JavaScript work on a specific page
How to use java class
[Java] How to use Optional ②
How to create a data URI (base64) in Java
[Java] How to use removeAll ()
[Java] How to display Wingdings
[Java] How to get a request by HTTP communication
I tried to make a login function in Java
[Java] How to use string.format
How to use Java Map
[Java] Make it a constant
[Java] How to execute tasks on a regular basis
How to set Java constants
[Java] How to cut out a character string character by character
Summary of Java communication API (1) How to use Socket
[Java] How to erase a specific character from a character string
Summary of Java communication API (3) How to use SocketChannel
How to convert A to a and a to A using AND and OR in Java
How to convert a file to a byte array in Java