본문 바로가기
  • 평범한 나의 개발공부 일지
나의 개발 기록/ERROR !!

[JAVA] Cannot format given Object as a Date / ERROR 해결

by 블랑 블랑 2023. 1. 26.
반응형

date format 하는과정에서 오류가 났다.

현재 날짜를 가져와서 그 날짜를 format 하는과정에서 오류가 난것같다.

 

- 에러 로그

2023-01-26 17:55:30 ERROR [org.springframework.scheduling.support.TaskUtils$LoggingErrorHandler:95] - Unexpected error occurred in scheduled task.
java.lang.IllegalArgumentException: Cannot format given Object as a Date
	at java.text.DateFormat.format(DateFormat.java:342) ~[?:?]
	at java.text.Format.format(Format.java:159) ~[?:?]
	at com.pangpany.dcaf.schedule.ScheduleUtil.kopisSendCron(ScheduleUtil.java:157) ~[Dcaf-Common-1.0.0.jar:?]
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[?:?]
	at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
	at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?]
	at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65) ~[spring-context-4.3.25.RELEASE.jar:4.3.25.RELEASE]
	at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) [spring-context-4.3.25.RELEASE.jar:4.3.25.RELEASE]
	at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81) [spring-context-4.3.25.RELEASE.jar:4.3.25.RELEASE]
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539) [?:?]
	at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) [?:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) [?:?]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) [?:?]
	at java.lang.Thread.run(Thread.java:833) [?:?]
2023-01-26 17:56:00 INFO [com.pangpany.dcaf.schedule.ScheduleUtil:147] - #############kopis전송test#############
2023-01-26 17:56:00 ERROR [org.springframework.scheduling.support.TaskUtils$LoggingErrorHandler:95] - Unexpected error occurred in scheduled task.
java.lang.IllegalArgumentException: Cannot format given Object as a Date
	at java.text.DateFormat.format(DateFormat.java:342) ~[?:?]
	at java.text.Format.format(Format.java:159) ~[?:?]
	at com.pangpany.dcaf.schedule.ScheduleUtil.kopisSendCron(ScheduleUtil.java:157) ~[Dcaf-Common-1.0.0.jar:?]
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[?:?]
	at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
	at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?]
	at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65) ~[spring-context-4.3.25.RELEASE.jar:4.3.25.RELEASE]
	at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) [spring-context-4.3.25.RELEASE.jar:4.3.25.RELEASE]
	at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81) [spring-context-4.3.25.RELEASE.jar:4.3.25.RELEASE]
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539) [?:?]
	at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) [?:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) [?:?]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) [?:?]
	at java.lang.Thread.run(Thread.java:833) [?:?]

 

- 문제가 된 부분

LocalDate now = LocalDate.now();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
String formatedNow = formatter.format(now);

 


 

해결! 

 

LocalDate 객체를 Date 객체로 바꿔줬다.

simpleDateFormat 은 정해진 형식만 format을 해줄수 있는데

LocalDate로 현재날짜를 추출하면 아래와같이 값이 나온다.

이 형태의 값으로는 simpleDateFormat가 format을 해주지 못한다

 

따라서 아래처럼 Date객체로 추출한 현재날짜로 format을 해주어야 정상적으로 작동한다.

 

최종적으로, 

Date now = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
String formatedNow = formatter.format(now);

요렇게 바꿔줬더니

오류가 발생하지 않고 정상적으로 작동했다!

 

반응형

댓글