diff --git a/app/src/main/java/com/example/citrusclient/views/CalendarFragment.java b/app/src/main/java/com/example/citrusclient/views/CalendarFragment.java index 3dba52e..8e0360a 100644 --- a/app/src/main/java/com/example/citrusclient/views/CalendarFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/CalendarFragment.java @@ -1,5 +1,6 @@ package com.example.citrusclient.views; +import android.app.Activity; import android.graphics.Color; import android.os.Bundle; @@ -60,9 +61,17 @@ private TableLayout tableLayout; private TableRow[] tableRows = new TableRow[6]; + private TextView curMonth; public CalendarFragment() { - // Required empty public constructor + Calendar calendar = Calendar.getInstance(); + year = calendar.get(Calendar.YEAR); //現在の年 + month = calendar.get(Calendar.MONTH) + 1; //現在の月 + } + + public CalendarFragment(int year, int month) { + this.year = year; + this.month = month; } /** @@ -236,9 +245,9 @@ booksViewModel.loadBooks(accountId, token); tableLayout = view.findViewById(R.id.calendarlayout); - Calendar calendar = Calendar.getInstance(); - year = calendar.get(Calendar.YEAR); //現在の年 - month = calendar.get(Calendar.MONTH) + 1; //現在の月 +// Calendar calendar = Calendar.getInstance(); +// year = calendar.get(Calendar.YEAR); //現在の年 +// month = calendar.get(Calendar.MONTH) + 1; //現在の月 scheduleViewModel.updateSchedulesByMonth(accountId, year, month, token); @@ -309,7 +318,6 @@ //カレンダーの初期表示 TableLayout tableLayout = view.findViewById(R.id.calendarlayout); TextView[][] days = calendar(firstDayOfWeek, prevMonthDay, lastDay); - String[][] daysString = calendarString(firstDayOfWeek, prevMonthDay, lastDay); for(int i = 0; i < 6; i++) { TableRow tableRow = (TableRow) tableLayout.getChildAt(i); for(int j = 0; j < 7; j++) { @@ -327,7 +335,26 @@ public void onClick(View view) { citrus.setCurMonth(month); citrus.setCurYear(year); + //指定した年月の一日の曜日を取得 + LocalDate firstDay = LocalDate.of(year, month, 1); + DayOfWeek dayOfWeek = firstDay.getDayOfWeek(); + int firstDayOfWeek = dayOfWeek.getValue(); + //前の月の最後の日付を取得 + LocalDate lastDate = LocalDate.of(year, month, 1); + LocalDate lastDayPrevMonth = lastDate.minusDays(1); + int prevMonthDay = lastDayPrevMonth.getDayOfMonth(); + //現在の月の最後の日付を取得 + YearMonth yearMonth = YearMonth.of(year, month); + LocalDate CurLastDay = yearMonth.atEndOfMonth(); + int lastDay = CurLastDay.getDayOfMonth(); + + String[][] daysString = calendarString(firstDayOfWeek, prevMonthDay, lastDay); citrus.setCurDay(Integer.parseInt(daysString[ii][jj])); + if(ii == 0 && Integer.parseInt(daysString[ii][jj]) > 8) { + citrus.setCurMonth(month - 1); + } else if (ii >= 4 && Integer.parseInt(daysString[ii][jj]) < 25) { + citrus.setCurMonth(month + 1); + } ((MainActivity) getActivity()).showFragment(new HomeFragment()); } });