0

This is my datepicker function.

public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
    Calendar c = Calendar.getInstance();
    c.set(Calendar.YEAR, year);
    c.set(Calendar.MONTH, month);
    c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
    String rideDateString = DateFormat.getDateInstance(DateFormat.SHORT).format(c.getTime());
}

Here the output will be in the format 'm/d/yy'. That is, for example: '3/9/20' for 9th march 2020.

I want a output like 'mm-dd-yyyy'. That is, for example: '03-09-2020 for 9th march 2020'.

What are the ways for achieving this result?

Prudhvi
  • 1,095
  • 1
  • 7
  • 18
imn
  • 620
  • 1
  • 7
  • 19
  • 1
    Have you tried to use different pre-built formats like `DateFormat.FULL` or `DateFormat.MEDIUM`? However, my advice is: **Use [`java.time`](https://www.oracle.com/technical-resources/articles/java/jf14-date-time.html) if possible and/or use the [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP)** – deHaar Mar 09 '20 at 12:35

1 Answers1

0

Check this SimpleDateFormat

Sample usage

new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(new Date())
Y.emre
  • 1
  • 2