Java 날짜로 요일 구하기
1. Joda Time
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 org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
public class JodaTime {
public static void main(String[] args) {
DateTime dateTime = new DateTime();
DateTimeFormatter dtf = DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss");
String today = dtf.print(dateTime);
int dayOfWeek = dateTime.getDayOfWeek();
String dayOfWeekStr = "";
switch (dayOfWeek) {
case 1:
dayOfWeekStr = "월요일";
break;
case 2:
dayOfWeekStr = "화요일";
break;
case 3:
dayOfWeekStr = "수요일";
break;
case 4:
dayOfWeekStr = "목요일";
break;
case 5:
dayOfWeekStr = "금요일";
break;
case 6:
dayOfWeekStr = "토요일";
break;
case 7:
dayOfWeekStr = "일요일";
break;
}
System.out.println(today);
System.out.println(dayOfWeekStr);
}
}
2. Java 8
1
2
3
4
5
6
7
8
9
10
11
12
13
import java.time.DayOfWeek;
import java.time.LocalDate;
public class Java8 {
public static void main(String[] args) {
LocalDate localDate = LocalDate.now();
DayOfWeek dayOfWeek = localDate.getDayOfWeek();
System.out.println(localDate);
System.out.println(dayOfWeek);
}
}
[출처 및 참고]
This post is licensed under CC BY 4.0 by the author.