CalendarSample.java
import java.util.Calendar;
public class CalendarSample {
public static void main(String[] args) {
int year = 2018;
int month = 8;
//Calendar instance creation
Calendar cal = Calendar.getInstance();
//Calendar settings (month- 1)
cal.set(year, month - 1, 1);
//Day of the week index at the beginning of the month (Sunday 1 to Saturday 7)
int weekIndex = cal.get(Calendar.DAY_OF_WEEK);
//Seeking the end of the month
int monthEndDay = cal.getActualMaximum(Calendar.DATE);
//Calendar creation
System.out.printf(" << %4d years%2d month>>\n", year, month);
System.out.println("Sunday Monday Tuesday Wednesday Thursday Friday Saturday");
//Day of the week offset space
for (int i = 1; i < weekIndex; i++) {
System.out.print(" ");
}
//Output from January to the end of the month
for (int day = 1; day <= monthEndDay; day++) {
System.out.printf("%3d", day);
//Line break on saturday
if ((day + weekIndex - 1) % 7 == 0) {
System.out.println();
}
}
}
}
Recommended Posts