Home > database >  Convert timestamp GMT 1 to europe/brussels timezone
Convert timestamp GMT 1 to europe/brussels timezone

Time:08-25

I'm receiving this timestamp in a json body:

{
  "timestamp": "2019-03-27 10:04:01.446937 01"
}

And I would like to convert this timestamp into europe/brussels timezone.

I'm using com.fasterxml.jackson.core so I'm wondering if this is possible with annotations in this class for example.

public class MyClass {

    @JsonFormat(...)
    Date timestamp;
}

If not how can this be achieved using plain java code?

CodePudding user response:

ISO 8601

If possible, educate the publisher of your data about using standard ISO 8601 formats when exchanging date-time values textually. That means a T in the middle instead of a SPACE character. And for the offset, use hours with minutes, delimited by a COLON character, rather than abbreviating.

DateTimeFormatter

If not possible, define a formatting pattern to match your input.

String input = "2019-03-27 10:04:01.446937 01" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-dd HH:mm:ss.SSSSSSx" ) ; 

java.time.OffsetDateTime

Parse as an OffsetDateTime.

OffsetDateTime odt = OffsetDateTime.parse( input , f ) ;

odt.toString(): 2019-03-27T10:04:01.446937 01:00

ZonedDateTime

Apply your desired time zone.

ZoneId z = ZoneId.of( "Europe/Brussels" ) ;
ZonedDateTime zdt = odt.atZoneSameInstant( z ) ; 

See this code run at Ideone.com.

zdt.toString(): 2019-03-27T10:04:01.446937 01:00[Europe/Brussels]

In this particular case, Brussels time is already using an offset of one hour ahead of UTC. So no change for the time-of-day from our original.

CodePudding user response:

Extracts the "timestamp" field in the JSON object as String. Then use the java.text.SimpleDateFormat with the correct formatter to parse the string into java.util.Date object. Then change to the required timezone before displayed it back into JSON string.

CodePudding user response:

Here's the plain Java solution. I guess there's more elegant way for this.

static {
    // make sure your JVM's timezone won't be "combined" with the input's timezone.
    TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
}

public class DateHandler extends StdDeserializer<Date> {
    public DateHandler() {
        this(null);
    }

    public DateHandler(Class<?> clazz) {
        super(clazz);
    }

    @Override
    public Date deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException {
        String date = jsonParser.getText();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSSZ"); // or DateTimeFormatter.ISO_OFFSET_DATE_TIME
            return sdf.parse(date "00");
        } catch (Exception e) {
            log.error(e, e);
            return null; // TODO throw new JsonParseException..
        }
    }
}

and instead of your @JsonFormat(...) do @JsonDeserialize(using = DateHandler.class)

Now new ObjectMapper().readValue("{ \"timestamp\": \"2019-03-27 10:04:01.446937 01\"}", MyClass.class) returns a date with 0100 offset as required.

  • Related