java.text.ParseException: Unparseable date: “2014-06-04” (at offset 5)

假如想象 提交于 2019-11-30 02:48:28

问题


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.:

Exception: java.text.ParseException: Unparseable date: "2014-06-04" (at offset 5)

Following is my code:

private String getconvertdate(String date) {
    DateFormat inputFormat = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss",Locale.ENGLISH);
    inputFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    DateFormat outputFormat = new SimpleDateFormat("dd-MMM-yyyy",Locale.ENGLISH);
    Date parsed = null;
    try {
        parsed = inputFormat.parse(date);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    String outputText = outputFormat.format(parsed);
    return outputText;
}

Input to method: 2014-06-04

Expected Output: 06-Jun-2014

I have followed some Ref. from Stackoverflow.com, but still he problem persist. Please Help.


回答1:


You have no time part in your string: and the Month has only two character replace

new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss",Locale.ENGLISH);

with

new SimpleDateFormat("yyyy-MM-dd",Locale.ENGLISH);



回答2:


// Try this way,hope this will help you to solve your problem....

public String convertDateStringFormat(String strDate, String fromFormat, String toFormat){
       try{
          SimpleDateFormat sdf = new SimpleDateFormat(fromFormat);
          sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
          SimpleDateFormat dateFormat2 = new SimpleDateFormat(toFormat.trim());
          return dateFormat2.format(sdf.parse(strDate));
       }catch (Exception e) {
          e.printStackTrace();
          return "";
       }
}



回答3:


Maybe you need to tackle different input formats Then catch the currently unmanaged format exception (just a sample):

private String getconvertdate(String date) {
    System.out.println(date.length());
    DateFormat inputFormat = null;
            if(date.length() == 20)
                inputFormat = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss",Locale.ENGLISH);
    if(date.length() == 10)
        inputFormat = new SimpleDateFormat("yyyy-MM-dd",Locale.ENGLISH) ;

    if(null == inputFormat)
        return "Format invalid";

    inputFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    DateFormat outputFormat = new SimpleDateFormat("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;
}



回答4:


In my case, I was using ::

SimpleDateFormat todaySdf = new SimpleDateFormat("dd MMM yyyy", Locale.ENGLISH);

changed it to

SimpleDateFormat todaySdf = new SimpleDateFormat("dd MM yyyy", Locale.ENGLISH);

and it worked.. the extra M was the culprit !!



来源:https://stackoverflow.com/questions/24030767/java-text-parseexception-unparseable-date-2014-06-04-at-offset-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!