co.centauri.json.JSONDateSerialize.java Source code

Java tutorial

Introduction

Here is the source code for co.centauri.json.JSONDateSerialize.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package co.centauri.json;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

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

public class JSONDateSerialize extends JsonSerializer<Date> {

    private static final String DATE_FORMAT = "dd/MM/yyyy";

    @Override
    public void serialize(Date t, JsonGenerator jg, SerializerProvider sp)
            throws IOException, JsonProcessingException {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
        String formattedDate = dateFormat.format(t);
        jg.writeString(formattedDate);
    }
}