-2

How can i get a date instance without a time stamp in java? Ex: 2013-05-23( date instance)

Sandeep Rao
  • 1,711
  • 5
  • 21
  • 40

2 Answers2

2

Create a Calendar object to store the date, clear out the time fields, and then get the resulting date:

Calendar cal = Calendar.getInstance();
cal.setTime(dateYouWantToTruncate);
cal.clear(Calendar.HOUR);
cal.clear(Calendar.MINUTE);
cal.clear(Calendar.SECOND);
cal.clear(Calendar.MILLISECOND);
Date dateOnly = cal.getTime();
jalynn2
  • 6,145
  • 2
  • 15
  • 14
2

If you just want to print out the date use this:

    DateFormat.getDateInstance().format(new Date());

or

    DateFormat.getDateInstance(DateFormat.SHORT).format(new Date());
Marcin
  • 2,399
  • 3
  • 16
  • 15