All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.useful_solutions.tosamara_sdk.api.deserializer.ZonedDateTimeDeserializer Maven / Gradle / Ivy

There is a newer version: 1.5
Show newest version
package com.github.useful_solutions.tosamara_sdk.api.deserializer;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class ZonedDateTimeDeserializer extends JsonDeserializer {

    @Override
    public ZonedDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
        String text = jsonParser.getText();
        if (text.indexOf('.') != -1) {
            String value = text.substring(0, text.indexOf('.'));// remove millis
            LocalDateTime localDateTime = LocalDateTime.parse(value, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            return ZonedDateTime.of(localDateTime, ZoneId.of("Europe/Samara"));
        } else {
            return null;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy