Skip to content

Commit af14f21

Browse files
committed
add the package practice.date.
1 parent 7e2a8bc commit af14f21

File tree

4 files changed

+173
-7
lines changed

4 files changed

+173
-7
lines changed

src/practice/CalendarClass.java renamed to src/practice/date/CalendarClass.java

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
package practice;
1+
package practice.date;
22

33
import java.util.Calendar;
44

55
public class CalendarClass {
66

77
private Calendar cal = null;
8-
// 요일 표시
9-
private String[] header = { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" };
8+
private String[] header = { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" }; // 요일 표시
109

1110
private int startDayOfWeek; // 해당 월의 시작 요일 (SUN: 1, ... , SAT: 7)
1211
private int endDate; // 해당 월의 마지막 날짜
@@ -24,7 +23,7 @@ public CalendarClass(int year, int month) throws Exception {
2423
this.year = year;
2524
}
2625

27-
// month 값의 범위는 1 ~ 12으로 제한한다
26+
// 1월 ~ 12월이 아니면 예외를 발생한다
2827
if(month < 1 || month > 12) {
2928
throw new Exception("month에는 1 ~ 12의 값만 들어갈 수 있습니다.");
3029
} else {
@@ -39,7 +38,6 @@ public CalendarClass(int year, int month) throws Exception {
3938
startDayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
4039
endDate = cal.getActualMaximum(Calendar.DATE); // DATE와 DAY_OF_MONTH는 같다.
4140
}
42-
4341
}
4442

4543
public void printCalendar() {
@@ -74,13 +72,11 @@ public void printCalendar() {
7472
}
7573
System.out.println();
7674
}
77-
7875
}
7976

8077
public static void main(String[] args) throws Exception {
8178

8279
CalendarClass cal = new CalendarClass(2020, 2);
8380
cal.printCalendar();
84-
8581
}
8682
}
+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package practice.date;
2+
3+
import java.time.*;
4+
5+
public class CalendarNewClass {
6+
7+
private LocalDate inputDate = null;
8+
private String[] header = { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" }; // 요일 표시
9+
10+
private int startDayOfWeek; // 해당 월의 시작 요일 (MON: 1, ... , SUN: 7)
11+
private int endDate; // 해당 월의 마지막 날짜
12+
private int tableCols = header.length;
13+
private int year;
14+
private int month;
15+
16+
public CalendarNewClass(int year, int month) throws DateTimeException {
17+
18+
// AD 1년부터 시작한다
19+
if(year < 1) {
20+
this.year = 1;
21+
} else {
22+
this.year = year;
23+
}
24+
25+
// 1월 ~ 12월이 아니면 예외를 발생한다
26+
if(month < 1 || month > 12) {
27+
throw new DateTimeException("month에는 1 ~ 12의 값만 들어갈 수 있습니다.");
28+
} else {
29+
// parameter로 받은 year, month와 date를 세팅한다
30+
this.month = month;
31+
inputDate = LocalDate.of(this.year, this.month, 1);
32+
33+
int temp = inputDate.getDayOfWeek().getValue();
34+
startDayOfWeek = (temp == 7) ? 0 : temp; // Sunday를 0으로 변경한다
35+
endDate = inputDate.lengthOfMonth();
36+
}
37+
}
38+
39+
public void printCalendar() {
40+
41+
// year, month를 출력한다
42+
System.out.printf("%10d년 %5d월\r\n", year, month);
43+
44+
// day of week를 출력한다
45+
for(int i=0; i<header.length; i++) {
46+
System.out.printf("%s ",header[i]);
47+
}
48+
System.out.println();
49+
50+
// date를 출력한다
51+
int date = 1;
52+
outer_loop:
53+
while (true) {
54+
for(int j=0; j<tableCols; j++) {
55+
56+
if( date == 1 && j < startDayOfWeek ) {
57+
// 시작 요일 전까지는 공백을 출력한다
58+
System.out.printf("%3s ", "");
59+
} else {
60+
System.out.printf("%3s ", date + "");
61+
62+
if(date == endDate) {
63+
// 해당 월의 마지막 날짜에 다다르면 입력을 종료한다
64+
break outer_loop;
65+
}
66+
date++;
67+
}
68+
}
69+
System.out.println();
70+
}
71+
}
72+
73+
public static void main(String[] args) {
74+
75+
CalendarNewClass cnc = new CalendarNewClass(2020, 2);
76+
cnc.printCalendar();
77+
}
78+
}

src/practice/date/DateClass.java

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package practice.date;
2+
3+
import java.time.Instant;
4+
import java.util.Date;
5+
import java.text.SimpleDateFormat;
6+
7+
public class DateClass {
8+
9+
public static void main(String[] args) {
10+
11+
Date date = new Date();
12+
13+
Instant ins = date.toInstant();
14+
System.out.println(ins);
15+
Date.from(ins);
16+
17+
// compareTo(): before() + equals() + after()
18+
Date earlier = new Date(0); // January 1, 1970, 00:00:00 GMT: the standard base time
19+
Date now = new Date();
20+
Date later = new Date(now.getTime() + 1000);
21+
System.out.println(now.compareTo(earlier));
22+
System.out.println(now.compareTo(later));
23+
24+
// new Date().getTime() == System.currentTimeMillis()
25+
System.out.println(now.getTime()); // the standard base time으로부터 현재까지 몇 밀리초
26+
System.out.println(System.currentTimeMillis()); // the standard base time으로부터 현재까지 몇 밀리초
27+
28+
// SimpleDateFormat.format()을 이용해 원하는 방식으로 Date를 출력할 수 있다
29+
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy/hh:mm:ss a zzz");
30+
System.out.println(formatter.format(now));
31+
}
32+
}

src/practice/date/TimeClass.java

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package practice.date;
2+
3+
import java.time.*;
4+
5+
public class TimeClass {
6+
7+
public static void main(String[] args) throws DateTimeException {
8+
9+
LocalDateTime rightNow = LocalDateTime.now();
10+
System.out.println("rightNow: " + rightNow);
11+
System.out.println();
12+
13+
// LocalDate.getMonthValue() == Month.getValue()
14+
LocalDate enlistment = LocalDate.of(2016, Month.NOVEMBER, 14);
15+
Month month = enlistment.getMonth();
16+
System.out.println(month.getValue());
17+
System.out.println(enlistment.getMonthValue());
18+
System.out.println();
19+
20+
// convert LocalDate to LocalDateTime
21+
LocalDateTime earlier = enlistment.atStartOfDay();
22+
earlier = enlistment.atTime(0, 0, 0);
23+
// earlier = LocalDateTime.from(enlistment); // DateTimeException ¹ß»ý
24+
int result = enlistment.compareTo(LocalDate.of(2016, 11, 13)); // -1, 0, 1
25+
System.out.println("result: " + result);
26+
System.out.println("LocalDateTime: " + earlier);
27+
System.out.println();
28+
29+
// convert LocalDateTime to LocalDate
30+
LocalDate ld = earlier.toLocalDate();
31+
ld = LocalDate.from(earlier);
32+
System.out.println("LocalDate: " + ld);
33+
int result2 = rightNow.compareTo(earlier); // -3, 0, 3
34+
System.out.println("result2: " + result2);
35+
System.out.println();
36+
37+
// convert LocalTime to LocalDateTime
38+
LocalTime noon = LocalTime.of(12, 0, 0);
39+
System.out.println("noon: " + noon);
40+
LocalDateTime noonOfToday = noon.atDate(enlistment);
41+
System.out.println("LocalTimeDate: " + noonOfToday);
42+
LocalTime midnight = LocalTime.of(0, 0, 0);
43+
int result3 = noon.compareTo(midnight); // -1, 0, 1
44+
System.out.println("result3: " + result3);
45+
System.out.println();
46+
47+
// convert LocalDateTime to LocalTime
48+
LocalTime noon2 = noonOfToday.toLocalTime();
49+
noon2 = LocalTime.from(noonOfToday);
50+
System.out.println("LocalTime: " + noon2);
51+
System.out.println();
52+
53+
rightNow = LocalDateTime.of(2018, 11, 9, 12, 1, 32);
54+
System.out.println("rightNow: " + rightNow);
55+
System.out.println();
56+
57+
LocalTime timePoint = LocalTime.parse("00:00:01");
58+
System.out.println("timePoint: " + timePoint);
59+
}
60+
}

0 commit comments

Comments
 (0)