Skip to content

Commit

Permalink
Change the code according to the developer’s response
Browse files Browse the repository at this point in the history
  • Loading branch information
mklinkj committed Nov 21, 2023
1 parent c045d29 commit f2c3a58
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Jackson/jackson-local-datetime-example/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ javaVersion=17
projectEncoding=UTF-8


jacksonVersion=2.15.3
jacksonVersion=2.16.0


lombokPluginVersion=8.4
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package org.mklinkj.qna.jackson;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class App {

Expand All @@ -17,15 +16,24 @@ public static void main(String[] args) throws Exception {
}

public static String serialize(Member member) throws Exception {

// It’s a non-recommended method of specifying the date format,
// and this method does not apply the date format in version 2.16.0.
/*
ObjectMapper mapper =
new ObjectMapper()
.registerModule(
new JavaTimeModule()
.addSerializer(
LocalDateTime.class,
new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("MM/dd/yyyy"))));
*/
ObjectMapper mapper =
new ObjectMapper() //
.registerModule(new JavaTimeModule());

mapper
.configOverride(LocalDateTime.class) //
.setFormat(JsonFormat.Value.forPattern("MM/dd/yyyy"));
return mapper.writeValueAsString(member);
}
}

0 comments on commit f2c3a58

Please sign in to comment.