使用 @Entity 从 mysql 表中以两种不同的格式显示时间戳创建



我正在开发弹簧引导应用程序以从MySQL服务器检索数据。

我有一个表,其中有一列对应于created_at时间戳。字段值存储为时间戳。

在检索时,我想以特定的日期格式显示时间戳以及两种不同计算的时间戳。

我正在使用 Spring Boot - CrudRepository 和 Hibernate @Entity

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'. **/
}

你必须使用@JsonProperty@JsonFormat。但是,不能让两个对象引用同一列。

@JsonFormat(locale = "us", shape = JsonFormat.Shape.STRING, pattern = "MM/dd/yyyy", timezone = "America/Chicago")
@JsonProperty("customDate")
private string getCustomDate(){
    return createdAt.toString();
}

您可以使用 json序列化编写序列化程序,您可以使用它根据需要设置字符串的格式。以下链接会将日期转换为所需的格式,您可以重复使用代码将其更改为字符串格式。链接

最新更新