헤르메스 LIFE

[Exception] java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: HourOfDay 본문

Exception

[Exception] java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: HourOfDay

헤르메스의날개 2020. 12. 29. 13:32
728x90

 

getDateTimeFormat( "yyyyMMddHHmmssfff" ) 를 사용해서 함수롤 Call 했더니 아래와 같은 오류가 발생했습니다.

Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: HourOfDay
at java.time.LocalDate.get0(LocalDate.java:680)at java.time.LocalDate.getLong(LocalDate.java:659)
at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:298)
at java.time.format.DateTimeFormatterBuilder$NumberPrinterParser.format(DateTimeFormatterBuilder.java:2540)
at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2179)
at java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1746)
at java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1720)
at java.time.LocalDate.format(LocalDate.java:1691)
at sample.DateSample.getDateTimeFormat(DateSample.java:27)
at sample.DateSample.main(DateSample.java:14)

원인은 LocalDate Class 였습니다.

LocalDate 는 날짜만을 생성하는 Class 입니다.

날짜 + 시간 을 생성하기 위해서는 LocalDateTime 을 사용해야 합니다.

    public static String getDateTimeFormat( String dateFormat ) {
        LocalDate         nowDate   = LocalDate.now();
        DateTimeFormatter formatter = null;
        String            strDate   = "";
        try {
            formatter = DateTimeFormatter.ofPattern( dateFormat );
        } catch ( Exception e ) {
            formatter = DateTimeFormatter.ofPattern( "yyyyMMddHHmmss" );
        }
        strDate = nowDate.format( formatter );
        return strDate;
    }

    public static String getDateTimeFormat( String dateFormat ) {
        LocalDateTime     nowDate   = LocalDateTime.now();
        DateTimeFormatter formatter = null;
        String            strDate   = "";
        try {
            formatter = DateTimeFormatter.ofPattern( dateFormat );
        } catch ( Exception e ) {
            formatter = DateTimeFormatter.ofPattern( "yyyyMMddHHmmss" );
        }
        strDate = nowDate.format( formatter );
        return strDate;
    }

 

 

 

 

728x90