This question already has answers here:
How to convert java.util.Date to java.sql.Date?
(17 answers)
Closed 7 years ago.
I am trying to send a date value from my java program into an oracle sql database. But I keep getting the error: java.text.parseexception: unparseable date.
I set the date format as:
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd", Locale.ENGLISH);
java.sql.Date date = (java.sql.Date)df.parse(dob_text.getText());
I have set my database with the same date format.
And try to send the date through a prepared statement like so:
ps.setDate(3, date);
I am entering a date 1994-09-09. That's the correct date format for the one I declared right? Is there something wrong with my java formation code? Has anyone else had this problem? Any help would be much appreciated
This should work, I corrected 2 errors :
First of all, the format should have been yyyy-MM-dd since that's the format of your input.
Then, you can not implicitely cast java.util.Date to java.sql.Date, you need to use the java.sql.Date constructor and java.util.Date#getTime(). See here
Solution
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
java.sql.Date SQLDate = new java.sql.Date(df.parse(dob_text.getText()).getTime());
Change your format to yyyy-MM-dd.
I just wrote this program and it works fine. Make sure you aren't getting some other error now.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;
public class DateFormatDemo
{
public static void main(String[] args) throws ParseException
{
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
System.out.println(df.parse("1994-09-09"));
}
}
Your first try probably threw the exception you mentioned because of the wrong format as Josh pointed out. After correcting this the next problem occurs:
A java.sql.Date is NOT a java.util.Date. So you cannot just typecast the outcome of the df.parse, which is a java.util.Date.
And third: If you provide the pattern to the SimpleDateFormat you can omit the locale.
Following code runs without errors:
String input = "1994-09-09";
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date = df.parse(input);
System.out.println( date);
java.sql.Date sqlDate = new java.sql.Date( date.getTime() );
System.out.println( sqlDate);
Related
This question already has answers here:
Why is Java's SimpleDateFormat not thread-safe? [duplicate]
(9 answers)
Closed 4 years ago.
I am converting Date to string format in yyyy-MM-dd HH:mm:ss format to save in sqlite database
below is object declared for simple date format
public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Sometime it prepends pair of zeros in date as you can see in below image
Example Wrong date returns as as below
2018-08-25 02:32:0000
2018-08-25 02:32:0049
2018-0008-25 02:32:50
2018-08-24 0023:32:50
2018-08-0024 23:32:50
I have created custom funtion to correct this wrong date. But I want to know exact cause of this issue.
below is the code
public static String getCurrentDateTime() {
Date d = new Date();
String datetime = sdf.format(d);
if (datetime.length() > 19) {
datetime = correctDate(datetime);
}
return datetime;
}
I'm sure that if you don't use that static instance of SimpleDateFormat you will have no problem:
public static String getCurrentDateTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = new Date();
String datetime = sdf.format(d);
return datetime;
}
See these links:
Why is Java's SimpleDateFormat not thread-safe?
"Java DateFormat is not threadsafe" what does this leads to?
SimpleDateFormat is not thread safe that's why you should check if in your calling code there is no issue with that.
This question already has answers here:
Conversion from 12 hours time to 24 hours time in java
(17 answers)
Closed 6 years ago.
I am storing time in the database in this format: "hh:mm:ss" (example- 09:30:00) and then retrieving and trying to show it to users in this format: "hh:mm AM/PM" (example- 09:30 AM).
I'm using below code for converting it:
DateFormat currentTime = new SimpleDateFormat("hh:mm a");
String startTimeInSF = currentTime.format(startTime);
String endTimeInSF = currentTime.format(endTime);
where startTime and endTime is in hh:mm:ss format, but the above code is producing this error: java.lang.IllegalArgumentException: Bad class: class java.lang.String.
Please let me know how can I successfully convert the time from hh:mm:ss to hh:mm AM/PM?
I think you should parse your "hh:mm:ss" time into a Date Object, then use formatter to format it to "hh:mm a".
Like this :
DateFormat format1 = new SimpleDateFormat("hh:mm:ss");
try {
Date date = format1.parse("01:11:22");
SimpleDateFormat format2 = new SimpleDateFormat("hh:mm a");
String result = format2.format(date);
return result;
} catch (ParseException e) {
e.printStackTrace();
}
I believe you're looking for the parse(String source) method. The format() methods take in a Date object and output a String representation of the object. the parse methods take in a String object and converts it to a Date object.
To do what you want, you'll need to have a DateFormat with hh:mm:ss, convert the database String to a Date using parse, and then use your existing DateFormat and use format on the Date object to get the output String to display to your user.
http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html
You need to change your code something like this, format function will not work directly on String object that is root cause of your exception.
DateFormat inputFormatter1 = new SimpleDateFormat"HH:mm:ss");
Date date1 = inputFormatter1.parse("22:10:11");
DateFormat outputFormatter1 = new SimpleDateFormat("hh:mm a");
String output1 = outputFormatter1.format(date1); //
Out will be 10:10 pm
This question already has answers here:
How can I get Date in MM/DD/YY format from Timestamp
(7 answers)
Closed 9 years ago.
How to convert java.util.Date directly into desired Date format ?
I have tried to convert the date into string first and then again into date but it still takes date format as Mar 10,2011, i want this format as 10-03-2011
Its simple :) and SimpleDateFormat can help you out.
But if you have datestring then yes you need to parse the datestring to date and re-format it again to desired format(dd-MM-yyyy).
OP failed to mention that he is displaying these in a JTable. You need these things for your date column in your JTable.
A CellRenderer : This will display the date in its string format (Use Simpledateformat)
A TableModel : This holds the internal representation
Now when you sort the JTable, the column will sort on the internal data representation.
Just pass in your date object directy to this method and you'll get the date in desired string format
new SimpleDateFormat("dd-MM-yyyy").format(date);
You can do like this
SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy");
String date = format.format(Date.parse(objectDate.toString()));
Create a SimpleDateFormat with the date format dd-MM-yyyy:
Date today = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
String date = dateFormat.format(today);
System.out.println("Today in dd-MM-yyyy format : " + date);
try this code.
String dateFormat = "dd-MM-yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, new Locale("en_US"));
String tDate = sdf.format(date);
System.out.println(tDate);
You can try this
Date date = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).parse(string);
for 10-03-2011 your format should be "dd-MM-yyyy"
This question already has answers here:
Java date format conversion - getting wrong month
(8 answers)
Closed 3 years ago.
I have a string form of Date. I have to change it to Sql Date. so for that i have used the following code.
String startDate="01-02-2013";
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-mm-yyyy");
java.util.Date date = sdf1.parse(startDate);
java.sql.Date sqlStartDate = new java.sql.Date(date.getTime());
when i used the above code and run that. I got the following output.
2013-01-01.
Here Month is not converted correctly.
Please tell me where is the problem and provide sample code to get correct result?
mm is minutes. You want MM for months:
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy");
Don't feel bad - this exact mistake comes up a lot.
mm stands for "minutes". Use MM instead:
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy");
You need to use MM as mm stands for minutes.
There are two ways of producing month pattern.
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy"); //outputs month in numeric way, 2013-02-01
SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MMM-yyyy"); // Outputs months as follows, 2013-Feb-01
Full coding snippet:
String startDate="01-Feb-2013"; // Input String
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy"); // New Pattern
java.util.Date date = sdf1.parse(startDate); // Returns a Date format object with the pattern
java.sql.Date sqlStartDate = new java.sql.Date(date.getTime());
System.out.println(sqlStartDate); // Outputs : 2013-02-01
That is the simple way of converting string into util date and sql date
String startDate="12-31-2014";
SimpleDateFormat sdf1 = new SimpleDateFormat("MM-dd-yyyy");
java.util.Date date = sdf1.parse(startDate);
java.sql.Date sqlStartDate = new java.sql.Date(date.getTime());
While using the date formats, you may want to keep in mind to always use MM for months and mm for minutes. That should resolve your problem.
This question already has answers here:
How to convert ISO 8601 date (string) to Date?
(3 answers)
Closed 5 years ago.
How can I convert a String object to a Date object?
I think I need to do something like this:
Date d=(some conversion ) "String "
Any help would be greatly appreciated.
SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
Date date = dateFormat.parse("1.1.2001");
For details refer to: SimpleDateFormat documentation
Date-to-String conversion is a relatively complex parsing operation, not something you can do with a simple cast as you are trying.
You'll have to use a DateFormat. It can be as simple as:
Date d = DateFormat.getDateInstance().parse("09/10/2009");
But this changes the expected date format depending on the locale settings of the machine it's running on. If you have a specific date format, you can use SimpleDateFormat:
Date d = new SimpleDateFormat("d MMM yyyy HH:mm").parse("4 Jul 2001 12:08");
Note that the parse method will always expect one specific format, and will not try to guess what could be meant if it receives a different format.
See Sun's Java tutorial and the class SimpleDateFormat
Use a SimpleDateFormat with a format string, which matches your actual format:
SimpleDateFormat sdf =
new SimpleDateFormat("yyyy-MM-dd");
Date d = sdf.parse("2009-10-09");
java.text.SimpleDateFormat that extends java.text.DateFormat abstract class.
DateFormat MYDate = new SimpleDateFormat("dd/MM/yyyy");
Date today = MYDate.parse("09/10/2009");
you should parse the string with the SimpleDateFormat class
use
Date date = DateFormat.getInstance.parse( dateString );
You can convert String object into Date object using this method. and this Java code is tested and running component in my environment.
public static Date parseStringAsDate(String dateStr, String format) throws ParseException
{
if(null==dateStr || "".equals(dateStr))
throw new IllegalArgumentException("dateStr must not be null or empty");
DateFormat df = new SimpleDateFormat(format);
return df.parse(dateStr);
}
dateStr = "17/05/2017"
format= "dd/MM/yyyy"