JSON Date Format
When we pass formatted date through JSON the conversion won't to same as we required. So we need to use @JsonSerialize annotation to get out customized date format. Below example shows with and without @JsonSerialize annotation and its output. Here we used Spring rest to demonstrate with response type as JSON.
Employee model class which contains date field.
Employee.java
Spring rest controller class.
MyController.java
OUTPUT:
IN above example we see that DOJ date conversion to default date format is wrong. So we need to set @JsonSerialize annotation to get our date conversion correctly as like below example.
JSON custom date formatting class
CustomDateSerializer.java
Employee model class with @JsonSerialize annotation. Just add below given annotation before "doj" setter method.
Employee.java
OUTPUT:
Employee model class which contains date field.
Employee.java
import java.util.Date; import com.fasterxml.jackson.databind.annotation.JsonSerialize; public class Employee { private int id; private String empName; private Date doj; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getEmpName() { return empName; } public void setEmpName(String empName) { this.empName = empName; } public Date getDoj() { return doj; } public void setDoj(Date doj) { this.doj = doj; } }
Spring rest controller class.
MyController.java
import java.util.Date; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.models.Employee; @RestController public class MyController { @RequestMapping(value="/getemp", method=RequestMethod.GET, headers="Accept=application/json") public ResponseEntity<Employee> getEmpDetails(){ Employee emp = new Employee(); emp.setId(1); emp.setEmpName("Steve"); emp.setDoj(new Date()); return new ResponseEntity<Employee>(emp, HttpStatus.OK); } }
OUTPUT:
IN above example we see that DOJ date conversion to default date format is wrong. So we need to set @JsonSerialize annotation to get our date conversion correctly as like below example.
JSON custom date formatting class
CustomDateSerializer.java
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 CustomDateSerializer extends JsonSerializer<Date> { @Override public void serialize(Date date, JsonGenerator jGen, SerializerProvider arg2) throws IOException, JsonProcessingException { SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss"); String fDate = sdf.format(date); jGen.writeString(fDate); } }
Employee model class with @JsonSerialize annotation. Just add below given annotation before "doj" setter method.
Employee.java
@JsonSerialize(using = CustomDateSerializer.class) public void setDoj(Date doj) { this.doj = doj; }
OUTPUT: