How to trim off the zeros in the millisecond when last digit is zero?

92 Views Asked by At

I am having a sample program . The program prints a java object into json string. The following is the code snippet.

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

import io.vertx.core.json.jackson.DatabindCodec;

public class Main {

    public static void main(String[] args) throws JsonProcessingException {

        ObjectMapper objectMapper = DatabindCodec.mapper();
        JavaTimeModule module = new JavaTimeModule();

        module.addSerializer(OffsetDateTime.class, new JsonSerializer<OffsetDateTime>() {
            @Override
            public void serialize(OffsetDateTime offsetDateTime, JsonGenerator jsonGenerator, SerializerProvider arg2)
                    throws IOException, JsonProcessingException {
                jsonGenerator.writeString(
                        DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").format(offsetDateTime));

            }
        });

        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        objectMapper.setDateFormat(dateFormatter);
        objectMapper.registerModule(module);

        TestOffsetDateTime s1 = new TestOffsetDateTime();

        System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(s1));

    }

    public static class TestOffsetDateTime {

        private OffsetDateTime offSetField;
        
        public TestOffsetDateTime() {
            this.offSetField = OffsetDateTime.now();
        }

        public OffsetDateTime getOffSetField() {
            return offSetField;
        }

        public void setOffSetField(OffsetDateTime offSetField) {
            this.offSetField = offSetField;
        }
    }

}

when i run the program i get the following output

{
  "offSetField" : "2024-01-29T17:23:30.290Z"
}

However I would expect the it to "2024-01-29T17:23:30.29Z" as the last digit in the millisecond is zero. what changes could I make to date formatter pattern so that if the last digit in the millisecond is zero i should truncate it ? currently if the last digit of the milliseconds is not zero it is fine .

Acceptable cases:
2024-01-29T17:23:30.821Z
2024-01-29T17:23:30.29Z

Non acceptable cases:
2024-01-29T17:23:31.820Z => expected 2024-01-29T17:23:31.82Z
2024-01-29T17:23:31.290Z =>  expected 2024-01-29T17:23:31.29Z 

thank you

0

There are 0 best solutions below