dayArray [dayOfWeek-1] How do I interpret this?

Asked 1 years ago, Updated 1 years ago, 311 views

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

2023-02-05 15:41

1 Answers

nowCalendar입니다. 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.

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.

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.

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


© 2024 OneMinuteCode. All rights reserved.