error code
public static void main(String[] args)throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
final LocalDateTime localDateTime = LocalDateTime.now();
final String s = objectMapper.writeValueAsString(localDateTime);
System.out.println(s);
}
Error
reason
The date was not formatted during conversion
Solution:
public static void main(String[] args)throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
// Date and time formatting
JavaTimeModule javaTimeModule = new JavaTimeModule();
// Set the serialization format of LocalDateTime
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
objectMapper.registerModule(javaTimeModule);
final LocalDateTime localDateTime = LocalDateTime.now();
final String s = objectMapper.writeValueAsString(localDateTime);
System.out.println(s);
}
Read More:
- [Solved] Conversion not supported for type java.time.LocalDateTime
- [Solved] error: taking address of temporary array av_make_error_string((char[AV_ERROR_MAX_STRING_SI…
- How to Solve error C2039: “to_ String “: not a member of” STD “
- How to parse JSON string in.Net [error reading job object from jsonreader. Current jsonreader item is not an obj]
- Oracle prompt text does not match format string
- How to Solve brew ERROR in `initialize‘: Version value must be a string; got a NilClass ()
- C++ Primer Program in VsCode error: no match for call to ‘(std::__cxx11::string…)
- How to Solve “Status bar could not find cached time string image. Rendering in-process.” in Xcode
- [Solved] The method getContextPath() from the type HttpServletRequest refers to the missing type String
- TypeError: unsupported format string passed to numpy.ndarray.__format__
- feign.FeignException: status 404 reading XXXClient#XXMethod(String)
- The request was rejected because the URL contained a potentially malicious String “;” [How to Solve]
- [Solved] java.lang.IllegalStateException: Cannot get a STRING value from a NUMERIC cell
- JSON parse error: Can not deserialize instance of java.lang.String out of START_OBJECT token
- Android Studio Error: String index out of range 0 [How to Solve]
- [Solved] ORA-21525: attribute number or (collection element at index) string violated its constraints
- frida.InvalidArgumentError: script(line *): SyntaxError: unexpected end of string
- MOTR compiling error: cannot call member function ‘void std::basic_string<_CharT, _Traits, _Alloc>::_R
- [Solved] Wwagger error: java.lang.NumberFormatException: For input string: ““
- [Solved] AS Warning: String literal in setText can not be translated. Use Android resources instead.