This question already has answers here:
How to convert java.util.Date to java.sql.Date?
(17 answers)
How to covert date variable to java.sql.date
(1 answer)
Closed 5 years ago.
I have the following code snippet where i get continous error for casting java.util.date to java.sql.date.
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
storedProcedureCall.setLong(1, 12345);
storedProcedureCall.setDate(2, (java.sql.Date) sdf.parse("09/02/2017"));
storedProcedureCall.setDate(3, (java.sql.Date) sdf.parse("10/02/2017"));
What am i doing wrong here. I have imported the java.util.Date package as well.
The class java.util.Date is totally different to java.sql.Date. Thus it can not be cast to.
You probably meant to use java.util.Date. Check your signature of storedProcedureCall#setDate, it probably accidentally imported java.sql.Date instead of java.util.Date.
Note that nowadays the Date class should not be used anymore. Instead use the new modern API located inside the package java.time. Like this:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDateTime dateTime = LocalDateTime.parse(dateAsString, formatter);
Related
This question already has answers here:
Java string to date conversion
(17 answers)
Closed 5 years ago.
I've a problem with date. I've a date as string in format: "2017-05-10 16:30"
I'd like to convert it to date looking the same as I wrote before.
Please help me, thanks in advance!
You can use LocalDateTime.parse to create a LocalDateTime object, but the second part of your question didn't really make sense. The date object itself doesn't have a format, so it can't "look like" anything. You decide what format to adopt when you convert it back to a string.
A simple search in google or stackoverflow might lead to answer but here you go.
Pre Java 8
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse("2017-05-10 16:30");
Java 8
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime ldt = LocalDateTime.parse("2017-05-10 16:30", dtf);
This question already has answers here:
Converting a date string to a DateTime object using Joda Time library
(10 answers)
Closed 6 years ago.
Is there a way to convert a date in the format "YYYY-MM-dd" to "YYYY-MM-dd HH:mm:ss" using Joda?
Eg: "2016-01-21" to "2016-01-21 00:00:00"
Use DateTimeFormat class from Joda API. It helps you to format the date to the formatting of your choice. You can simply provide the format you want, like in this case you want "YYYY-MM-dd HH:mm:ss". The code below works with JodaTime 2.0 and above.
DateTime date = DateTime.parse("2016-01-21", DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss"));
There are two things in play here, first we need to parse the existing string into a DateTime object, which is done via the parse method, it also allows an additional argument, to convert the output into a different format. The longer but easier to understand implementation is given below.
DateTime date = DateTime.parse("2016-01-21");
DateTimeFormatter formatter = DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss");
date = formatter.parseDateTime(string);
Your question is not clear:
Do you want to just format "something representing a date" into a string with time of "00:00:00"?
Or are you trying to convert "something representing a date" into "something representing a date+time, with 00:00:00 as time"?
Or are you trying to convert a java.util.Date to a Joda org.joda.time.DateTime by ignoring the original time and set time to 00:00:00?
Or are you trying to convert a string of date with format of "YYYY-MM-dd" to another String with date+time, with 00:00:00 as time?
Or something else?
In Joda, the proper way to represent a date is by LocalDate, and the proper way to represent a "date + time" information (but not a instant of time) is by LocalDateTime. DateTime is representing a instant of time. With these basic understanding:
Answer for Q1:
String result = DateTimeFormat.forPattern("YYYY-MM-dd", myLocalDate);
Answer for Q2:
LocalDateTime result = myLocalDate.toLocalDateTime(LocalTime.MIDNIGHT);
Answer for Q3:
DateTime result = new DateTime(javaUtilDate).withTimeAtStartOfDay();
Answer for Q4:
String result = dateString + " 00:00:00";
This question already has answers here:
Java Date - Insert into database
(10 answers)
Closed 7 years ago.
I want to save date to oracle from eclipse java program.
Right now I am using this code
DateFormat dt=new SimpleDateFormat("MM/dd/yyyy");
java.sql.Date dob=(java.sql.Date)dt.parse("02/02/2015");
ob.setDateOfBirth(dob);
The table has a column named Date_of_birth having date datatype.
But i am getting an error
Exception in thread "main" java.lang.ClassCastException:
java.util.Date cannot be cast to java.sql.Date
at com.TestCustomerDao.main(TestCustomerDao.java:22)
Please Help
The class that DateFormat.parse(String) returns is java.util.Date.
You need a java.sql.Date, which is actually a subclass of the above. You can only cast an object up to a class it inherits from, you can't cast down to a class that inherits from it.
In order to do that properly, you need to create a new java.sql.Date object from the java.util.Date object by using:
java.sql.Date dob = new java.sql.Date( dt.parse("02/02/2015").getTime() );
This gets the internal time stamp (representation of time as milliseconds since January 1970) from the java.util.Date object, and creates a java.sql.Date that is based on the same time stamp.
This question already has answers here:
How to convert LocalDate to SQL Date Java?
(3 answers)
Closed 7 years ago.
This post was edited and submitted for review last year and failed to reopen the post:
Original close reason(s) were not resolved
What's the correct way to convert between java.sql.Date and LocalDate (in both directions) in Java 8 (or higher)?
The Java 8 version (and later) of java.sql.Date has built in support for LocalDate, including toLocalDate and valueOf(LocalDate).
To convert from LocalDate to java.sql.Date you can use
java.sql.Date.valueOf( localDate );
And to convert from java.sql.Date to LocalDate:
sqlDate.toLocalDate();
Time zones:
The LocalDate type stores no time zone information, while java.sql.Date does. Therefore, when using the above conversions, the results depend on the system's default timezone (as pointed out in the comments).
If you don't want to rely on the default timezone, you can use the following conversion:
Date now = new Date();
LocalDate current = now.toInstant()
.atZone(ZoneId.systemDefault()) // Specify the correct timezone
.toLocalDate();
This question already has answers here:
Difference between Date class in Package java.util & Package java.sql
(5 answers)
java.util.Date vs java.sql.Date
(7 answers)
Closed 9 years ago.
Can many one help me?
import java.util.Date;
public class DateDemo {
public static void main(String [] p)
{
java.util.Date date1 = new Date();
#SuppressWarnings("deprecation")
java.sql.Date date2 = new java.sql.Date(2013, 12, 22);
System.out.println(date1.compareTo(date2));
}
}
As per Javadoc java.sql.Date is a thin wrapper around millisecond value which is used by JDBC to identify an SQL DATE type.
java.sql.Date just represent DATE without time information while java.util.Date represent both Date and Time information. This is the major differences why java.util.Date can not directly map to java.sql.Date.
In order to suppress time information and to confirm with definition of ANSI SQL DATE type, the millisecond values used in java.sql.Date instance must be "normalized by setting the hours, minutes, seconds and milliseconds to zero in the timezone with with DATE instance is associated. In other words all time related information is removed from java.sql.Date class.
Read more: http://javarevisited.blogspot.com/2012/04/difference-between-javautildate-and.html#ixzz2bBWDwBD0
Date from util package has is combination of date and time while Date from SQL package only represent only Date part. to be precise Date contains year, month and day information while Time means hour, minute and second information. java.util.Date contains all year, month, day, hour, minute and second information. In fact java.sql.Time and java.sql.TimeStamp which represents TIME and TIMESTAMP type of SQL database is more close to java.util.Date, It extends java.util.DATE and if you are using java.util.DATE in your Class to represent DATE value its better to use TIMESTAMP type in Database and java.sql.Time in JDBC or DAO code.
Read more: http://javarevisited.blogspot.com/2012/04/difference-between-javautildate-and.html#ixzz2bBmF4lBd