How to remove milliseconds from Date Object format in Java
java, java.util.date
Solution
Basic answer is, you can't. The value returned by `Date#toString` is a representation of the `Date` object and it carries no concept of format other then what it uses internally for the `toString` method.
Generally this shouldn't be used for display purpose (except for rare occasions)
Instead you should be using some kind of `DateFormat`
For example...
Date date = new Date();
System.out.println(date);
System.out.println(DateFormat.getDateTimeInstance().format(date));
System.out.println(DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(date));
System.out.println(DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM).format(date));
System.out.println(DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG).format(date));
Will output something like...
Thu Jan 30 16:29:31 EST 2014
30/01/2014 4:29:31 PM
30/01/14 4:29 PM
30/01/2014 4:29:31 PM
30 January 2014 4:29:31 PM
If you get really stuck, you can customise it further by using a `SimpleDateFormat`, but I would avoid this if you can, as not everybody uses the same date/time formatting ;)
Problem
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 want to remove the milliseconds part. I checked a question related to my question... How to remove sub seconds part of Date object I've tried the given answer ``` long time = date.getTime(); date.setTime((time / 1000) * 1000); ``` but I've got my result Date format as `2014-01-24 17:33:47.0`. How can I remove that `0` from my Date format???