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.
・ 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
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());
	}
}
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         
                     
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