1

I want to convert a date into "yyyy-MM-dd". I am using

 val parser = SimpleDateFormat("dd-MMM-yyyy") // The original format
 val formatter = SimpleDateFormat("yyyy-MM-dd")
 val output = formatter.format(parser.parse(etDOB.text.toString()))

as given here. But Android studio is warning me to use SimpleDateFormat(String, Locale). Which locale should I use to get output into "yyyy-MM-dd"?

Or is there a better way to achieve this (API 21)? I also have the date in a calendar instance.

Justin
  • 63
  • 6

1 Answers1

0

Use Locale.getDefault() in SimpleDateFormat

 val formatter = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
Waqar UlHaq
  • 6,144
  • 2
  • 34
  • 42
  • That gave me "Wed Mar 01 00:00:00 GMT+5.30 1995" This is my code: `val formatter = SimpleDateFormat("dd-MMM-yyyy", Locale.getDefault()) formatter.parse(etDOB.text.toString()` – Justin Mar 01 '20 at 17:38
  • Also, is Locale.getDefault() depends on the locale set in the user device? Won't that change from user to user? – Justin Mar 01 '20 at 17:42