콘솔창에 달력을 출력합니다.
int year과 int month를 변경하면 달력도 변경됩니다. 아래 코드에서는 2022년 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
32
33
34
35
36
37
38
39
40
41
|
import java.util.Calendar;
public class task01 {
public static void main(String[] args) {
int year = 2022;
int month = 10;
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, 1);
// 해당 달의 마지막 일자(DATE) 얻기
int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
// 해당 달의 시작 요일 얻기
// 1: 일요일 2: 월요일, 3: 화요일 ....
int startDay = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println(year + "년" + month + "월 \n");
System.out.println("일\t월\t화\t수\t목\t금\t토");
int currentDay = 1;
for (int i = 0; i <= 42; i++) {
if (i < startDay) {
System.out.print("\t");
} else {
System.out.printf("%02d\t", currentDay);
currentDay++;
}
if (i % 7 == 0) {
System.out.println();
}
if (currentDay > lastDay) {
break;
}
}
}
|
cs |
출력 결과
'JAVA' 카테고리의 다른 글
JAVA - 동전의 사용 개수를 구하는 문제 (500,100,50,10원) (0) | 2022.05.24 |
---|---|
JAVA - ArrayList를 알고리즘(버블정렬)과 메소드로 정렬해보자 (0) | 2022.05.23 |
Java - 배열로 로또 번호를 생성해보자 (0) | 2022.05.18 |
Java - 삼항 연산자로 주민등록번호를 분석해 성별 알아내기 (0) | 2022.05.17 |
JAVA - Scanner로 성적을 받아서 평균 계산하기 (0) | 2022.05.11 |
댓글