Search code examples
androidandroid-layoutdate-formattingandroid-datepickerandroid-date

How to format date in android environment


I want to display date in month and date format(ex:May 26).But when I am trying to use it was showing cannot resolve method.

String dateStr = DateFormat.format("MMM dd", cal.getTime().getTime()).toString();

Solution

  • You can use SimpleDateFormat.

    SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd", Locale.US);
    Date date = new Date(cal.getTimeInMillis());
    String dateStr = dateFormat.format(date);