Saturday 10 September 2022

Convert java.util.Date to java.sql.Date in Java

java.util.Date

‘java.util.Date’ class is used to represent a specific instant in time, with millisecond precision since the 1st of January 1970 00:00:00 GMT (the epoch time).

 

How to get an instance of Date class?

Following constructors are used to get an instance of java.util.Date class.

 

public Date()

Allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.

 

public Date(long date)

Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.

 

java.util.Date object is not immutable

Date object is mutable, you can change the date using setTime method.

date1.setTime(0);	// Thu Jan 01 05:30:00 IST 1970
date2.setTime(1111111111111l);	// Fri Mar 18 07:28:31 IST 2005

 

Find the below working application.

 

JavaUtilDateDemo.java

 

package com.sample.app.time;

import java.util.Date;

public class JavaUtilDateDemo {

	public static void main(String[] args) {
		Date date1 = new Date();
		Date date2 = new Date(1642516388900l);

		System.out.println("date1 : " + date1);
		System.out.println("date2 : " + date2);

		// Update date1 and date2 objects.
		date1.setTime(0);	// Thu Jan 01 05:30:00 IST 1970
		date2.setTime(1111111111111l);	// Fri Mar 18 07:28:31 IST 2005

		System.out.println("\n\ndate1 : " + date1);
		System.out.println("date2 : " + date2);

	}

}

 

Output

date1 : Sat Sep 10 12:20:12 IST 2022
date2 : Tue Jan 18 20:03:08 IST 2022


date1 : Thu Jan 01 05:30:00 IST 1970
date2 : Fri Mar 18 07:28:31 IST 2005

 

java.sql.Date

java.sql.Date class extends java.util.Date class.

 

package java.sql;

public class Date extends java.util.Date {
	...........
	...........
}

 

java.sql.Date class is used to model a Date object whenever we want to work with DATE type in SQL. To conform with the definition of SQL DATE type, the millisecond values wrapped by a java.sql.Date instance are normalized by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated.

java.sql.Date sqlDate = new java.sql.Date(1642516388900l);	// 2022-01-18
java.util.Date utilDate = new java.util.Date(1642516388900l);	// Tue Jan 18 20:03:08 IST 2022

Find the below working application.

 

JavaSqlDateDemo.java

package com.sample.app.time;

public class JavaSqlDateDemo {

	public static void main(String[] args) {
		java.sql.Date sqlDate = new java.sql.Date(1642516388900l);	// 2022-01-18
		java.util.Date utilDate = new java.util.Date(1642516388900l);	// Tue Jan 18 20:03:08 IST 2022

		System.out.println("sqlDate : " + sqlDate);
		System.out.println("utilDate : " + utilDate);
	}

}

Output

sqlDate : 2022-01-18
utilDate : Tue Jan 18 20:03:08 IST 2022

Just like java.util.Date class, java.sql.Date class is also mutable.

 

In summary, java.util.Date object stores a date plus time information (Tue Jan 18 20:03:08 IST 2022) as milliseconds since the epoch, whereas java.sql.Date stores date only (2022-01-18).

 

How to convert java.util.Date to java.sql.Date?

Step 1: Get the time in milliseconds.

java.util.Date utilDate = new java.util.Date();
long timeInMillis = utilDate.getTime();

Step 2: Get an instance of java.sql.Date using the milliseconds that we got in step 1.

java.sql.Date sqlDate = new java.sql.Date(timeInMillis);

Find the below working application.

 

DateConverter.java

package com.sample.app.time;

public class DateConverter {

	public static void main(String[] args) {
		java.util.Date utilDate = new java.util.Date();
		long timeInMillis = utilDate.getTime();
		
		java.sql.Date sqlDate = new java.sql.Date(timeInMillis);

		System.out.println("utilDate : " + utilDate);
		System.out.println("sqlDate : " + sqlDate);

	}

}

Output

utilDate : Sat Sep 10 13:12:38 IST 2022
sqlDate : 2022-09-10


 

Previous                                                 Next                                                 Home

No comments:

Post a Comment