Java.text.ParseException: Opaque date: convert string mm / dd / yyyy to date
when I convert a string object format mm/dd/yyyy
in Date
, it gives me
java.text.ParseException: Unparseable date: "09/17/2014"
I am trying to do it like this:
String date= "09/17/2014";
DateFormat df = new SimpleDateFormat();
Date journeyDate= (java.sql.Date) df.parse(date);
+3
Subham tripathi
source
to share
4 answers
There are several potential problems here:
- You don't specify the format
- You do not specify the locale
- You do not specify a time zone
- You are trying to cast the return value (which will be a reference
java.util.Date
) tojava.sql.Date
- this will throw an error
You need something like:
DateFormat df = new SimpleDateFormat("MM/dd/yyyy", Locale.US);
df.setTimeZone(...); // Whatever time zone you want to use
Date journeyDate = new java.sql.Date(df.parse(text).getTime());
+12
Jon Skeet
source
to share
DateFormat df = new SimpleDateFormat("MM/dd/yyyy",Locale.ENGLISH);
Date journeyDate = df.parse(date); // gives you java.util.Date
If you want java.sql.Date then
java.sql.Date sqlDate = new java.sql.Date(journeyDate.getTime());
+1
SparkOn
source
to share
I have the following exception:
java.text.ParseException: Continuous date
System.out.println("inside the XMLGregorianCalendar");
sb = (String) map.get(fieldname);
System.out.println("THis is XMLGReogoriaaaaan calendar"+ sb);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
Date journeyDate = new java.sql.Date(df.parse(sb).getTime());
System.out.println("this"+journeyDate);
0
syed
source
to share
You mixed m
and m
.
m
indicates minutes and m
within a month.
Below is an example of a working format.
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
String dateInString = "07/06/2013";
Date date = formatter.parse(dateInString);
System.out.println(formatter.format(date));
0
Hrushi
source
to share