-2

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

Sandeep Rao
  • 1,749
  • 6
  • 23
  • 41

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,397
  • 2
  • 16
  • 15
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
  • 17
  • 15