-3

Can anyone please give me the snippet for converting Date "2018-04-20" to 20 April, 2018 in Android Studio 3.0.

Also my i have the time string along with my date coming from webservice

2018-04-20 12:30:00

Waqas
  • 23
  • 4
  • Whoever has down voted my question, can you provide me the link where I can find the answer .... ??? – Waqas Apr 22 '18 at 12:37
  • maybe because there are already similar questions asked on SO. e.g. https://stackoverflow.com/questions/12781273/what-are-the-date-formats-available-in-simpledateformat-class – Shubham AgaRwal Apr 22 '18 at 12:47
  • 1
    Possible duplicate of [How do you format date and time in Android?](https://stackoverflow.com/questions/454315/how-do-you-format-date-and-time-in-android) – Shubham AgaRwal Apr 22 '18 at 12:48
  • 1
    Thanks anyway ... – Waqas Apr 22 '18 at 12:54

2 Answers2

1

The code snippet below demonstrates how you can convert the current date to the format as required by you.

final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR); // current year
int mMonth = c.get(Calendar.MONTH); // current month
int mDay = c.get(Calendar.DAY_OF_MONTH);// current day
String monthName = getMonthName(mMonth);
String dateInCorrectFormat = (mDay + monthName + ", " + mYear); // will display in format "22April, 2018"

The getMonthName method can be defined as follows:

public static String getMonthName(int month) {
    String[] monthNames = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    return monthNames[month];
}
Huzaifa Iftikhar
  • 755
  • 1
  • 6
  • 9
0
String pattern = "yyyy-MM-dd";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

Date date = simpleDateFormat.parse("2012-12-24");

You can go here to format the date in your requested format.