Java.text.parseexception: Unparseable Date: "2014-06-04" (at Offset 5)
I want to parse the date into a desired format but i am receiving an exception every time. i know it is easy to implement but i am facing some problem don't know where exactly.: Ex
Solution 1:
You have no time part in your string: and the Month has only two character replace
newSimpleDateFormat("yyyy-MMM-dd HH:mm:ss",Locale.ENGLISH);
with
newSimpleDateFormat("yyyy-MM-dd",Locale.ENGLISH);
Solution 2:
// Try this way,hope this will help you to solve your problem....publicStringconvertDateStringFormat(String strDate, String fromFormat, String toFormat){
try{
SimpleDateFormat sdf = newSimpleDateFormat(fromFormat);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
SimpleDateFormat dateFormat2 = newSimpleDateFormat(toFormat.trim());
return dateFormat2.format(sdf.parse(strDate));
}catch (Exception e) {
e.printStackTrace();
return"";
}
}
Solution 3:
Maybe you need to tackle different input formats Then catch the currently unmanaged format exception (just a sample):
privateStringgetconvertdate(String date) {
System.out.println(date.length());
DateFormat inputFormat = null;
if(date.length() == 20)
inputFormat = newSimpleDateFormat("yyyy-MMM-dd HH:mm:ss",Locale.ENGLISH);
if(date.length() == 10)
inputFormat = newSimpleDateFormat("yyyy-MM-dd",Locale.ENGLISH) ;
if(null == inputFormat)
return"Format invalid";
inputFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
DateFormat outputFormat = newSimpleDateFormat("dd-MMM-yyyy",Locale.ENGLISH);
Date parsed = null;
try {
parsed = inputFormat.parse(date);
} catch (ParseException e) {
return"Input Date invalid";
}
String outputText = outputFormat.format(parsed);
return outputText;
}
Solution 4:
In my case, I was using ::
SimpleDateFormattodaySdf=newSimpleDateFormat("dd MMM yyyy", Locale.ENGLISH);
changed it to
SimpleDateFormattodaySdf=newSimpleDateFormat("dd MM yyyy", Locale.ENGLISH);
and it worked.. the extra M was the culprit !!
Post a Comment for "Java.text.parseexception: Unparseable Date: "2014-06-04" (at Offset 5)"