Java 将字符串日期映射到LocalDateTime时发生MessageConversionException
我有一个队列,它侦听一个主题,我的侦听器接收一个DTO。 我需要将字符串解析为Java 将字符串日期映射到LocalDateTime时发生MessageConversionException,java,spring-boot,localdatetime,Java,Spring Boot,Localdatetime,我有一个队列,它侦听一个主题,我的侦听器接收一个DTO。 我需要将字符串解析为LocalDateTime,但我遇到了这个错误 org.springframework.messaging.converter.MessageConversionException: Could not read JSON: Text '2020-06-18 11:12:46' could not be parsed at index 10 这是留言的详细信息 {"id":444, "details":{"TYPE"
LocalDateTime
,但我遇到了这个错误
org.springframework.messaging.converter.MessageConversionException: Could not read JSON: Text '2020-06-18 11:12:46' could not be parsed at index 10
这是留言的详细信息
{"id":444, "details":{"TYPE":[1]},"dateOccurred":"2020-06-18 11:12:46"}"]
下面是我如何在DTO中设置它的
public class PlanSubscriptionDto {
private Long id;
private Map<String, List<Long>> details;
private LocalDateTime dateOccurred;
public void setDateOccurred(String dateTime) {
this.dateOccurred = LocalDateTime.parse(dateTime, DateTimeFormatter.ISO_DATE_TIME);
//ive also tried this
//this.dateOccurred = LocalDateTime.parse(dateTime, DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG));
}
}
公共类计划订阅到{
私人长id;
私人地图详情;
私有LocalDateTime日期发生;
public void setdateoccurrent(字符串dateTime){
this.dateoccurrent=LocalDateTime.parse(dateTime,DateTimeFormatter.ISO_DATE_TIME);
//我也试过这个
//this.dateoccurrent=LocalDateTime.parse(dateTime,DateTimeFormatter.ofcalizeddatetime(FormatStyle.LONG));
}
}
谢谢你的帮助!
任何建议我们都会很好 使用格式模式字符串定义格式
public class PlanSubscriptionDto {
private static final DateTimeFormatter FORMATTER
= DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
private Long id;
private Map<String, List<Long>> details;
private LocalDateTime dateOccurred;
public void setDateOccurred(String dateTime) {
this.dateOccurred = LocalDateTime.parse(dateTime, FORMATTER);
}
}
或者根据格式化程序以及日期和时间之间的间隔,我们可以用更详细的方式定义格式化程序:
private static final DateTimeFormatter FORMATTER = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_LOCAL_DATE)
.appendLiteral(' ')
.append(DateTimeFormatter.ISO_LOCAL_TIME)
.toFormatter();
我们从额外的代码行中得到的是(1)更多地重用内置格式化程序(2)此格式化程序将接受没有秒的时间和只有几分之一秒的时间,因为DateTimeFormatter.ISO_LOCAL_time
确实如此
链接
我刚刚了解到这也是解析的一个选项:)
非常感谢你!--起初我认为我的地图有问题。
private static final DateTimeFormatter FORMATTER = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_LOCAL_DATE)
.appendLiteral(' ')
.append(DateTimeFormatter.ISO_LOCAL_TIME)
.toFormatter();
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "uuuu-MM-dd HH:mm:ss")
private LocalDateTime dateOccurred;