java.text.ParseException:无法解析的日期:"2014-06-04"(偏移量为5)

Anc*_*tal 10 java android android-date

我想将日期解析成所需的格式,但我每次都收到一个例外.我知道这很容易实现,但我面临一些问题,不知道究竟在哪里:

Exception: java.text.ParseException: Unparseable date: "2014-06-04" (at offset 5)
Run Code Online (Sandbox Code Playgroud)

以下是我的代码:

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;
}
Run Code Online (Sandbox Code Playgroud)

方法输入:2014-06-04

预期产出:2014年6月6日

我跟着一些Ref.来自Stackoverflow.com,但他仍然存在问题.请帮忙.

Jen*_*ens 17

你没有时间参与你的字符串:而且月只有两个字符替换

new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss",Locale.ENGLISH);
Run Code Online (Sandbox Code Playgroud)

new SimpleDateFormat("yyyy-MM-dd",Locale.ENGLISH);
Run Code Online (Sandbox Code Playgroud)


Har*_*ana 7

// 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 "";
       }
}
Run Code Online (Sandbox Code Playgroud)