search

Home  >  Q&A  >  body text

Java - Can milliseconds be removed from the displayed time?

The time in the database is of DateTime type
Getting the time

public Timestamp getDatetime() {
        return new Timestamp(System.currentTimeMillis());
    }

Can you remove it? It’s so ugly

仅有的幸福仅有的幸福2774 days ago1001

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-06-23 09:16:23

    It seems that when Timestamp is converted to String, the toString method of Timestamp is directly called.
    The easiest way is to find the display place and change String str = timestamp.toString() similar code to

    String str = timestamp.toString();
    str = str.substring(0, str.length() - 4);

    reply
    0
  • 代言

    代言2017-06-23 09:16:23

    Use SimpleDateFormat to convert Timestamp to String type.

    public Timestamp getDatetime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Timestamp now = new Timestamp(System.currentTimeMillis());
        String str = df.format(now);
        
        return str;
    }

    reply
    0
  • Cancelreply