str_to_date error in MySQL (Java) - java

I have the following column value which I am converting and storing in another column
Nov 22 2014 00:00:00 AM
Now I am using the following query to convert it to Date format and store in another column
UPDATE DataNov2014 SET Datee = str_to_date(Date,'%d %b %Y %H:%i:%s');
But I am getting the following exception
Exception in thread "main" java.sql.SQLException: Incorrect datetime
value: 'Nov 22 2014 00:00:00 AM' for function str_to_date
Is there any mistake in my query/date format ??
Any help would be appreciated, Thanks

tyr this
UPDATE DataNov2014 SET Date= to_char(Date,'mon dd yyyy mm:ss:hh');

I suggest you use a PreparedStatement and a Date bind parameter. Also, you can use try-with-resources. Putting it all together, something like
String sql = "UPDATE DataNov2014 SET Datee = ?";
DateFormat sdf = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
try (PreparedStatement ps = conn.prepareStatement(sql)) {
Date theDate = sdf.parse("11-22-2014 00:00:00");
ps.setDate(1, new java.sql.Date(theDate.getTime()));
int count = ps.executeUpdate();
if (count > 0) {
System.out.printf("Query updated %d rows.%n", count);
} else {
System.out.println("Query didn't update any rows");
}
} catch (Exception e) {
e.printStackTrace();
}

Assumes your field Datee datatype is Datetime
UPDATE DataNov2014 SET Datee = str_to_date('Nov 22 2014 00:00:00 AM','%M %d %Y %H:%i:%s');

Related

Date formatting issue for sql format 'YYYY-MM-DD HH24:MI:SS.FF' to java

In my sql query I have date formating as :
to_char(crtd_ts,'YYYY-MM-DD HH24:MI:SS.FF') crtd
and my database column stores the value as 2018-4-24.8.1. 30. 404577000
What is the way of doing the same thing in Java?
I tried this way, but I am getting an error.
private String formatDate(String refCrtdTs) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MMM-dd HH24:mm:ss.ff");
String dateInString = refCrtdTs;
try {
Date date = sdf.parse(dateInString);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
I am trying to replace the above sql query with hibernate criteria. My hibernate entity for the column is defined as
#Column(name="REF_CRTD_TS",columnDefinition="timestamp")
private String refCrtdTs;
and the column type in Oracle is Timestamp.
So hibernate criteria returns me this value as String which I want to format now.
Try this,
yy-M-d H:m:s.F
And your database column stores the value should be "2018-4-24 8:1:30.114"
F is Day in year (example: Feb 1st => F = 32, 31 day in Jan + 1 day in Feb)
You have to match same patter as per your sql, just try the same pattern to get in Java
I think this will work, please comment if you required more info
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS aa");

Java/OAF Date Conversion Error through Callable Statement

I am passing date in OAF page to SQL database through Callable statement. I looked at many solution but still I'am getting no Success. This is What I've written.
OAMessageDateFieldBean validFrom =(OAMessageDateFieldBean)webBean.findIndexedChildRecursive("VaidFrom");
String getValidFrom = validFrom.getValue(pageContext).toString();
SimpleDateFormat formatter = new SimpleDateFormat("DD/MM/YYYY");
System.out.println("Formatter Executed " + formatter);
Date dateValidFrom ;
dateValidFrom = null;
java.sql.Date sqlDate = null;
try {
System.out.println("Formatter Date Valid from "+formatter.parse(getValidFrom));
dateValidFrom = (java.sql.Date)formatter.parse(getValidFrom);
sqlDate = new java.sql.Date(dateValidFrom.getTime());
System.out.println("SQL date is "+sqlDate);
}catch (Exception e){
System.out.println("------ "+e);
}
callableStatement.setDate(3, sqlDate);
I tried to debug it and I think error is coming in dateValidFrom = (java.sql.Date)formatter.parse(getValidFrom);
The error it is showing is - java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date
I've used Date Picker to pick date into the field. The value that is stored in String is
2017-05-01 00:00:00.0
And the value coming after SimpleDateFormat is used is -
Mon Dec 05 00:01:00 IST 2016
Please help.
TIA
Don't cast dateValidFrom in java.sql.Date beacause formatter.parse return a java.util.Date
Try something like that :
java.util.Date dateValidFrom = formatter.parse(getValidFrom);
java.sql.Date sqlDate = new java.sql.Date(dateValidFrom.getTime());

Change date format to Oracle Date Format

I am reading few data from a text file using java code,along with date in format (30-OCT-2012 12-22-44-991) and i want to store these details in Oracle Database but in the same format as used by oracle.
I tried To_date but of no use, it gives error.
Kindly help me.
Use SimpleDateFormat in Java to parse your String to a java.util.Date. Then use a PreparedStatement and set the date on that.
Edit:
You can use a PreparedStatement like Aleksander Blomskøld already suggested and with help from Using Oracle to_date function for date string with milliseconds:
final sql = "INSERT into IFT_VEHICLE_STATUS (LATITUDE, LONGITUDE, UPDATED_AT) " +
"VALUES (?, ?, to_timestamp(?, 'DD.MM.YYYY HH24:MI:SS:SSFF3'))";
final PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setInt(1, 81000000);
pstmt.setInt(2, 162000000);
pstmt.setDate(3, oracleDate);
pstmt.execute();
Old:
Are you trying to convert a java.util.Date into a java.sql.Timestamp? You could do that like this:
try {
final Date javaUtilDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2012-01-20 11:46:06");
System.out.println("javaUtilDate = " + javaUtilDate);
final Timestamp oracleDate = new Timestamp(javaUtilDate.getTime());
System.out.println("oracleDate = " + oracleDate);
} catch (ParseException e) {
e.printStackTrace();
}
This will give the following output:
javaUtilDate = Fri Jan 20 11:46:06 CET 2012
oracleDate = 2012-01-20 11:46:06.0

Convert Date into MYSQL Date Format

I have this MySQL Date data for 6 months in this format:
2010-01-01 to 2010-07-01
But from the UI the ToDate and FromDate are passed in this format:
Jan 1, 2010 and July 1, 2010
Please tell me how can I convert this data into MySQL equivalent format?
First create a SimpleDateFormat for parsing your input from the UI:
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy");
Next parse an input into a java.sql.Date (which is unfortunately named and different from java.util.Date). So for example:
java.sql.Date date = new java.sql.Date(sdf.parse(fromDate).getTime());
Finally use the date to pass to JDBC when making your database queries. Such as:
Connection con; // assuming you have a database connection
PreparedStatement ps = con.prepareStatement("SELECT * FROM table WHERE x = ?");
ps.setDate(1, date);
ResultSet resultSet = ps.executeQuery();

Parsing a string to date gives 01/01/0001 00:00:00

String dateimput=request.getParameter("datepicker");
System.out.printl("datepicker:" +dateimput);
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Date dt = null;
try
{
dt = df.parse(dateimput);
System.out.println("date imput is:" +dt);
} catch (ParseException e)
{
e.printStackTrace();
}
*datepicker:04/29/2010 (value I currently selected from datepicker).
*the field in database is typed date.
1-date imput is:Thu Apr 29 00:00:00 CEST 2010
and in database level it is inserted like that 01/01/0001 00:00:00
Your Java code will work fine.
04/29/2010 will give you a date object with the correct time/date set.
You said the problem is during the Database insert, so you should tell us the used database and post the code you are using for the insert.
Based on your comment to echox's answer. It looks like your problem might be not putting quotes around your date value in your insert statement.

Categories

Resources