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

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.

Related

Unparseable date: "2018-08-02T14:24:40.040353"

I am trying to parse a DateTime from C# to Java. Here is what comes as string from the java call "2018-08-02T14:24:40.040353". When I try to parse it, I get the following error Unparseable date: "2018-08-02T14:24:40.040353"
Here is the method for parsing the date
public static String dateFormater(String dateFromJSON, String
expectedFormat, String oldFormat) {
SimpleDateFormat dateFormat = new SimpleDateFormat(oldFormat);
Date date = null;
String convertedDate = null;
try {
date = dateFormat.parse(dateFromJSON);
SimpleDateFormat simpleDateFormat = new
SimpleDateFormat(expectedFormat);
convertedDate = simpleDateFormat.format(date);
} catch (Exception e) {
e.printStackTrace();
}
return convertedDate;
}
Your date look like a ISO_LOCAL_DATE_TIME, If you are using Java8 you can use java.time API and use the default format of LocalDateTime like this :
String dateString = "2018-08-02T14:24:40.040353";
LocalDateTime ldt = LocalDateTime.parse(dateString);
ldt.toString():
2018-08-02T14:24:40.040353

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.

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

date parsing in java

I have written following program to convert date string to sql date object to store in db2 but the ouput shown is 2013-01-02 instead of 2013-02-02. can anyone explain why??
String string = "02/02/2013";
Date date = null;
try {
date = new SimpleDateFormat("dd/mm/yyyy", Locale.ENGLISH).parse(string);
java.sql.Date newDate=new java.sql.Date(date.getTime());
System.out.println(newDate);
} catch (ParseException e) {
e.printStackTrace();
}
For month you should use MM instead of mm. mm is used for minutes in hour: -
date = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH)
your format should be
"dd/MM/yyyy"
note that mm is for minutes whereas MM is for Month
Check the Doc

How to find difference in Joda time

I am trying to use the Interval class of Joda Time but I am unable to use its constructor. It does not take the format.
I am trying to extract two DateTime from mysql DB in the format yyyy-MM-dd HH:mm:ss and it is retrieved in String format. I am trying to convert it into date format but the interval class is unable to take date formats. Please help what should I use???
String text = "2011-10-02 18:48:05";
String text2 = "2011-10-02 18:50:05";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date olddate=sdf.parse(text);
Date newdate=sdf.parse(text2);
System.out.println(olddate);
System.out.println(newdate);
// Interval interval = new Interval(olddate, newdate);
} catch (ParseException ex) {
JOptionPane.showMessageDialog(null, "Error at Timestamp subtract Format function Dategenerator" + ex.getMessage());
}
Don't use java.util.Date, but Joda Time's org.joda.time.DateTime.
For parsing use Joda Time's org.joda.time.format.DateTimeFormat.
That should work:
String text = "2011-10-02 18:48:05";
String text2 = "2011-10-02 18:50:05";
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
DateTime oldDate = formatter.parseDateTime(text);
DateTime newDate = formatter.parseDateTime(text2);
System.out.println(oldDate);
System.out.println(newDate);
Interval interval = new Interval(oldDate, newDate);
System.out.println(interval);
I was able to get the minutes difference using this code in joda time:
String text = "2011-10-02 15:48:05";
String text2 = "2011-10-02 18:52:10";
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
DateTime oldDate = formatter.parseDateTime(text);
DateTime newDate = formatter.parseDateTime(text2);
System.out.println(oldDate);
System.out.println(newDate);
Interval interval = new Interval(oldDate, newDate);
System.out.println(interval.toDuration().toPeriod().getHours()*60+ interval.toDuration().toPeriod().getMinutes());

Categories

Resources