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
You can use SimpleDateFormatter
. Please see the following code.
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
Date now = date.getTime();
System.out.println(formatter.format(now));
Truncate to Seconds (no milliseconds), return a new Date:
public Date truncToSec(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.MILLISECOND, 0);
Date newDate = c.getTime();
return newDate;
}