【问题标题】:i got the error java.text.ParseException: Unparseable date null我收到错误 java.text.ParseException: Unparseable date null
【发布时间】:2020-06-26 10:01:39
【问题描述】:

我希望日期格式为 yyyy-MM-dd。我的代码是:

       Calendar cal = Calendar.getInstance();
              SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd", Locale.ENGLISH);
              cal.setTime(sdf.parse(row.getCell(67).toString()));

单元格中的值为:06/02/2020 但是,我得到的错误是:

java.text.ParseException: Unparseable date: "null"
    at java.text.DateFormat.parse(Unknown Source) ~[na:1.8.0_251]

感谢您的帮助....

但是,我希望日期格式为 yyyy-MM-dd。

这是完整的代码:

if(row.getCell(67).equals("null"))
                  {

                      Calendar cal = Calendar.getInstance();
                      cal.set(Calendar.YEAR, 1988);
                      cal.set(Calendar.MONTH, Calendar.JUNE);
                      cal.set(Calendar.DAY_OF_MONTH, 1);
                      Date dateRepresentation = cal.getTime();
                      rfx.setRv_rc_date(dateRepresentation);
                  }
                  else
                  {

                      Calendar cal = Calendar.getInstance();
                      SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd", Locale.ENGLISH);
                      cal.setTime(sdf.parse(row.getCell(67).toString()));
                      System.out.println("******************************************************");
                      System.out.println(cal);
                      System.out.println("*******************************************************");

                     

                  }
                  

【问题讨论】:

  • 请检查您的数据并调试您的代码,对我来说这看起来好像row.getCell(67).toString() 返回null
  • 你的sdf中的“-”在哪里?
  • 另外,尽量避免java.util的过时日期时间API,改用java.time
  • 请再次查看我刚刚更新的答案
  • 我收到了这个错误:java.text.ParseException: Unparseable date: "30-janv.-2020"

标签: java date calendar


【解决方案1】:

我收到此错误:java.text.ParseException: Unparseable date: "30-janv.-2020"

在 Google 中搜索此日期时,我发现它位于 French 区域设置中,而您使用的是 Locale.ENGLISH

如下图,改用Locale.FRENCH来摆脱错误

SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd", Locale.FRENCH);

演示:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Main {
    public static void main(String[] args) throws ParseException {
        // Input format
        SimpleDateFormat inputFormat = new SimpleDateFormat("dd-MMM-yyyy", Locale.FRENCH);
        Date date = inputFormat.parse("30-janv.-2020");

        // Output format
        SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(outputFormat.format(date));
    }
}

输出:

2020-01-30

但是,我建议您停止使用过时的日期时间 API,并切换到modern date-time API,如下所示:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy", Locale.FRENCH);
        LocalDate date = LocalDate.parse("30-janv.-2020", formatter);
        System.out.println(date);
    }
}

输出:

2020-01-30

请注意,我没有为此输出定义任何格式化程序,因为LocalDate#toString 已经返回了您所需格式的字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-16
    • 1970-01-01
    • 2022-12-09
    • 1970-01-01
    • 1970-01-01
    • 2018-02-27
    相关资源
    最近更新 更多