How to insert PAST dates into MySQL as Datetime? - java

if(sqlData.get(i) != null){
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
java.sql.Timestamp myDate = new java.sql.Timestamp(dateFormat.parse(sqlData.get(i)).getTime());
preparedStatement.setTimestamp(i+1, myDate);
I'm not sure what i'm doign wrong and i keep geting "java.text.ParseException: Unparseable date:" on the 3rd line.
These are dates that i have pulled out of a database, and are set in the past.
System.out.println(sqlData.get(i));
prints out : 2014-04-19 05:48:22

java.text.ParseException: Unparseable date
is thrown from dateFormat.parse( when the string passed to it is not in the same format as the specified pattern.
Also note that you have not posted the entire exception message (the entire line). It would have been
java.text.ParseException: Unparseable date:2014-04-19 05:48:22
In your case
2014-04-19 05:48:22 is not in the format "yyyy/MM/dd HH:mm:ss"
Your pattern string should be
yyyy-MM-dd HH:mm:ss Note the - instead of /

Here's an example of parsing a date and then creating a timestamp out of it. This was testing and is working, so if you run into any errors, it may have something to do with the data you have and not the code itself.
String dateString = "1999/10/24 10:24:99";
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date dateObj = dateFormat.parse(dateString);
Timestamp timeStamp = new Timestamp(dateObj.getTime());

Related

From yyyy-MM-dd to Timestamp

I need to parse this date to get results from database, i tried to make it by many ways, but I can't find any fine. As i read it should work.
//2016-01-04
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS");
Timestamp filterDateFromTs = new Timestamp ((dateFormat.parse(filterDateFrom)).getTime());
But I get
java.text.ParseException: Unparseable date: "2016-01-04".
You are trying to parse 2016-01-04 hence you should be using yyyy-MM-dd and not yyyy-MM-dd hh:mm:ss:SSS.
Try this instead:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Timestamp filterDateFromTs = new Timestamp ((dateFormat.parse(filterDateFrom)).getTime());

How to get the correct format of dateand time in mysql?

When I get the date and time in MySQL it retrieves it in this format:
2016-01-14 14:24:00.0
Where does the .0 come from and how do I get this format with Java:
2016-01-14 14:24:00
You can use 2 ways to do this.
Split the date string at '.'
String date = "2016-01-14 14:24:00.0";
String newDate = date.split("\\.")[0];
System.out.println(newDate);
Use SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date date = sdf.parse("2016-01-14 14:24:00.0");
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(date));
If you are getting date as string, you can use Format to get the format you need:
SELECT DATE_FORMAT(YourDateField, '%d-%m-%Y %T')
FROM YourTable
Have a look here for all possible formats
If you are getting your date as a java.sql.Timestamp, you can get the corresponding java.util.Date instance very easily and then format it to the desired string representation with a java.text.SimpleDateFormat class.

Solving Parse Date Exception

i have a string "12/3/2014 12:00:00 AM" and i want to cast this string in date which have format like this "yyyy-MM-dd HH:mm:ss"
#DatabaseField(columnName="out_date",dataType=DataType.DATE_STRING,format="yyyy-MM-dd HH:mm:ss")
private Date out_date;
above is the object to get value from date string
SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
outlet.setOut_date(formater.parse(json.getString("outletDate")));
after i using like the error occur java.text.ParseException: Unparseable date: ... (at offset 2) how to solve that problem thank in advance
Based on this answer, you need something like this. First convert your string in its present format to a date object, then reformat the date object.
String date = "12/3/2014 12:00:00 AM";
SimpleDateFormat sdf = new SimpleDateFormat("mm/dd/yyyy HH:MM:SS a");
Date testDate = null;
try {
testDate = sdf.parse(date);
}catch(Exception ex){
ex.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String newFormat = formatter.format(testDate);
System.out.println(".....Date..."+newFormat);
Conversion steps :
Create date object from your date format.
Target date format provide to SimpleDateFormat class constructor.
Parse date using SimpleDateFormat.
The code provided convert yyyy-MM-dd HH:mm:ss to dd/mm/yyyy HH:mm:ss format.
try {
SimpleDateFormat sdf1 = new SimpleDateFormat("dd/mm/yyyy HH:MM:SS a");
Date date = sdf1.parse("12/3/2014 12:00:00 AM");
String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
System.out.println(format);
}catch(Exception e){
e.printStackTrace();
}
Error : java.text.ParseException: Unparseable date: ... (at offset 2) indicates that you are using wrong date format to parse your sting.

Get time from entire date

I have to get time from entire date
e.g. time=11:00:00 from date 2012-09-01 11:00:00.0
I tried following snippet but getting error Error : Unparseable date: "2012-9-1.13.30. 0. 0"
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = inputFormat.parse(iResultSet1.getString(i));
DateFormat outputFormat = new SimpleDateFormat("hh:mm:ss");
String outputString = outputFormat.format(date);
Edit: Now I am getting only date instead I want only time
if (iResultSet1.getDate(i) != null) {
Date date = iResultSet1.getDate(i);
System.out.println("date-->" + date);
// Format date into output format
DateFormat outputFormat = new SimpleDateFormat(
"HH:mm:ss");
String outputString = outputFormat.format(date);
// System.out.println("date1-->"+date1);
I wil suggest, in this case rather doing parsing and manipulation in java change your SQL to format and return only date as
Example
SELECT TIME_FORMAT(NOW(), '%H:%i:%s');
You are probably retrieving your date from the database (iResultSet1.getString(i)) and the problem is that you're getting wrong format, i.e. 2012-9-1.13.30. 0. 0. Either change the date format in the database or use:
Date date = iResultSet1.getDate(i);
instead of
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = inputFormat.parse(iResultSet1.getString(i));

Java change date format from custom date to MM dd yyyy

I am trying to convert a String value that is stored in a database,for example "2012-01-20", to be in the format January 20, 2012.
I have seen some examples, but they are using Date which works with SimpleDateFormat.
As an example here is one way I tried but the "try" always fails and the result is null
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Date convertedDate=null;
try {
convertedDate = df.parse(datePlayed);
} catch(ParseException e){
e.printStackTrace();
}
In short, you're not using the right format for parsing. You need to use two DateFormat instances; one for parsing and one for formatting.
DateFormat parser = new SimpleDateFormat("yyyy-MM-dd");
DateFormat formatter = new SimpleDateFormat("MMM dd, yyyy");
Date convertedDate = parser.parse(datePlayed);
String output = formatter.format(convertedDate);
Date da = new Date();
SimpleDateFormat ft = new SimpleDateFormat("E, dd/MM/yyyy !");
System.out.println("Update : " + ft.format(da));
You can change your date style do you want at: E, dd/MM/yyyy !
Good luck !
If the Date is read from a Database, then store it as a java.sql.Date. You can then use SimpleDateFormat on it, maybe after converting to java.util.Date. From the ResultSet object, you can extract Dates.
If what you meant is that you are given a date in text that was extracted from a DB by someone else and you are stuck with it. Try using:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
df.setLenient(true);
convertedDate = df.parse(datePlayed.trim() );
Also try displaying the text you are parsing before you parse to make sure the datePlayed value is what you expect.
With parseInt, an extra space before or after the data will cause an error, so calling trim() removes extra spaces.

Categories

Resources