Java/OAF Date Conversion Error through Callable Statement - java

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

Related

java to mysql. I need convert from string parametre to timestamp

I'm trying to parser String to Timestamp because I need to save this data on bbdd mysql.
String dateString: "2018-10-17T22:37:10.000+0000";
java.sql.Timestamp timeStampDate = null;
try {
DateFormat formatter;
formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
Date date = (Date) formatter.parse(dateString);
timeStampDate = new Timestamp(date.getTime());
} catch (ParseException e) {
log.debug("ERROR parser String to Timestamp to save bbdd. ", e.getMessage());
}
When I run my app I get this catch message:
ERROR parser String to Timestamp to save bbdd. Unparseable date: "2018-10-17T22:37:10.000+0000"
Can anybody help me?
change your mask to
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS");
so you have
java.sql.Timestamp timeStampDate = null;
String dateString = "2018-10-17T22:37:10.000+0000";
try {
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date date = formatter.parse(dateString);
timeStampDate = new Timestamp(date.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
By the way you should not need to cast the Date
Apologies for my slackness, in my haste I did not test the output and as per #andreas comment, the correct mask is actually yyyy-MM-dd'T'HH:mm:ss.SSSZ
java.time
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSXX");
String dateString = "2018-10-17T22:37:10.000+0000";
OffsetDateTime odt = OffsetDateTime.parse(dateString, formatter);
System.out.println("Parsed datetime: " + odt);
Output from this code is:
Parsed datetime: 2018-10-17T22:37:10Z
For saving into MySQL it’s good to use a datetime object, but the Timestamp class has design problems and is now long outdated. I am sorry that I don’t have the experience with MySQL, but I think the following should work:
PreparedStatement ps = myDatabaseConnection.prepareStatement(
"insert into my_table (my_timestamp) values (?)");
ps.setObject(1, odt);
Link: Oracle tutorial: Date Time explaining how to use java.time.

Data truncation: Incorrect datetime value: '' for column 'date' at row 1

My application is getting this error:
Data truncation: Incorrect datetime value: '2-24-2015' for column 'POrder_Date' at row 1
I have MySQL connector java v-5.1.7
java.util.Date date = new java.util.Date();
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
String date1, mon, datex, year, yearx, currentDate;
int d, d1;
following code is in my class,s constructor:
date1=df.format(date);
d=date1.indexOf('/');
mon=date1.substring(0,d);
d1=date1.lastIndexOf('/');
datex=date1.substring(d+1,d1);
yearx=date1.substring(d1+1);
year="20"+yearx;
currentDate=mon+"-"+datex+"-"+year;
System.out.println("current date "+currentDate);
mysql default date format "yyyy-mm-dd".change date format then store .may be it will work.
java.util.Date date = new java.util.Date();
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
String date1,mon,datex,year,yearx,currentDate;
int d,d1;
date1=df.format(date);
d=date1.indexOf('/');
mon=date1.substring(0,d);
d1=date1.lastIndexOf('/');
datex=date1.substring(d+1,d1);
yearx=date1.substring(d1+1);
year="20"+yearx;
currentDate=mon+"-"+datex+"-"+year;
System.out.println("current date "+currentDate);
//change currentdate format MM-dd-yyyy into yyyy-MM-dd
try {
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
Date convertedCurrentDate = sdf.parse(currentDate);
System.out.println(new SimpleDateFormat("yyyy-MM- dd").format(convertedCurrentDate));
} catch (ParseException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
check print date format like(2015-05-25).
You have two choices here:
Either use updateDate() where you presently use rs.updateString(2,podate). Pass a Date object to updateDate().
Or change your internal date formatting throughout to comply with the ISO yyyy-mm-dd standard.

java.text.ParseException: Unparseable date: "531772200000"

I need to convert the string 531772200000 to a Java Date object. The date is stored in a database.
When I am doing this, I am getting java.text.ParseException: Unparseable date: "531772200000".
My code:
String dateToBeConverted = String.valueOf(dbObject.get("customerDateOfBirth"));
String parseabledate = dateToBeConverted
.replace("/Date(","")
.replace(")/","")
.replace("+0530", "");
dbObject.put("_id", String.valueOf(dbObject.get("userInfoId")));
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
Date date;
date = formatter.parse(parseabledate);
This looks like a timestamp value, this will probably give you the date:
new Date(Long.parseLong("531772200000"));
which works out at Fri Nov 07 1986 18:30:00 GMT+0000
Here is one solution that will provide the date correctly formatted.
String d = "531772200000";
SimpleDateFormat newFormatter = new SimpleDateFormat("dd-MMM-yyyy");
try {
Date date1 = new Date(Long.parseLong(d));
System.out.println(newFormatter.format(date1)); //Will print out as 07-Nov-1986
} catch (ParseException e) {
e.printStackTrace();
}
Another solution is to use Joda Time with a solution below.
String d = "531772200000";
DateTime newDate = new DateTime(Long.parseLong(d));
DateTimeFormatter dd = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
DateTime date = dd.parseDateTime(newDate.toString());
System.out.println(date.toString("dd-MMM-yyyy")); //Prints out as 07-Nov-1986
Personally I prefer to use the second solution (Joda Time) as it is much nicer and easier.
It is epoh time format.
Somebody already answered about that in https://stackoverflow.com/a/20732668/379779
and test your epoh time in this website http://www.epochconverter.com/

How to save current date and time to database using java?

I am using the following code to get the current date and time, but the output is not what I am expecting and I cant save it into database.
Output >> current: Tue Mar 05 09:58:26 EST 2013
Expected output >> current: 2013-03-05 9:58:26
.....{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat parseFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
try {
System.out.println("current: " +parseFormat.parse(dateFormat.format(date)));
return parseFormat.parse(dateFormat.format(date));
} catch (ParseException ex) {
Logger.getLogger(ConstructionModel.class.getName()).log(Level.SEVERE, null, ex);
}
return date;
}
......
ps.setDate(....) <<< failed
Database
name type
mydate Date
You don't need to parse before formatting:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String frmtdDate = dateFormat.format(date);
System.out.println("frmtdDate: " + frmtdDate);
However, if you are trying to fit the date into some DB statement, you should not do it in the form of text, instead use one of the JDBC setters that utilize java.sql.Date or java.sql.Timestamp
You need to use sql timestamp for saving to the database. Convert your java.util.Date to java.sql.Timestamp:
ps.setTimestamp(new java.sql.Timestamp(myDate.getTime()));
format takes a Date and returns a formatted String. parse takes a formatted String and returns a Date object. When you do parseFormat.parse(dateFormat.format(date)) you are converting Date to String and to Date again. The value that is printed is the default representation provided by Date.toString() instead of the formatted string.
System.out.println("current: " +dateFormat.format(date));

How to get a Date object from String

DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy' 'HH:mm:ss");
Date d = (Date)formatter.parse(dateTime);
System.out.println("date in controller "+d);
I get the output as
date in controller Mon Dec 31 16:04:57 IST 2012
Please suggest a method to output the date in MM/dd/yyyy HH:mm:ss format.
Need the output in date format and not as string so as to store the output in datetime field in mysql db
Need the output in date format and not as string so as to store the output in datetime field in mysql db
After the statement
Date d = (Date)formatter.parse(dateTime);
java.sql.Date sqldate = new java.sql.Date(d.getTime())
you have got a java.util.Date object and you can store it as it is in mysql DB (column type : datetime).
However, when you are printing d, it defaults to the .toString() implementation. I think you expected some output like Date# but the toString printed in user readable format thats why the confusion.
You are using d an object of Date class, so its toString method is called that gives the output as Mon Dec 31 16:04:57 IST 2012.
If you want to display it in the format that you have specified in your SimpleDateFormat then try using this :
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy' 'HH:mm:ss");
Date d = (Date)formatter.parse(dateTime);
System.out.println("date in controller "+ formatter.format(d));
Don't see why the single-qoutes (') are used in the format-string and you also need to catch ParseException:
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date d = new Date();
try {
d = (Date)formatter.parse("12/31/2012 12:13:14");
} catch(ParseException pex) { System.out.println(pex.getMessage()); }
// convert the date into java.sql.Date
java.sql.Date sqldate = new java.sql.Date(d.getTime());
// then put it in the database, something like this:
//resultSet.updateDate("myDateTimeField", sqldate);
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy' 'HH:mm:ss");
System.out.println("date in controller"+ df.format(d));
Try using format instead of parse.
Date date = new Date();
String DATE_FORMAT = "MM/dd/yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
System.out.println("Today is " + sdf.format(date) );

Categories

Resources