-1

I've a date object that returns a calendar.getTime() date.

However, this date is formatted like this:

("dd/MM/time/yyyy")
  1. i want to get rid of that time and still keep the date as a date since I need the DATE type for the rest of my application to run correctly. I want it to show as

    ("dd/MM/yyy")

  2. how can I do this? the other answers I have seen all require you to change it to a string. is it possible to put it to as string and then back to a date with no time?

ATRS
  • 247
  • 4
  • 15
app_maker
  • 29
  • 8
  • http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html – duggu Jan 30 '15 at 06:35
  • http://developer.android.com/reference/java/text/SimpleDateFormat.html – duggu Jan 30 '15 at 06:36
  • 1
    If you can possibly use Joda Time, I'd suggest using a `LocalDate` - that way it's really obvious what the value is meant to represent. – Jon Skeet Jan 30 '15 at 06:44

2 Answers2

1

A dateobject always has a timepart. So you can not have a date without timepart. you only can set the timepart to zero:

    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);

If you only what to convert it to a String you can use SimpleDateFormat:

    DateFormat df = new SimpleDateFormat("dd/MM/yyy");
    String s = df.format(calendar.getTime());
Jens
  • 67,715
  • 15
  • 98
  • 113
  • Thank you. That was helpful. I managed to use it this way. I just realized i only need to display is as a string and program with it as a date since no one really looks at the DATE version anyways. Thank you for answering my question so clearly. – app_maker Jan 30 '15 at 06:50
0

You can do this with the DateFormat class, like this:

Calendar cal = new GregorianCalendar(2015, 01, 30);
DateFormat df = android.text.format.DateFormat.getDateFormat(this); 
String date = df.format(calendar.getTime());

getDateFormat(Context)

this return formatters defined by the system's locale. (It's god if you want support multi date formats (eg us, de)

Manu Zi
  • 2,300
  • 6
  • 33
  • 67