How to save date to oracle [duplicate] - java

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.

Related

Java MYSQL Timestamp with millieconds issue [duplicate]

This question already has answers here:
Timestamp with a millisecond precision: How to save them in MySQL
(4 answers)
Closed 2 years ago.
I am using MySQL and have a column type TimeStamp. I am not able to store or retrieve milliseconds.
Any help please ? Thanks!
public static Date getBeginAndEndTime(String time) {
DateTimeFormatter format = DateTimeFormatter.ofPattern("HHmmssSSS");
LocalTime lt = LocalTime.parse(time, format);
return Timestamp.valueOf(lt.atDate(LocalDate.now()));
}
Ex: Time value coming as is "140833222" and when I stored in database it looks like "2020-08-26 14:08:33" missing last milliseconds "222".
I need to store and retrieve including milliseconds.
You should use the datatype BIGINT as it allows you to store 8 bytes. The DATE field is generally used for just that, Date and DateTime implementations like LocalDateTime. Since you're using a LocalDate and subsequently a LocalTime you're better off just using a DATE type. Your method effectively becomes;
public static Date getBeginAndEndTime(String time) {
// cache this, it's a thread-safe object
DateTimeFormatter format = DateTimeFormatter.ofPattern("HHmmssSSS");
return LocalTime.parse(time, format).atDate(LocalDate.now()));
}

Cannot cast java.util.date to java.sql.date [duplicate]

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);

Convert between LocalDate and sql.Date [duplicate]

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();

difference between util.date and sql.date? [duplicate]

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

Java parse oracle timestamp to date.util.date [duplicate]

This question already has answers here:
Conversion of string with AM/PM date-time, from Oracle database
(3 answers)
Closed 6 years ago.
i want to parse oracle timestamp (01-MAY-12 01.00.47.000000000 PM) to java.util.Date
i used this:
Date dateStart=new SimpleDateFormat("yy-MM-dd HH:mm:ss.S").parse("01-MAY-12 01.00.47.000000000 PM");
but i get this error
java.text.ParseException: Unparseable date: "2012-5-1.13.0. 47. 0"
You shouldn't have to parse anything. Use one of the ResultSet.getTimestamp() methods, and you'll have a java.sql.Timestamp object directly, which extends java.util.Date.
java.sql.Timestamp ts = myResultSet.getTimestamp( … );
And this will have the additional advantage of being portale across databases and locales.
"yy-MM-dd"?
"01-MAY-12"
Is your day number really "12" and your year "01"?
And how come your error shows "2012-5-1.13.0. 47. 0", which is presumably a date in yet another format?
If you are trying to access it using JDBC then as #JB Nizet suggested use getTimestamp() or if you just have String and need to parse to Date then do it by following
Try with following format
01-MAY-12 01.00.47.000000000 PM
yy-MMM-dd hh.mm.ss.SSSSSSSSSS a
Working demo

Categories

Resources