3

everyone!

I know SimpleDateFormat is not thread safe,we shouldn't use a single instance in a multithreading environment!

ObjectMapper is thread safe, which is good.

now i'm wondering is it safe to use SimpleDateFormat in ObjectMapper! here's my custom ObjectMapper:

public class MyObjectMapper extends ObjectMapper {

    public MyObjectMapper() {
        configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
    }

}

and I'm using it in springmvc's configuration.

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
    jsonConverter.setObjectMapper(new MyObjectMapper());
    converters.add(jsonConverter);
}

If yes, how ObjectMapper did it!

If no, what's DateFormat should i use!

Thanks!

Luke
  • 47
  • 7
  • I'm using it with ObjectMapper! I think maybe ObjectMapper have some synchronizing mechanism to keep it safe! – Luke Nov 12 '15 at 13:14

1 Answers1

6

See comments of @StaxMan on Should I declare Jackson's ObjectMapper as a static field?

Baseline:

DateFormat is cloned within Jackson, so it's safe to use SimpleDateFormat.

Community
  • 1
  • 1
Jan
  • 13,319
  • 3
  • 27
  • 51