How to remove milliseconds from Date Object format in Java

前端 未结 8 945
夕颜
夕颜 2020-12-15 21:56

Since the java.util.Date object stores Date as 2014-01-24 17:33:47.214, but I want the Date format as 2014-01-24 17:33:47. I w

8条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-15 22:45

    Just for the record, the accepted answer given at the post you linked works:

    public static void main(String[] args) {
           SimpleDateFormat df = new SimpleDateFormat("S");
           Date d = new Date();
           System.out.println(df.format(d));
           Calendar c = Calendar.getInstance();
           c.set(Calendar.MILLISECOND, 0);
           d.setTime(c.getTimeInMillis());
           System.out.println(df.format(d));
    
    }
    

提交回复
热议问题