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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24030767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 09:47:51  来源:igfitidea点击:

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

javaandroidandroid-date

提问by Anchit Mittal

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

方法输入:2014-06-04

Expected Output: 06-Jun-2014

预期产出:2014 年 6 月 6 日

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

我遵循了一些参考文献。来自 Stackoverflow.com,但他的问题仍然存在。请帮忙。

采纳答案by Jens

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);

回答by Abs

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;
}

回答by Haresh Chhelana

// 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 "";
       }
}

回答by AndroidGuy

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 !!

它奏效了.. 额外的 M 是罪魁祸首!