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.
Let's change the function like this. The op
factor became Enum.
There are many other advantages, but because of these advantages, you can consider using Enum.
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] + "요일";
2023-02-05 17:21
If you have any answers or tips
Popular Tags
python x 4647
android x 1593
java x 1494
javascript x 1427
c x 927
c++ x 878
ruby-on-rails x 696
php x 692
python3 x 685
html x 656
© 2025 OneMinuteCode. All rights reserved.