DatePicker Date Display for Android

I am trying to use Date Picker and on Select I want to show the date in the following format

[Name of the month] [date], [year]

final Calendar c = Calendar.getInstance(); mYear = c.get(Calendar.YEAR)-13; mMonth = c.get(Calendar.MONTH); mDay = c.get(Calendar.DAY_OF_MONTH); 

which gives the month as a number. How to get the name of the month instead of the number.

+6
source share
6 answers
 public static final String[] MONTHS = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; 

use an array and get the string MONTHS[monthNumber] .

+18
source

Try the following:

  final Calendar c = Calendar.getInstance(); c.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault()); 

the output will be January if Month == 1; Or you can use this

  c.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.getDefault()); 

Then you get Jan

+4
source

Use the switch statement:

 String monthName; switch(mMonth){ case Calendar.JANUARY: monthName = "January"; break; case Calendar.FEBRUARY: monthName = "February"; break; 

and etc.

+3
source

After receiving the year, month and day, you can format the date as follows:

 DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM); 

DateFormat.MEDIUM displays the date as January 12, 1952 in the example. If you want to display the full name of the month, you can use DateFormat.LONG.

+2
source

Example:

 //if or swith if (c.get(Calendar.MONTH)==(Calendar.FEBRUARY)) { // Do something like // String Month = "FEBRUARY"; } 
+1
source

After receiving the year, month and day, you can format the date as follows:

 DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM); 

DateFormat.MEDIUM displays the date as January 12, 1952 in the example. If you want to display the full name of the month, you can use DateFormat.LONG.

This is the easiest way, in my opinion.

+1
source

Source: https://habr.com/ru/post/900345/


All Articles