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
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
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];
}
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.