How do I change java.util.Date to java.sql.Date?

Asked 1 years ago, Updated 1 years ago, 147 views

Input is being processed using the java.util.Date class. You want to use this data to create an SQL query. Therefore, you must convert to an object in the java.sql.Date class.

However, we found that the java.util.Date object cannot be automatically/forced to an object of class java.sql.Date.

The Java API itself is still unfamiliar to me. I don't know how to handle this.

sql datetime java date

2022-09-22 21:37

1 Answers

Don't worry.

You can do it as follows.

public class MainClass {

  public static void main(String[] args) {
    java.util.Date utilDate = new java.util.Date();
    java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
    System.out.println("utilDate:" + utilDate);
    System.out.println("sqlDate:" + sqlDate);

  }

}

Please refer to the following link.


2022-09-22 21:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.