package sec02.verify.exam02;
import java.util.Calendar;
public class DatePrintExample2 {
public static void main(String[] args) {
Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);
int month = now.get(Calendar.MONTH)+1;
String strMonth = (month<10) ? ("0"+month) : (""+month);
int dayOfMonth = now.get(Calendar.DAY_OF_MONTH);
String strDayOfMonth = (dayOfMonth<10)? ("0"+dayOfMonth) : (""+dayOfMonth);
String[] dayArray = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
int dayOfWeek = now.get(Calendar.DAY_OF_WEEK);
String strDayOfWeek = dayArray[dayOfWeek-1] + "요일";
int hour = now.get(Calendar.HOUR_OF_DAY);
String strHour = (hour<10) ? ("0"+hour) : (""+hour);
int second = now.get(Calendar.SECOND);
String strSecond = (second<10) ? ("0"+second) : (""+second);
System.out.print(year + "년 ");
System.out.print(strMonth + "월 ");
System.out.print(strDayOfMonth + "일 ");
System.out.print(strDayOfWeek + " ");
System.out.print(strHour + "시 ");
System.out.print(strSecond + "분 ");
}
}
Here... String[] dayArray = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; Declare in this order. Replace the list [dayOfWeek-1] in this variable dayArray?I don't know how to interpret it.
java
now
는 Calendar
입니다. Calendar.get(Calendar.DAY_OF_WEEK)
을 하면 DayOfWeek.MONDAY
부터 DayOfWeek.Returns one of the values between SUNDAY
. where DayOfWeek
is Enum.
Let me explain Enum for a moment. If you know what Enum is, you can skip it.
Enumenumerable means a columnar object, which is simply a class of constants, and Enum can reduce errors in code writing. For example, let's say it creates a simple quadratic operation function without Enum. I can make it like this.
int eval(int a, int b, char op) throws ArithmeticException {
if (op == '+') return a + b;
if (op == '-') return a - b;
if (op == '*') return a * b;
if (op == '/') return a / b;
throw new ArithmeticException();
}
Here we see that op
can also contain characters other than +-*/
. A line like bal(1,2, '=')
will crash due to ArithmeticalException
, but no one will tell you that there will be an error until it is run.
Let's change the function like this. The op
factor became Enum.
enum Operator { ADD, SUB, MUL, DIV }
int eval(int a, int b, Operator op) throws ArithmeticException {
if (op == Operator.ADD) return a + b;
if (op == Operator.SUB) return a - b;
if (op == Operator.MUL) return a * b;
if (op == Operator.DIV) return a / b;
throw new ArithmeticException();
}
This change allows other parts of the code to call in the same way as bal(1, 2, Operator.ADD)
. Since only Operator.ADD
, Operator.SUB
, Operator.MUL
, Operator.DIV
are the values that can enter the op
position, you will not need to implement these four functions.
There are many other advantages, but because of these advantages, you can consider using Enum.
So the data type of now.get(Calendar.DAY_OF_WEEK)
is not int
, but a slightly special data type, DayOfWeek
. I think intdayOfWeek=now.get(Calendar.DAY_OF_WEEK);
will get an error, but if you want to receive it as int
instead, use the getValue()
method. You can do the same as in the example below.
int dayOfWeek = now.get(Calendar.DAY_OF_WEEK).getValue();
However, according to the official document , getValue()
returns Monday from 1 to Sunday 7. We need to modify the original function accordingly.
String[] dayArray={"Month,"Tuesday,"Wednesday,"Thursday,"Friday,"Saturday"};
// DayOfWeek is Monday 1 and Sunday 7
int dayOfWeek = now.get(Calendar.DAY_OF_WEEK).getValue();
// dayOfWeek - 1 is 0 on Monday, 6 on Sunday
String strDayOfWeek = dayArray[dayOfWeek - 1] + "요일";
© 2024 OneMinuteCode. All rights reserved.