Java – get only the date of the timestamp

This is my following function. I'm passing a timestamp. I just need to return the date from the timestamp, not the hour and second Using the following code,

private String toDate(long timestamp) {
        Date date = new Date (timestamp * 1000);
        return DateFormat.getInstance().format(date).toString();
}

This is the output I got

11/4/01 11:27 PM

But I just need such a date

2001-11-04

Any suggestions?

Solution

Use simpledateformat instead:

private String toDate(long timestamp) {
    Date date = new Date (timestamp * 1000);
    return new SimpleDateFormat("yyyy-MM-dd").format(date);
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>