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


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

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 27-Jun-2020

376 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements