jacksontimezonejava-timezoneddatetime

How to remove ZoneId?


Thank you always for your help.

I got a problem in my project.

I want to delete Zoned Id and leave only time.

How to remove zoned Id?

It is written using kotlin in Spring Boot Framework.

data class AvailableScheduleRequest (
    val address: String,
    val date: LocalDate,
    val people: Long,
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'", timezone = "Asia/Seoul")
    val hour: ZonedDateTime
)
@RestController
class Controller(
    val newBookingElasticSearchService: NewBookingElasticSearchService
) {
    @RequestMapping("/")
    @ResponseBody
    fun get(@RequestBody param: Map<String, Any>): List<AvailableRestaurant> {
        val json = JacksonUtils.om.writeValueAsString(param)
        val availableScheduleRequest =
            JacksonUtils.om.readValue(json, AvailableScheduleRequest::class.java)

        println(availableScheduleRequest.hour)
    }
}
object JacksonUtils {
    val om = ObjectMapper()

    init {
        val module = SimpleModule()
        module.addDeserializer(String::class.java, JsonRawValueDeserializer.INSTANCE)
        om.registerModule(JavaTimeModule())
        om.registerModule(KotlinModule())
        om.registerModule(module)
        om.dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX")
        om.setTimeZone(TimeZone.getDefault())
        om.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
        om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
        om.configure(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT, true)
        om.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true)
        om.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true)
    }

This code outputs:

2019-10-02T07:00+09:00[Asia/Seoul]

But I wanna get this:

2019-10-02T07:00+09:00

How to get this?

Thank you for your efforts all the time!!


Solution

  • Instead of ZonedDateTime, you could use OffsetDateTime.