Created
January 16, 2020 12:25
-
-
Save StefRe/89c33a7695453a0fedbd1e91096510eb to your computer and use it in GitHub Desktop.
Custom joda date format with Jackson serialization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.core.JsonProcessingException; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.databind.module.SimpleModule; | |
import com.fasterxml.jackson.datatype.joda.JodaModule; | |
import com.fasterxml.jackson.datatype.joda.cfg.JacksonJodaDateFormat; | |
import com.fasterxml.jackson.datatype.joda.ser.LocalDateSerializer; | |
import org.joda.time.format.DateTimeFormat; | |
import org.joda.time.LocalDate; | |
public class CustomJodaFormat { | |
public static void main(final String[] args) throws JsonProcessingException { | |
SimpleModule customJodaModule = new SimpleModule(); | |
customJodaModule.addSerializer(LocalDate.class, | |
new LocalDateSerializer(new JacksonJodaDateFormat(DateTimeFormat.forPattern("dd.MM.yyyy")), 1)); | |
ObjectMapper om = new ObjectMapper(); | |
om.registerModule(new JodaModule()); | |
om.registerModule(customJodaModule); | |
System.out.println(om.writerWithDefaultPrettyPrinter().writeValueAsString(new LocalDate(2020, 1, 16))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gives
"16.01.2020"
as opposed to the standard format"2020-01-16"
from simply