我收到错误java.text.ParseException:无法解析的日期为null [英] i got the error java.text.ParseException: Unparseable date null

查看:317
本文介绍了我收到错误java.text.ParseException:无法解析的日期为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want the date format as yyyy-MM-dd. My code is:

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

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

the value in cell is : 06/02/2020 But, I got the error as:

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

需要任何帮助...。

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

But, I want the date in date format as 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("*******************************************************");

                     

                  }
                  


推荐答案


我收到此错误:java.text.ParseException:不可解析的日期:" 30-janv.-2020"

i got this error : java.text.ParseException: Unparseable date: "30-janv.-2020"

在Google中搜索此日期时,我发现它位于法语语言环境中,而您使用的是 Locale.ENGLISH

On searching this date in Google I found that it is in the French locale whereas you have used Locale.ENGLISH.

如下所示,使用 Locale.FRENCH 来消除错误

As shown below, use Locale.FRENCH instead to get rid of the error

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并切换到现代日期-time API ,如下所示:

However, I recommend you stop using outdated date-time API and switch to the modern date-time API as shown below:

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 已经以您要求的格式返回了一个字符串。

Note that I haven't defined any formatter for this output because LocalDate#toString already returns a string in your required format.

这篇关于我收到错误java.text.ParseException:无法解析的日期为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆