JueLance opened a new issue, #12983:
URL: https://github.com/apache/dolphinscheduler/issues/12983

   ### Search before asking
   
   - [X] I had searched in the 
[issues](https://github.com/apache/dolphinscheduler/issues?q=is%3Aissue) and 
found no similar issues.
   
   
   ### What happened
   
   
https://github.com/apache/dolphinscheduler/blob/dev/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/json/DateJsonSerializer.java
   
   
在将Date转换成String时,入参是java.util.Date,然后内部会调用date.toInstant()方法转换成LocalDateTime。然而这里有一个问题是:jdk的内部实现里,java.sql.Date是继承自java.util.Date,但是又没有实现toInstant()方法,导致调用时直接报错。jdk方法源代码:
   
      /**
       * This method always throws an UnsupportedOperationException and should
       * not be used because SQL {@code Date} values do not have a time
       * component.
       *
       * @exception java.lang.UnsupportedOperationException if this method is 
invoked
       */
       @Override
       public Instant toInstant() {
           throw new java.lang.UnsupportedOperationException();
       }
   
   ### What you expected to happen
   
   下面的代码提供一种思路将sql.Date转换成util.Date:
   
   @JsonComponent
   public class DateJsonSerializer extends JsonSerializer<Date> {
       @Override
       public void serialize(Date value, JsonGenerator gen, SerializerProvider 
serializers) throws IOException {
           Date temp = value;
           // 
JDK的内部实现里面,sql.Date是继承自util.Date,sql.Date里面重写了很多util.Date里面的方法,但是又没有实现。
           // 后续在格式化的时候需要调用Date里面的方法报错。
           if (value instanceof java.sql.Date) {
               Calendar calendar = Calendar.getInstance();
               calendar.setTime(value);
               temp = calendar.getTime();
           }
           gen.writeString(DateUtils.dateToString(temp));
       }
   }
   
   
   ### How to reproduce
   
   输入java.sql.Date即可复现
   
   ### Anything else
   
   _No response_
   
   ### Version
   
   dev
   
   ### Are you willing to submit PR?
   
   - [X] Yes I am willing to submit a PR!
   
   ### Code of Conduct
   
   - [X] I agree to follow this project's [Code of 
Conduct](https://www.apache.org/foundation/policies/conduct)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: 
[email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to