3
5

More than 3 years have passed since last update.

Java カレンダー 作り方 まとめ

Last updated at Posted at 2020-04-15

はじめに

Javaでカレンダーを作成してみました。
今年1年分のカレンダーが出力される仕様になっています。

プログラミング学習を初めて2ヶ月が過ぎ、このままだと心が折れそうなので細々と投稿を始めることにしました。
暖かい目で見てください。
今にも挫折しそうです、お願いします。

目次

1.やりたかったコト
2.カレンダー作成コード
3.出力結果
4.補足

やりたかったコト

・1年分のカレンダーの作成
・うるう年を見分けて2月の日数を調整
・文字フォーマットを3文字で統一(見やすくするため)
・toStringメソッドのオーバーライド

カレンダー作成コード

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


    /*************************
     * フィールド
     *************************/

    //カレンダーマトリックス用の配列
    public int[][] calendarMatrix;

    //カレンダー月の年月
    private Year year;

    //月初用LocalDate
    LocalDate ld;

    /***********************
     * コンストラクタ
     ***********************/

    //今年をセット
    public Calendar_test() {
        this.setYear();
    }


    /***********************
     * メソッド
     ***********************/

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

    //今年のyearを返す
    private Year getYear() {
        return this.year;
    }

    //指定月の月初setter
    private void setLd(int month) {
        this.ld = LocalDate.of(this.getYear().getValue(), month, 1);
    }

    //指定月の月初getter
    private LocalDate getLd() {
        return this.ld;
    }


    //指定月の長さを取り出す
    private int getMonthLength() {
        Month thisMonth = Month.from(getLd());
        return thisMonth.length(this.ld.isLeapYear());
    }

    //月初の曜日を取得
    private int getFirstDay() {
        return getLd().getDayOfWeek().getValue() - 1;       //月:0 火:1 水:2 ・・・
    }

    //それぞれの月の配列に日数を入れ込む
    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
        StringBuilder sb = new StringBuilder();

        //3桁表示のためのフォーマット指定
        final String FORMAT = "%3d";

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

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

            calcFields();
            sb.append(getYear() + "年" + n + "月");
            sb.append("\r\n");

            //入れ込むために回す/縦6回
            for (int i = 0; i < calendarMatrix.length; i++) {

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

                    //初期値のままのものは空文字埋め
                    if (date == 0) {
                        sb.append("   ");

                    //数字はフォーマット指定して入れ込み
                    } else {
                        sb.append(String.format(FORMAT,date));
                    }
                }

                //一週間で改行
                sb.append("\r\n");
            }

            //一月で改行
            sb.append("\r\n");

        }

            return sb.toString();
    }





    public static void main(String[] args) {

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

}

出力結果

2020年1月
        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      


2020年2月
                 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   


2020年3月
                    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               

2020年4月
        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         


2020年5月
              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


2020年6月
  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               


2020年7月
        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      


2020年8月
                 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                  

2020年9月
     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            


2020年10月
           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   


2020年11月
                    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                  

2020年12月
     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         

補足

私はいわゆる情弱の部類に入る人間だと思います。
twitterのアカウントも持っていません。

ただ、プログラミングの勉強を始めてみてコンピューターがいかに凄いものか実感しています。
まだ勉強を初めて2ヶ月ですが、これからもっと知識を深めて、ここで共有していきたいと思っています。
お手柔らかにお願いします。

3
5
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5