Android Java Calendar getDisplayName / s returns null

timeOfMark = Calendar.getInstance(Locale.ENGLISH); Map<String, Integer> what = timeOfMark.getDisplayNames(Calendar.HOUR, Calendar.SHORT, Locale.ENGLISH); for(Map.Entry<String, Integer> pair : what.entrySet()) { Log.e(pair.getKey(), Integer.toString(pair.getValue())); } 

In this case, my Android application crashes because Log.e cannot accept an empty string.

Same thing with

 timeOfMark.getDisplayName(Calendar.HOUR, Calendar.SHORT, Locale.ENGLISH); 

I tried many options with input parameters. For the locale, I also used Locale.getDefault () and many others. Nonetheless **

Nothing wrong. Is there anything that can be solved? Or an easy workaround?

+4
source share
1 answer

public Map getDisplayNames (int field, int style, Locale locale)

This method returns a map containing all the display names in the style and locale, and their field values, or null if there is no string representation.

 timeOfMark.getDisplayName(Calendar.HOUR, Calendar.SHORT, Locale.ENGLISH); 

There is no string representation of HOUR, i.e. you can express HOURS in string , as was done for MONTH AND DAY_OF_WEEK

Try WEEK, MONTH Example

  Map< String, Integer> representations = now.getDisplayNames(Calendar.MONTH, Calendar.ALL_STYLES, locale); 

You'll get

  {Apr=3, April=3, Aug=7, August=7, Dec=11, December=11, Feb=1, February=1, Jan=0, January=0, Jul=6, July=6, Jun=5, June=5, Mar=2, March=2, May=4, Nov=10, November=10, Oct=9, October=9, Sep=8, September=8} 

For Calendar.DAY_OF_WEEK

 {Fri=6, Friday=6, Mon=2, Monday=2, Sat=7, Saturday=7, Sun=1, Sunday=1, Thu=5, Thursday=5, Tue=3, Tuesday=3, Wed=4, Wednesday=4} 

Try here

http://www.browxy.com/SubmittedCode/18596

+9
source

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


All Articles