application.yml
configuration:
jackson:
date-format: yyyy-MM-dd
timestamp-format:yyyy-MM-dd HH:mm:ss
serialization:
write-dates-as-timestamps: false
Bean properties:
@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
)
For using timestamp
, I found the @Temporal(TemporalType.Date
or Timestamp
) which is mapping by DB Type
.
Date and timestamp format is stored correctly like yyyy-MM-dd
or yyyy-MM-dd HH:mm:ss.sss
RestController
class:
@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.
Is there any solution to receive yyyy-MM-dd
and yyyy-MM-dd HH:mm:ss
in Controller
?
I am wondering about application.yml
settings as well. 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. Simple example:prints:
As you can see I parsed your example date
Wed Mar 20 09:00:00 KST 2019
and print usingtoString
method and formatted with two different timezones. 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.You can configure format using
com.fasterxml.jackson.annotation.JsonFormat
annotation. SinceJava 8
is available we should usejava.time.*
classes for time related properties. ExamplePOJO
class could like this:To make it work we need to register
JavaTimeModule
module:If you can change your
Bean
properties tojava.time.*
classes just propagate these dates fromController
toDB
. In other case see this question: Converting between java.time.LocalDateTime and java.util.Date.