java.time.format.DateTimeParseException:无法解析文本“03/03/2020,03/03/2020",在索引 10 处找到未解析的文本 [英] java.time.format.DateTimeParseException: Text '03/03/2020,03/03/2020' could not be parsed, unparsed text found at index 10

查看:125
本文介绍了java.time.format.DateTimeParseException:无法解析文本“03/03/2020,03/03/2020",在索引 10 处找到未解析的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含NO"和Date"的表格.它将是动态形式.在 Spring Boot JPA 中进行批量更新时,我得到了java.time.format.DateTimeParseException: Text '03/03/2020,03/03/2020' could not be parsed, unparsed text found at index 10"异常

I have one form which contains "NO" and "Date". It will be Dynamic form. While doing a batch update in Spring Boot JPA i got the "java.time.format.DateTimeParseException: Text '03/03/2020,03/03/2020' could not be parsed, unparsed text found at index 10" exception

@RequestMapping(value="/abcpage", produces = { MediaType.APPLICATION_JSON_VALUE })
public String savePurchaseEntries(@ModelAttribute ABC abc,HttpSession session)
{

    System.out.println(abc);
    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
    LocalDate today = LocalDate.parse(abc.getDate(),dateTimeFormatter);
    abc.setLoclaDate(today);
    return "redirect:/home";
}

这里 ABC 是实体类,我可以获取 dd/mm/yyyy 格式的日期值并转换为 localdate 并设置到实体中.

Here ABC is the entity class and i can get the date value in dd/mm/yyyy format and convert into localdate and set into the entity.

ABC[NO=101,102,date=03/03/2020,03/03/2020]

对于一个条目,它工作正常,但在批处理时它抛出异常.

for one entry it is working fine but while batch it throw the exception.

@Entity
class ABC{ 
@column(name="NO") 
private String NO;
@Transient
private String date;
@Column(name="invdate")
private LocalDate loclaDate;
//getters & setters//tostring
}

推荐答案

我认为您的代码有问题,如果我正确理解您的输入,是您在 LocalDate 中传递逗号(,)分隔的日期字符串.解析(abc.getDate(),dateTimeFormatter).

I think problem with your code, if I understood your input correctly, is that you're passing comma(,) separated date strings in LocalDate.parse(abc.getDate(),dateTimeFormatter).

您每次都需要将单个日期传递给 parse() 方法,因为它尝试使用日期格式化程序格式来格式化提供的字符串输入,因此在03/03"的输入中遇到逗号(,)时会发生错误/2020,03/03/2020".

You need to pass a single date each time to parse() method as it tries to format the provided string input with that of date formatter format and hence error occurs when it encounters comma(,) in input of "03/03/2020,03/03/2020".

请参考官方文档.

以下是您可以尝试的内容:

Below is something you can try :

String[] inputDates = abc.getDate().split[","];
for (String date : inputDates) {
 // you can now use LocalDate.parse(abc.getDate(),dateTimeFormatter) here
 // write your logic here.
}

我希望这可以帮助您解决疑问,如果不能让我知道您的确切问题,我会尽力帮助您.

I hope this helps you get your doubt cleared, if not let me know your exact question and I will try to help you out.

EDIT 1 用于插入数据

下面是插入数据的方法,

Below is a way in which you can insert data,

创建一个表,该表将存储具有 3 列的日期 - id(P.K.)、abc_id(ABC 表的 F.K. ref.)、日期(在此处存储单个日期).现在假设上表的名称是 abc_date_map,然后在您的 ABC 实体中将此实体引用为 OneToMany.如下图,

Create a table which will store dates having 3 columns - id(P.K.), abc_id(F.K. ref. of ABC table), date (store single date here). Now let's say name of above table is abc_date_map, then reference this entity as OneToMany in your ABC entity. Like below,

@Entity
class ABC { 

 @Column(name="NO") 
 private String NO;

 @Transient
 private String date;

 @Column(name="abc_date_map_id")
 private List<AbcDateMap> abcDateMapEntityList;

 //getters & setters//tostring

}

你的 AbcDateMap 实体会像

And your AbcDateMap entity will be like

@Entity
public class AbcDateMap{

 @Column(name="abc_id")
 private Integer abcId;

 @Column(name="date")
 private LocalDate localDate;

 // getters setters

}

您的实体插入逻辑将类似于:

And your entity insertion logic will be like :

public Long insert(ABC abc) {

 abc.setNo(/*something*/);

 List<AbcDateMap> l = new ArrayList<>();

 AbcDateMap abcDate = new AbcDateMap();
 for (String date : abc.getDate().split(",")) {
  abcDate.setLocalDate(/*parse date here and store*/);
  abcDate.setAbcId(abc.getId());
 }
 abc.setAbcDateMapEntityList(l);
 repo.save(abc);

}

现在上面不是确切的代码,您必须对其进行润色,可能存在语法/语义错误.您还需要根据您的要求更改它.

Now above is not exact code, you will have to polish it out, might have syntax/semantics mistakes. Also you will need to change it according to your requirements.

希望对你有帮助.

祝你学习愉快!

这篇关于java.time.format.DateTimeParseException:无法解析文本“03/03/2020,03/03/2020",在索引 10 处找到未解析的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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