Convert the Current Time to a java.sql.Date Object

Java 8Object Oriented ProgrammingProgramming

Firstly, create a Calendar class object.

Calendar calendar = Calendar.getInstance();

Now, import the following package.

import java.sql.Date;

Using a Date class now and creating an object would belong to the above package. Convert the current time to the java.sql.Date Object.

Date sqlDate = new Date((calendar.getTime()).getTime());

The following is an example.

Example

 Live Demo

import java.util.Calendar;
import java.sql.Date;
import java.text.ParseException;
public class Demo {
   public static void main(String[] args) throws ParseException {
      Calendar calendar = Calendar.getInstance();
      // object
      Date sqlDate = new Date((calendar.getTime()).getTime());
      System.out.println(sqlDate);
   }
}

Output

2018-11-19
raja
Published on 17-Dec-2018 09:48:45
Advertisements