개발자 Q&A

개발하다 막혔다면? 여기서 질문하세요! 초보부터 고수까지, 함께 고민하고 해결하는 공간입니다. 누구나 자유롭게 질문하고 답변을 남겨보세요!

2025.04.07 16:13

IntlCalendar::isLenient 관련 질문 내용

목록
  • ORM수집가 19시간 전 2025.04.07 16:13 새글
  • 3
    1
제가 공부 중인 Java의 IntlCalendar 클래스에서 isLenient() 메소드에 대한 이해가 부족합니다.

isLenient() 메소드는 일정을 처리할 때 엄격한지 느슨한지 결정하는 메소드입니다. 하지만 정확하게 어떤 의미인지 이해하지 못하고 있습니다.

예를 들어, 일정을 2022-02-28으로 설정하고, 불편한 날짜인 2022-02-30을 입력했을 때, isLenient() 메소드는 어떤 결과를 반환할까요?

이 메소드의 동작에 대해 더 자세한 설명을 부탁드립니다.

    댓글목록

    profile_image
    나우호스팅  19시간 전



    isLenient() 메소드는 일정을 처리할 때 일자에 대한 유효성을 검사하는 방법을 결정하는 메소드입니다.

    isLenient() 메소드가 true인 경우, 일자에 대한 유효성을 검사하지 않습니다. 따라서 2022-02-30과 같은 불편한 날짜를 입력해도, 일정을 2022-02-28으로 설정한 것과 동일한 결과를 반환합니다.

    반면에, isLenient() 메소드가 false인 경우, 일자에 대한 유효성을 검사합니다. 따라서 2022-02-30과 같은 불편한 날짜를 입력하면, java.time.DateTimeException 예외가 발생합니다.

    예를 들어, 다음 코드를 실행하면, isLenient() 메소드가 true인 경우, 2022-02-30을 입력해도, 일정을 2022-02-28으로 설정한 것과 동일한 결과를 반환합니다.

    #hostingforum.kr
    java
    
    import java.time.*;
    
    import java.time.chrono.ChronoLocalDate;
    
    
    
    public class Main {
    
        public static void main(String[] args) {
    
            // isLenient() 메소드가 true인 경우
    
            IntlCalendar calendar = IntlCalendar.of(2022, 2, 28);
    
            calendar.setLenient(true);
    
            System.out.println(calendar.get(Calendar.YEAR) + "-" + calendar.get(Calendar.MONTH) + "-" + calendar.get(Calendar.DAY_OF_MONTH)); // 2022-02-28
    
            calendar.set(Calendar.YEAR, 2022);
    
            calendar.set(Calendar.MONTH, 2);
    
            calendar.set(Calendar.DAY_OF_MONTH, 30);
    
            System.out.println(calendar.get(Calendar.YEAR) + "-" + calendar.get(Calendar.MONTH) + "-" + calendar.get(Calendar.DAY_OF_MONTH)); // 2022-02-28
    
        }
    
    }
    
    


    반면에, 다음 코드를 실행하면, isLenient() 메소드가 false인 경우, 2022-02-30과 같은 불편한 날짜를 입력하면, java.time.DateTimeException 예외가 발생합니다.

    #hostingforum.kr
    java
    
    import java.time.*;
    
    import java.time.chrono.ChronoLocalDate;
    
    
    
    public class Main {
    
        public static void main(String[] args) {
    
            // isLenient() 메소드가 false인 경우
    
            IntlCalendar calendar = IntlCalendar.of(2022, 2, 28);
    
            calendar.setLenient(false);
    
            try {
    
                calendar.set(Calendar.YEAR, 2022);
    
                calendar.set(Calendar.MONTH, 2);
    
                calendar.set(Calendar.DAY_OF_MONTH, 30);
    
                System.out.println(calendar.get(Calendar.YEAR) + "-" + calendar.get(Calendar.MONTH) + "-" + calendar.get(Calendar.DAY_OF_MONTH));
    
            } catch (DateTimeException e) {
    
                System.out.println(e.getMessage()); // java.time.DateTimeException: Invalid date '2022-02-30'
    
            }
    
        }
    
    }
    
    

    2025-04-07 16:14

  • 개발자 Q&A 포인트 정책
      글쓰기
      50P
      댓글
      10P
  • 전체 9,858건 / 6 페이지

검색

게시물 검색