How to merge java.sql.Date and java.sql.Time to java.util.Date?
I have two objects: a
java.sql.Date and a
What is the best way to merge them into single
In the database those columns are stored separately. I get them by JDBC
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();
Combine a java.util.Date object with a java.sql.Time , Combine a java.util.Date object with a java.sql.Time object getInstance(); tCal.setTime(time); dCal.set(Calendar.HOUR_OF_DAY, tCal.get(Calendar. Convert java.util.Date to java.sql.Time In order to store only time information in a database, almost all relational database have TIME datatype which only stores the time informtion. In java, we have java.sql.Time class that corresponds to SQL TIME and only contain information about hour, minutes, seconds and milliseconds.
Both have time and day, so you could do something like:
Date d = new Date(2013, 11, 23); Time t = new Time(23, 45, 45); d.setMinutes(t.getMinutes()); d.setHours(t.getHours()); d.setSeconds(t.getSeconds());
How to convert java.sql.Date to java.util.Date in Java , sql.Date. The trick involves getTime() method which returns the number of milliseconds from Epoch time. Since both java.util. How to convert java.sql.Date into a java.util.Date and vice-versa is a popular JDBC interview question which is also asked a follow-up question of the difference between java.sql.Date and java.util.Date which we have seen in our last article.
The answer by istovatis is correct except for not carrying the milliseconds over. I should like to contribute the modern answer.
java.sql.Time are now long outdated, and their replacements, the
LocalTime classes, make your task much simpler. Assuming you are using Java 8 or later and JDBC 4.2 or higher, get those types from your result set and combine them:
LocalDate date = rs.getObject("Date", LocalDate.class); LocalTime time = rs.getObject("Time", LocalTime.class); LocalDateTime dateTime = date.atTime(time);
In case you don’t have direct access to your SQL result set and get
java.sql.Time from some legacy API that you cannot change, I recommend you convert to the modern types and then use the same way of merging:
LocalDate date = sqlDate.toLocalDate(); LocalTime time = sqlTime.toLocalTime(); LocalDateTime dateTime = date.atTime(time);
You asked for a
java.util.Date. That class too is long outdated, so it’s better to use the
LocalDateTime from the above code (or perhaps convert to
Instant, depending on what you will be using it for). If you do need a
Date for some other legacy API that you cannot change either, convert like this:
Instant inst = dateTime.atZone(ZoneId.systemDefault()).toInstant(); java.util.Date utilDate = java.util.Date.from(inst);
If your legacy API required a
Timestamp object, it is even simpler:
Timestamp ts = Timestamp.valueOf(dateTime);
Link: Oracle tutorial: Date Time explaining how to use
JDBC, Here is a quick example of converting java.util.Date to java.sql.Date and then back to util Date. This article is in the next of earlier posts on Java Date and Time The java.sql.Date extends java.util.Date class.. Its main purpose is to represent SQL DATE, which keeps years, months and days. No time data is kept. 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.
How to convert java.util.Date to java.sql.Date, sql.Date object using the given milliseconds time value. Here is an example for the same : Example : convert java.util Time class is a part of Java SQL package.This class is a thin wrapper around java.util.Date that allows JDBC API to identify this as a SQL TIME value.The initial value of time is set to 1st January, 1970.
how to merge data with date and time (Beginning Java forum at , import java.sql.Statement;. import java.sql.Timestamp;. import java.util.ArrayList;. import java.util.Calendar;. import java.util.Date;. import java.util Java examples to convert from LocalTime to java.sql.Time and vice versa.. 1) LocalTime to java.sql.Time. Use java.sql.Time.valueOf() method to get sql time from LocalTime instance.
Support new date time API · Issue #57 · apache/olingo-odata4 · GitHub, Jump to bottom Merged Calendar (date component in the TZ of the calendar) - java.util.Date (date Date (time component in UTC) - java.sql. A class which can consume and produce dates in SQL Date format. Dates are represented in SQL as yyyy-MM-dd. Note that this date format only deals with year, month and day values. There are no values for hours,
- Why don't you use java.sql.Timestamp to set date and time at once?
- In database those columns stored separately. I get them by JDBC rs.getDate("Date") and rs.getTime("Time");
- FYI, the troublesome old date-time classes such as
java.text.SimpleDateFormatare now legacy, supplanted by the java.time classes built into Java 8 and later. See Tutorial by Oracle. See modern approach in Answer by Ole V.V.
- Bear in mind that those methods are deprecated because of very poor implementation of the concept of both Dates and Times.
- Yes, I know, but if (s)he wants to do precisely that, I guess (s)he already knows it.
- Using of deprecated methods is not a best way to do that.