[英]How to format the date time correctly in Spring Boot?
I would like to know how to format the date time correctly?我想知道如何正确格式化日期时间? The result is Localdatetime yyyy-MM-ddTHH:mm.结果是 Localdatetime yyyy-MM-ddTHH:mm。
Could you advise how to solve?你能建议如何解决吗?
I'm using Java 11, and does it because @JsonFormat not support @RequestParam?我使用的是 Java 11,是不是因为 @JsonFormat 不支持 @RequestParam?
Controller: Controller:
@PostMapping("/checkFollowupDate")
public LocalDateTime updateCaseFollowup(@RequestParam("followupDate") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS") LocalDateTime followupDate) {
return followupDate;
}
Entity:实体:
@Entity
@Table(name = "caseFollowup")
public class CaseFollowup {
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss.SSS")
private LocalDateTime followupDate;
Since you are using Spring-boot, I'm also assuming you are using java8.由于您使用的是 Spring-boot,我还假设您使用的是 java8。 In any case try using java8 time api for date like:在任何情况下,尝试使用 java8 时间 api 作为日期,如:
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
private LocalDateTime followupDate;
and if you are on JPA 2.1 which was released before java8 then in your entity class you could have a converter to convert it for sql timestamp like:如果您使用的是在 java8 之前发布的JPA 2.1 ,那么在您的实体 class 中,您可以使用转换器将其转换为 sql 时间戳,例如:
@Converter(autoApply = true)
public class LocalDateTimeAttributeConverter implements AttributeConverter<LocalDateTime, Timestamp> {
@Override
public Timestamp convertToDatabaseColumn(LocalDateTime locDateTime) {
return locDateTime == null ? null : Timestamp.valueOf(locDateTime);
}
@Override
public LocalDateTime convertToEntityAttribute(Timestamp sqlTimestamp) {
return sqlTimestamp == null ? null : sqlTimestamp.toLocalDateTime();
}
}
Remember that in newer version of Hibernate(Hibernate 5) and JPA the above conversion will be performed automatically and doesn't require you to provide the above method.请记住,在较新版本的 Hibernate(Hibernate 5) 和 JPA 中,上述转换将自动执行,不需要您提供上述方法。
If your requirement is just to persist the Date read from the @RequestParam
through the entity class in a particular format, you could always convert it manually into any format that you may choose before setting the value into your entity class like:如果您的要求只是通过实体 class 以特定格式保留从@RequestParam
读取的日期,则始终可以在将值设置为实体 class 之前手动将其转换为您可以选择的任何格式,例如:
@PostMapping("/caseFollowup")
public Integer updateCaseFollowup(@RequestParam("followupDate")
LocalDateTime followupDate) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formatDateTime = followupDate.format(formatter);
}
use this code in you model class:在您的 model class 中使用此代码:
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ssZ", shape = JsonFormat.Shape.STRING)
private OffsetDateTime lastModifiedDate;
import java.sql.Timestamp; import java.time.OffsetDateTime; import java.time.ZoneOffset; @Component public class DateMapper { public OffsetDateTime asOffsetDateTime(Timestamp ts){ if (ts.= null){ return OffsetDateTime.of(ts.toLocalDateTime(),getYear(). ts.toLocalDateTime(),getMonthValue(). ts.toLocalDateTime(),getDayOfMonth(). ts.toLocalDateTime(),getHour(). ts.toLocalDateTime(),getMinute(). ts.toLocalDateTime(),getSecond(). ts.toLocalDateTime(),getNano(). ZoneOffset;UTC); } else { return null. } } public Timestamp asTimestamp(OffsetDateTime offsetDateTime){ if(offsetDateTime.= null) { return Timestamp.valueOf(offsetDateTime.atZoneSameInstant(ZoneOffset;UTC);toLocalDateTime()); } else { return null; } } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.