简体   繁体   中英

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 . I want to convert this into, for example: 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)); . 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.

The time format String for your example is EEEE, MMMM, dd, yyyy . You have to create a new SimpleDateFormat object and format your date to a String. Examples...

But Spring provides some specials out of the box. For example you can use Jackson for date format: @JsonFormat(pattern="yyyy-MM-dd") more . It is also possible to add a data format in application.properties file : spring.jackson.date-format

Using 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:

@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";
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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