Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge java.sql.Date and java.sql.Time to java.util.Date?

I have two objects: a java.sql.Date and a java.sql.Time.
What is the best way to merge them into single java.util.Date?

In the database those columns are stored separately. I get them by JDBC rs.getDate("Date") and rs.getTime("Time");.

like image 1000
kostepanych Avatar asked Oct 15 '13 14:10

kostepanych


People also ask

How is date stored in database * Java sql date Java Util date Java sql datetime Java Util datetime?

In fact, the date is stored as milliseconds since the 1st of January 1970 00:00:00 GMT and the time part is normalized, i.e. set to zero. Basically, it's a wrapper around java. util. Date that handles SQL specific requirements.

What is the difference between Java Util date and Java sql date?

sql. Date just represent DATE without time information while java. util. Date represents both Date and Time information.

What is the difference between Java sql time and Java sql timestamp in Java?

If you compare to java. sql. Timestamp with equals() the method it will return false as value of nanosecond is unknown. That's all on the difference between java.

Does Java Util date have timestamp?

A thin wrapper around java. util. Date that allows the JDBC API to identify this as an SQL TIMESTAMP value. It adds the ability to hold the SQL TIMESTAMP fractional seconds value, by allowing the specification of fractional seconds to a precision of nanoseconds.


1 Answers

You can create two Calendar instances. In the first you initialize the date and in the latter the time. You can the extract the time values from the "time" instance and set them to the "date".

  // Construct date and time objects
  Calendar dateCal = Calendar.getInstance();
  dateCal.setTime(date);
  Calendar timeCal = Calendar.getInstance();
  timeCal.setTime(time);

  // Extract the time of the "time" object to the "date"
  dateCal.set(Calendar.HOUR_OF_DAY, timeCal.get(Calendar.HOUR_OF_DAY));
  dateCal.set(Calendar.MINUTE, timeCal.get(Calendar.MINUTE));
  dateCal.set(Calendar.SECOND, timeCal.get(Calendar.SECOND));

  // Get the time value!
  date = dateCal.getTime();
like image 191
istovatis Avatar answered Sep 28 '22 00:09

istovatis