私はかなり新しいJavaです。以下の形式でカレンダーを印刷するプログラムを作成しようとしています。現在の日付を使用してカレンダーを開始し、1カ月先のカレンダーのみを作成することになっています。コンソールでグレゴリオ暦を印刷する
Example: October 26, 2016. Su M T W T F S [October] | | | | | 27 | 28 | 29 | | 30 | 31 | | | | | | [November] | | | 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 |
私はできるだけ多くのことを書いていますが、実際のコーディングに着手する際には問題にぶつかります。たとえば、カレンダーを正しい曜日に開始する方法を見つけることができず、1か月で正しい週数を印刷するのに問題があります。 calendar = new GregorianCalendar()を使用するようにコードをやり直す必要がありますか?または、私は単にinstanceOf()を使用し、カレンダークラスを使いこなす必要がありますか?
これは、私のカレンダーを壊さずに書くことができるほどのコードです。
StringBuilder calendarString = new StringBuilder();
Calendar calendar = Calendar.getInstance();
int maxDaysOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int dayOfMonth = Calendar.DAY_OF_WEEK;
int weekOfMonth = Calendar.WEEK_OF_MONTH;
int theMonth = Calendar.MONTH;
String month;
switch(theMonth) { // Months start with 0
case 0: month = "January"; break;
case 1: month = "February"; break;
case 2: month = "March"; break;
case 3: month = "April"; break;
case 4: month = "May"; break;
case 5: month = "June"; break;
case 6: month = "July"; break;
case 7: month = "August"; break;
case 8: month = "September"; break;
case 9: month = "October"; break;
case 10: month = "November"; break;
default: month = "December"; break;
}
calendarString.append(month + " " + calendar.get(Calendar.DAY_OF_MONTH) + ", ");
calendarString.append(calendar.get(Calendar.YEAR) + "."); // First line ends here (Add Jobs this month)
calendarString.append(System.getProperty("line.separator"));
calendarString.append(System.getProperty("line.separator"));
calendarString.append(" Su ");
calendarString.append(" M ");
calendarString.append(" T ");
calendarString.append(" W ");
calendarString.append(" T ");
calendarString.append(" F ");
calendarString.append(" S ");
calendarString.append(System.getProperty("line.separator"));