본문 바로가기
Development/Java

LocalDateTime.parse(..) Error. java.time.format.DateTimeParseException: Text '2022-07-05T13:32:56' could not be parsed at index 10

by 버들도령 2022. 7. 22.
728x90

LocalDateTime.parse(..) Error. java.time.format.DateTimeParseException: Text '2022-07-05T13:32:56' could not be parsed at index 10

날짜 변환을 하다보면
아래와 같은 오류가 발생할 수 있습니다.
날짜의 입력데이터 포맷이 다르기 때문이지요.

2022-07-06 22:10:46[ERROR][DateUtils.java]getSession(85)[http-nio-8888-exec-1] : [DateUtils.convertTime] error Occurred : message - Text '2022-07-05T13:32:56' could not be parsed at index 10
java.time.format.DateTimeParseException: Text '2022-07-05T13:32:56' could not be parsed at index 10
	at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
	at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
	at java.time.LocalDateTime.parse(LocalDateTime.java:492)

 


이러한 오류를 만났을때 어떻게 해야할지 알아볼까요?
아래의 코드들을 이용하여 어떻게 되는지 테스트해보세요.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

.
.
.
.
String dateStr = "2022-07-05T13:32:56.152362";

DateTimeFormatter stdFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
LOG.info("[LocalDateTime] stdFormat date - {}", LocalDateTime.parse(dateStr, stdFormat) );

DateTimeFormatter sssFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.ssssss");
LOG.info("[LocalDateTime] sssFormat date - {}", LocalDateTime.parse(dateStr, sssFormat) );

// 나노 초 (nano seconds. 1/1000000000 sec이므로 9자리로 표현)
DateTimeFormatter nnnFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.nnnnnn");
LOG.info("[LocalDateTime] nnnFormat date - {}", LocalDateTime.parse(dateStr, nnnFormat) );

######## Output ########
[LocalDateTime] stdFormat date - 2022-07-05T13:32:56
[LocalDateTime] sssFormat date - 2022-07-05T13:32:56.152362
[LocalDateTime] nnnFormat date - 2022-07-05T13:32:56.000152362

 

ISO_LOCAL_DATE_TIME
 ISO_LOCAL_DATE_TIME
 
 public static final DateTimeFormatter ISO_LOCAL_DATE_TIME
The ISO date-time formatter that formats or parses a date-time without an offset, such as '2011-12-03T10:15:30'.
This returns an immutable formatter capable of formatting and parsing the ISO-8601 extended offset date-time format. The format consists of:

The ISO_LOCAL_DATE
The letter 'T'. Parsing is case insensitive.
The ISO_LOCAL_TIME
The returned formatter has a chronology of ISO set to ensure dates in other calendar systems are correctly converted. It has no override zone and uses the STRICT resolver style.

 

https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_LOCAL_DATE_TIME

 

DateTimeFormatter (Java Platform SE 8 )

Parses the text using this formatter, without resolving the result, intended for advanced use cases. Parsing is implemented as a two-phase operation. First, the text is parsed using the layout defined by the formatter, producing a Map of field to value, a

docs.oracle.com

 

728x90

댓글