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

net.yadaframework.web.YadaJsonDateTimeShortSerializer Maven / Gradle / Ivy

There is a newer version: 0.7.7.R4
Show newest version
package net.yadaframework.web;
import java.io.IOException;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import org.apache.commons.lang3.time.FastDateFormat;
import org.springframework.context.i18n.LocaleContextHolder;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

/**
 * Date serializer that produces a date in the FastDateFormat.SHORT format using the thread's timezone and locale
 *
 */
public class YadaJsonDateTimeShortSerializer extends JsonSerializer {
	
	@Override
	public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
		if (date!=null) {
			Locale locale = LocaleContextHolder.getLocale();
			TimeZone timeZone = LocaleContextHolder.getTimeZone();
			FastDateFormat fastDateFormat = FastDateFormat.getDateTimeInstance(FastDateFormat.SHORT, FastDateFormat.SHORT, timeZone, locale);
			jsonGenerator.writeString(fastDateFormat.format(date));
		} else {
			jsonGenerator.writeString("");
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy