0

I am working on spring-boot application to retrieve data from MySQL server.

I have a table with a column corresponding to the created_at timestamp. The field value is stored as timestamp.

While retrieving I want to show the timestamp in a particular date format as well as the timestamp for two different calculations.

I am using Spring Boot - CrudRepository with the Hibernate @Entity classes

public class Event {
    @Column(name="created_at")
    public String created_at;

    @Column(name="created_at")
    public String createdMonth; /***??? I want this 
      field to carry say date in month with three letters preceded by day 
      Eg: '22 May'. **/
}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • Use Custom JSONDeserializer [link] (https://stackoverflow.com/questions/5591967/how-to-deserialize-js-date-using-jackson) – sathees May 23 '17 at 16:39
  • Satheesh, I want to refer a single column in table and show in different formats in @Entity class as given in the snippet. – vijayakanth p madhavan May 23 '17 at 16:59
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](https://meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer May 26 '17 at 14:16

2 Answers2

0

You write serializer with jsonserialization with which you can format the string as required. Following link will convert date to required format, you reuse the code to change it as string to string formating. Link

sathees
  • 193
  • 2
  • 3
  • 10
0

You have to use @JsonProperty and @JsonFormat. But you cannot have two objects refer to the same column.

@JsonFormat(locale = "us", shape = JsonFormat.Shape.STRING, pattern = "MM/dd/yyyy", timezone = "America/Chicago")
@JsonProperty("customDate")
private string getCustomDate(){
    return createdAt.toString();
}
Abdullah Khan
  • 12,010
  • 6
  • 65
  • 78
zeagord
  • 2,257
  • 3
  • 17
  • 24