[英]Spring Boot Jackson date and timestamp Format
application.yml
configuration: application.yml
配置:
jackson:
date-format: yyyy-MM-dd
timestamp-format:yyyy-MM-dd HH:mm:ss
serialization:
write-dates-as-timestamps: false
Bean properties: Bean属性:
@Entity
@Column(nullable = false)
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Temporal(TemporalType.DATE)
private Date date_created;
@Column(nullable = false)
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Temporal(TemporalType.TIMESTAMP)
private Date reg_date;
I set all Date
fields as java.util.Date
type which receive date in format yyyy-MM-dd
and timestamp type( yyyy-MM-dd HH:mm:ss
) according to request param style( yyyy-MM-dd
or yyyy-MM-dd HH:mm:ss
) 我根据请求参数样式(
yyyy-MM-dd
或yyyy-MM-dd HH:mm:ss
)将所有Date
字段设置为java.util.Date
类型,以yyyy-MM-dd
格式接收日期,并以timestamp类型( yyyy-MM-dd HH:mm:ss
)接收日期yyyy-MM-dd HH:mm:ss
)
For using timestamp
, I found the @Temporal( TemporalType.Date
or Timestamp
) which is mapping by DB Type
. 对于使用
timestamp
,我发现了@Temporal( TemporalType.Date
或Timestamp
),它通过DB Type
映射。
Date and timestamp format is stored correctly like yyyy-MM-dd
or yyyy-MM-dd HH:mm:ss.sss
日期和时间戳格式可以正确存储,例如
yyyy-MM-dd
或yyyy-MM-dd HH:mm:ss.sss
RestController
class: RestController
类:
@PostMapping("/")
public ResponseEntity<Object> create(@RequestBody CreateVO createVO, HttpServletRequest request) {
System.out.println("planned_date> "+createVO.getDate_planned_start());
System.out.println("regdate> "+createVO.getReg_date());
}
Are set to: 设置为:
planned_date> Wed Mar 20 09:00:00 KST 2019 // Date Result
regdate> Mon Oct 01 16:45:00 KST 2012 //Timestamp Result
However, I receive in RestController Date
in different format than i expected. 但是,我收到的RestController
Date
格式与我预期的格式不同。
Is there any solution to receive yyyy-MM-dd
and yyyy-MM-dd HH:mm:ss
in Controller
? 有什么解决方案可以在
Controller
接收yyyy-MM-dd
和yyyy-MM-dd HH:mm:ss
吗?
I am wondering about application.yml
settings as well. 我也想知道
application.yml
设置。 Because I am not sure how to set timestamp-format. 因为我不确定如何设置时间戳格式。
First of all Date.toString
method generates misleading output and we should not rely on it. 首先,
Date.toString
方法会产生误导性的输出,我们不应该依赖它。 Simple example: 简单的例子:
SimpleDateFormat dateToStringFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", new Locale("us"));
Date parsed = dateToStringFormat.parse("Wed Mar 20 09:00:00 KST 2019");
System.out.println("Default toString: " + parsed);
dateToStringFormat.setTimeZone(TimeZone.getTimeZone("Asia/Seoul"));
System.out.println("With 'Asia/Seoul' TZ: " + dateToStringFormat.format(parsed));
dateToStringFormat.setTimeZone(TimeZone.getTimeZone("Chile/Continental"));
System.out.println("With 'Chile/Continental' TZ: " + dateToStringFormat.format(parsed));
prints: 打印:
Default toString: Wed Mar 20 01:00:00 CET 2019
With 'Asia/Seoul' TZ: Wed Mar 20 09:00:00 +0900 2019
With 'Chile/Continental' TZ: Tue Mar 19 21:00:00 -0300 2019
As you can see I parsed your example date Wed Mar 20 09:00:00 KST 2019
and print using toString
method and formatted with two different timezones. 如您所见,我解析了示例日期为
Wed Mar 20 09:00:00 KST 2019
日Wed Mar 20 09:00:00 KST 2019
并使用toString
方法进行打印并使用两个不同的时区进行了格式化。 So, everyone sees date combined with his timezone. 因此,每个人都看到日期和他的时区。 Read more about:
进一步了解:
We can not define date patters in configuration like you proposed. 我们无法像您建议的那样在配置中定义日期模式。 See available
Jackson
configuration options here . 在此处查看可用的
Jackson
配置选项 。
You can configure format using com.fasterxml.jackson.annotation.JsonFormat
annotation. 您可以使用
com.fasterxml.jackson.annotation.JsonFormat
批注配置格式。 Since Java 8
is available we should use java.time.*
classes for time related properties. 由于
Java 8
可用,因此我们应该将java.time.*
类用于与时间相关的属性。 Example POJO
class could like this: 示例
POJO
类可能是这样的:
import com.fasterxml.jackson.annotation.JsonFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.MonthDay;
public class RequestPayload {
@JsonFormat(pattern = "MM/dd")
private MonthDay md;
@JsonFormat(pattern = "yyyy-MM-dd")
private LocalDate date;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime dateTime;
// getters, setters, toString
}
To make it work we need to register JavaTimeModule
module: 为了使其工作,我们需要注册
JavaTimeModule
模块:
@Bean
public Jackson2ObjectMapperBuilder objectMapperBuilder() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.modules(new JavaTimeModule());
return builder;
}
If you can change your Bean
properties to java.time.*
classes just propagate these dates from Controller
to DB
. 如果可以将
Bean
属性更改为java.time.*
类,只需将这些日期从Controller
传播到DB
。 In other case see this question: Converting between java.time.LocalDateTime and java.util.Date . 在其他情况下,请参见以下问题: 在java.time.LocalDateTime和java.util.Date之间进行转换 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.