Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Java Calendar getDisplayName/s returning 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()));
    }

This case crashes my android app, because Log.e cannot accept null string.

It is same thing with

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

I tried many variations with input parameters. For locale I also did Locale.getDefault() and many other. Still same **

No darn luck. Is there anything to solve this? Or a easy workaround?

like image 778
Vili Volcini Avatar asked Jan 13 '23 11:01

Vili Volcini


1 Answers

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

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

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

There is not String representation of HOUR, i.e you can express HOURS in string like we did for MONTH And DAY_OF_WEEK

Try WEEK, MONTH Example

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

You will 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

like image 84
Pragnani Avatar answered Jan 24 '23 14:01

Pragnani