【问题标题】:java.text.ParseException: Unparseable date: "2014-06-04" (at offset 5)java.text.ParseException:无法解析的日期:“2014-06-04”(偏移量 5)
【发布时间】:2014-07-24 17:02:00
【问题描述】:

我想将日期解析为所需的格式,但我每次都收到异常。 我知道这很容易实现,但我遇到了一些不知道具体在哪里的问题。:

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

以下是我的代码:

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

方法输入:2014-06-04

预计产出:2014 年 6 月 6 日

我关注了一些 Ref。来自 Stackoverflow.com,但问题仍然存在。 请帮忙。

【问题讨论】:

    标签: java android android-date


    【解决方案1】:

    就我而言,我使用的是 ::

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

    改成

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

    它起作用了.. 额外的 M 是罪魁祸首!

    【讨论】:

      【解决方案2】:

      您的字符串中没有时间部分: 而月份只有两个字符 替换

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

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

      【讨论】:

        【解决方案3】:
        // 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 "";
               }
        }
        

        【讨论】:

        • 但是对于 MMM,它总是给我 'Jan' 一个月
        • 你能告诉我你传递给这个函数的 fromFormate 和 toFormate 值吗?
        • fromFormate = dd-mm-yyyy hh:mm a toFormate = MMM
        【解决方案4】:

        也许您需要处理不同的输入格式 然后捕获当前非托管格式异常(只是一个示例):

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

        【讨论】:

          猜你喜欢
          • 2017-11-19
          • 2016-09-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-07-05
          • 2013-05-28
          • 1970-01-01
          相关资源
          最近更新 更多