-2

I am trying to convert java.util.Date into java.sql.Date with below function but this function is giving the time a s 00:00:00.000.

private java.sql.Date getCurrentDate() {
    java.util.Date today = new java.util.Date();
    return new java.sql.Date(today.getTime());
}

please help me how can I get both date and time in java.sql.date. thanks

Thomas
  • 87,414
  • 12
  • 119
  • 157
  • Please do your research before posting on stackoverflow: https://docs.oracle.com/javase/7/docs/api/java/sql/Date.html – Gilfoyle Apr 21 '16 at 08:46

2 Answers2

1

Converting java.util.Date to java.sql.Date will lost the hour,minute and second. So if it is possible, I suggest you use java.sql.Timestamp.

Iffo
  • 353
  • 7
  • 18
0
 final String str = "INSERT INTO table (curTime)  VALUES( ?)";
 final PreparedStatement preparedStatement =connection.prepareStatement(str);
  preparedStatement.setTimestamp(1, new Timestamp(date.getTime()));

you have to use a timestamp for that its not possible with java.sql.date since it removes data related to your time.

Priyamal
  • 2,919
  • 2
  • 25
  • 52