Saturday 10 September 2022

Convert java.sql.Date to java.util.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.

 

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.sql.Date to java.util.Date?

Get java.sql.Date object time in milliseconds.


java.sql.Date sqlDate = new java.sql.Date(System.currentTimeMillis());
long timeInMillis = sqlDate.getTime();

Get the java.util.Date object using timeInMillis.

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

DateConverter.java

package com.sample.app.time;

public class DateConverter {

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

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

}

Output

utilDate : Sun Sep 11 02:37:16 IST 2022
sqlDate : 2022-09-11



Previous                                                 Next                                                 Home



You may like

Date and time programs

Convert java.util.Date to calendar

Get current date and time using LocalDate, LocalTime, LocalDateTime

Convert time from 12 hour format to 24 hour format

Convert time from 24 hour format to 12 hour format

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

No comments:

Post a Comment