Skip to content

Commit e6fc0bd

Browse files
mprevisicmaibin
authored andcommitted
[BAEL-2533] Formatting JSON Dates in Spring Boot (eugenp#6313)
* [BAEL-2533] formatting json date output in spring boot * [BAEL-2533] removed unused import
1 parent ecabba4 commit e6fc0bd

11 files changed

Lines changed: 561 additions & 0 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.baeldung.jsondateformat;
2+
3+
import com.fasterxml.jackson.annotation.JsonFormat;
4+
5+
import java.time.LocalDate;
6+
import java.time.LocalDateTime;
7+
8+
public class Contact {
9+
10+
private String name;
11+
private String address;
12+
private String phone;
13+
14+
@JsonFormat(pattern="yyyy-MM-dd")
15+
private LocalDate birthday;
16+
17+
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
18+
private LocalDateTime lastUpdate;
19+
20+
public String getName() {
21+
return name;
22+
}
23+
24+
public void setName(String name) {
25+
this.name = name;
26+
}
27+
28+
public String getAddress() {
29+
return address;
30+
}
31+
32+
public void setAddress(String address) {
33+
this.address = address;
34+
}
35+
36+
public String getPhone() {
37+
return phone;
38+
}
39+
40+
public void setPhone(String phone) {
41+
this.phone = phone;
42+
}
43+
44+
public LocalDate getBirthday() {
45+
return birthday;
46+
}
47+
48+
public void setBirthday(LocalDate birthday) {
49+
this.birthday = birthday;
50+
}
51+
52+
public LocalDateTime getLastUpdate() {
53+
return lastUpdate;
54+
}
55+
56+
public void setLastUpdate(LocalDateTime lastUpdate) {
57+
this.lastUpdate = lastUpdate;
58+
}
59+
60+
public Contact() {
61+
}
62+
63+
public Contact(String name, String address, String phone, LocalDate birthday, LocalDateTime lastUpdate) {
64+
this.name = name;
65+
this.address = address;
66+
this.phone = phone;
67+
this.birthday = birthday;
68+
this.lastUpdate = lastUpdate;
69+
}
70+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.jsondateformat;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class ContactApp {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(ContactApp.class, args);
11+
}
12+
13+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.jsondateformat;
2+
3+
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
4+
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
5+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
6+
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Configuration;
9+
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
10+
11+
import java.time.format.DateTimeFormatter;
12+
13+
@Configuration
14+
public class ContactAppConfig {
15+
16+
private static final String dateFormat = "yyyy-MM-dd";
17+
18+
private static final String dateTimeFormat = "yyyy-MM-dd HH:mm:ss";
19+
20+
@Bean
21+
@ConditionalOnProperty(value = "spring.jackson.date-format", matchIfMissing = true, havingValue = "none")
22+
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
23+
return new Jackson2ObjectMapperBuilderCustomizer() {
24+
@Override
25+
public void customize(Jackson2ObjectMapperBuilder builder) {
26+
builder.simpleDateFormat(dateTimeFormat);
27+
builder.serializers(new LocalDateSerializer(DateTimeFormatter.ofPattern(dateFormat)));
28+
builder.serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat)));
29+
}
30+
};
31+
}
32+
33+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.baeldung.jsondateformat;
2+
3+
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
import org.springframework.web.bind.annotation.RestController;
6+
7+
import java.time.LocalDate;
8+
import java.time.LocalDateTime;
9+
import java.util.ArrayList;
10+
import java.util.Date;
11+
import java.util.List;
12+
13+
@RestController
14+
@RequestMapping(value = "/contacts")
15+
public class ContactController {
16+
17+
@GetMapping
18+
public List<Contact> getContacts() {
19+
List<Contact> contacts = new ArrayList<>();
20+
21+
Contact contact1 = new Contact("John Doe", "123 Sesame Street", "123-456-789", LocalDate.now(), LocalDateTime.now());
22+
Contact contact2 = new Contact("John Doe 2", "124 Sesame Street", "123-456-789", LocalDate.now(), LocalDateTime.now());
23+
Contact contact3 = new Contact("John Doe 3", "125 Sesame Street", "123-456-789", LocalDate.now(), LocalDateTime.now());
24+
25+
contacts.add(contact1);
26+
contacts.add(contact2);
27+
contacts.add(contact3);
28+
29+
return contacts;
30+
}
31+
32+
@GetMapping("/javaUtilDate")
33+
public List<ContactWithJavaUtilDate> getContactsWithJavaUtilDate() {
34+
List<ContactWithJavaUtilDate> contacts = new ArrayList<>();
35+
36+
ContactWithJavaUtilDate contact1 = new ContactWithJavaUtilDate("John Doe", "123 Sesame Street", "123-456-789", new Date(), new Date());
37+
ContactWithJavaUtilDate contact2 = new ContactWithJavaUtilDate("John Doe 2", "124 Sesame Street", "123-456-789", new Date(), new Date());
38+
ContactWithJavaUtilDate contact3 = new ContactWithJavaUtilDate("John Doe 3", "125 Sesame Street", "123-456-789", new Date(), new Date());
39+
40+
contacts.add(contact1);
41+
contacts.add(contact2);
42+
contacts.add(contact3);
43+
44+
return contacts;
45+
}
46+
47+
@GetMapping("/plain")
48+
public List<PlainContact> getPlainContacts() {
49+
List<PlainContact> contacts = new ArrayList<>();
50+
51+
PlainContact contact1 = new PlainContact("John Doe", "123 Sesame Street", "123-456-789", LocalDate.now(), LocalDateTime.now());
52+
PlainContact contact2 = new PlainContact("John Doe 2", "124 Sesame Street", "123-456-789", LocalDate.now(), LocalDateTime.now());
53+
PlainContact contact3 = new PlainContact("John Doe 3", "125 Sesame Street", "123-456-789", LocalDate.now(), LocalDateTime.now());
54+
55+
contacts.add(contact1);
56+
contacts.add(contact2);
57+
contacts.add(contact3);
58+
59+
return contacts;
60+
}
61+
62+
@GetMapping("/plainWithJavaUtilDate")
63+
public List<PlainContactWithJavaUtilDate> getPlainContactsWithJavaUtilDate() {
64+
List<PlainContactWithJavaUtilDate> contacts = new ArrayList<>();
65+
66+
PlainContactWithJavaUtilDate contact1 = new PlainContactWithJavaUtilDate("John Doe", "123 Sesame Street", "123-456-789", new Date(), new Date());
67+
PlainContactWithJavaUtilDate contact2 = new PlainContactWithJavaUtilDate("John Doe 2", "124 Sesame Street", "123-456-789", new Date(), new Date());
68+
PlainContactWithJavaUtilDate contact3 = new PlainContactWithJavaUtilDate("John Doe 3", "125 Sesame Street", "123-456-789", new Date(), new Date());
69+
70+
contacts.add(contact1);
71+
contacts.add(contact2);
72+
contacts.add(contact3);
73+
74+
return contacts;
75+
}
76+
77+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.baeldung.jsondateformat;
2+
3+
import com.fasterxml.jackson.annotation.JsonFormat;
4+
5+
import java.util.Date;
6+
7+
public class ContactWithJavaUtilDate {
8+
9+
private String name;
10+
private String address;
11+
private String phone;
12+
13+
@JsonFormat(pattern="yyyy-MM-dd")
14+
private Date birthday;
15+
16+
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
17+
private Date lastUpdate;
18+
19+
public String getName() {
20+
return name;
21+
}
22+
23+
public void setName(String name) {
24+
this.name = name;
25+
}
26+
27+
public String getAddress() {
28+
return address;
29+
}
30+
31+
public void setAddress(String address) {
32+
this.address = address;
33+
}
34+
35+
public String getPhone() {
36+
return phone;
37+
}
38+
39+
public void setPhone(String phone) {
40+
this.phone = phone;
41+
}
42+
43+
public Date getBirthday() {
44+
return birthday;
45+
}
46+
47+
public void setBirthday(Date birthday) {
48+
this.birthday = birthday;
49+
}
50+
51+
public Date getLastUpdate() {
52+
return lastUpdate;
53+
}
54+
55+
public void setLastUpdate(Date lastUpdate) {
56+
this.lastUpdate = lastUpdate;
57+
}
58+
59+
public ContactWithJavaUtilDate() {
60+
}
61+
62+
public ContactWithJavaUtilDate(String name, String address, String phone, Date birthday, Date lastUpdate) {
63+
this.name = name;
64+
this.address = address;
65+
this.phone = phone;
66+
this.birthday = birthday;
67+
this.lastUpdate = lastUpdate;
68+
}
69+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.baeldung.jsondateformat;
2+
3+
import java.time.LocalDate;
4+
import java.time.LocalDateTime;
5+
6+
public class PlainContact {
7+
8+
private String name;
9+
private String address;
10+
private String phone;
11+
12+
private LocalDate birthday;
13+
14+
private LocalDateTime lastUpdate;
15+
16+
public String getName() {
17+
return name;
18+
}
19+
20+
public void setName(String name) {
21+
this.name = name;
22+
}
23+
24+
public String getAddress() {
25+
return address;
26+
}
27+
28+
public void setAddress(String address) {
29+
this.address = address;
30+
}
31+
32+
public String getPhone() {
33+
return phone;
34+
}
35+
36+
public void setPhone(String phone) {
37+
this.phone = phone;
38+
}
39+
40+
public LocalDate getBirthday() {
41+
return birthday;
42+
}
43+
44+
public void setBirthday(LocalDate birthday) {
45+
this.birthday = birthday;
46+
}
47+
48+
public LocalDateTime getLastUpdate() {
49+
return lastUpdate;
50+
}
51+
52+
public void setLastUpdate(LocalDateTime lastUpdate) {
53+
this.lastUpdate = lastUpdate;
54+
}
55+
56+
public PlainContact() {
57+
}
58+
59+
public PlainContact(String name, String address, String phone, LocalDate birthday, LocalDateTime lastUpdate) {
60+
this.name = name;
61+
this.address = address;
62+
this.phone = phone;
63+
this.birthday = birthday;
64+
this.lastUpdate = lastUpdate;
65+
}
66+
}

0 commit comments

Comments
 (0)