简体   繁体   English

在实体构造函数中将日期从时间戳转换为可读

[英]Converting date from timestamp to human readable in entity constructor

Currently, the format of the Date requestDate variable stored looks like: 2017-02-17 00:00:00.0 . 当前,存储的Date requestDate变量的格式如下: 2017-02-17 00:00:00.0 I want to convert this into, for example: Friday, February 17, 2017 . 我想将其转换为例如: Friday, February 17, 2017 I would like to do the conversion here in my entity and return it so that when it's displayed it is more human readable. 我想在这里在我的实体中进行转换并返回它,以便在显示时更易读。 This will likely happen in the constructor, at this line: this.setRequestDate(doDateConversion(requestDate)); 这很可能在构造函数中的这一行发生: this.setRequestDate(doDateConversion(requestDate)); . How can I make this conversion? 我该如何进行转换?

My Request entity: 我的请求实体:

@Entity
@Table(name = "Request")
public class RequestDO implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="request_id")
    private Long id;
    private Date requestDate;
    private String description;
    private RequestStatus status;
    /*private Boolean read;*/
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="user_id", nullable = false)
    private Users users;

    public RequestDO() {}

    public RequestDO(Users user, Date requestDate) {
        this.setUsers(user);
        this.setRequestDate(requestDate);
    }

    @Override
    public String toString() {
        return String.format(
                "RequestDO[id=%d, inital='%s', requestDate='%s']",
                getId()
                , getUsers().getInitialName()
                , getRequestDate());
    }


    public Date getRequestDate() {
        return requestDate;
    }

    public void setRequestDate(Date requestDate) {
        this.requestDate = requestDate;
    }
}

You can use SimpleDateFormat to convert your Date to a readable String of your choice. 您可以使用SimpleDateFormat将Date转换为您选择的可读字符串。

The time format String for your example is EEEE, MMMM, dd, yyyy . 您的示例的时间格式字符串为EEEE, MMMM, dd, yyyy You have to create a new SimpleDateFormat object and format your date to a String. 您必须创建一个新的SimpleDateFormat对象,并将日期格式设置为字符串。 Examples... 例子...

But Spring provides some specials out of the box. 但是Spring提供了一些特别的功能。 For example you can use Jackson for date format: @JsonFormat(pattern="yyyy-MM-dd") more . 例如,您可以将Jackson用作日期格式: @JsonFormat(pattern="yyyy-MM-dd") more It is also possible to add a data format in application.properties file : spring.jackson.date-format 也可以在application.properties文件中添加数据格式: spring.jackson.date-format

Using SimpleDateFormat : 使用SimpleDateFormat

java.sql.Date date = new Date(System.currentTimeMillis());
System.out.println(new SimpleDateFormat("EEEE, MMMM dd, YYYY").format(date));

See this for more details. 请参阅了解更多详情。

I solved the problem by changing the dates as they are read in my controller, using SimpleDateFormat: 我通过使用SimpleDateFormat更改在控制器中读取的日期来解决该问题:

@RequestMapping(value = "/requests", method = RequestMethod.GET)
    public String getAllRequests(Model model, RequestModel requestModel) throws ParseException {
        List<RequestDO> requestDOArrayList = new ArrayList<RequestDO>();
        for (RequestDO requestDO : requestRepository.findAll()) {
            log.info(requestDO.toString());

            // Display all dates in Requests list in human-readable form
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(requestDO.getRequestDate().toString());
            log.info(String.valueOf(date));
            requestDO.setRequestDate(date);

            requestDOArrayList.add(requestDO);
        }
        model.addAttribute("requests", requestDOArrayList);
        log.info(requestDOArrayList.toString());
        return "requests";
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM