how to get dateformat to capitalize month and day

前端 未结 4 923
执笔经年
执笔经年 2021-01-14 10:55

I have my strings like so in my strings.xml:

EEEE
dd. MMMM         


        
相关标签:
4条回答
  • 2021-01-14 11:00

    I fixed it like so:

            final CharSequence date = DateFormat.format(mDateFormat, mCalendar);
        final CharSequence day = DateFormat.format(mDayFormat, mCalendar);
        String time = (String) DateFormat.format(mTimeFormat, mCalendar);
        RemoteViews views = new RemoteViews(getPackageName(), R.layout.clock2by2);
        String days = new String(day.toString().substring(0,1).toUpperCase() + day.toString().substring(1));
        String dates = new String(date.toString().substring(0,1).toUpperCase() + date.toString().substring(1));
    
        views.setTextViewText(R.id.Day, days);
        views.setTextViewText(R.id.Date, dates);
        views.setImageViewBitmap(R.id.TimeView, buildUpdate(time));
    
    0 讨论(0)
  • 2021-01-14 11:06

    String's toUpperCase() ?

    0 讨论(0)
  • 2021-01-14 11:07

    There are more precise way of doing this. You can control each part of your formatter using DateFormatSymbols.

    Look at that, this is beautiful:

    Scanner s = new Scanner(System.in);
    SimpleDateFormat simpleDateFormatFrom = new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat simpleDateFormatTo = new SimpleDateFormat("MMM dd, yyyy");
    
    DateFormatSymbols dateFormatSymbols = new DateFormatSymbols();
    String[] months = simpleDateFormatTo.getDateFormatSymbols().getShortMonths();
    for (int i = 0; i < months.length; i++) {
        months[i] = months[i].toUpperCase();
    }
    dateFormatSymbols.setShortMonths(months);
    simpleDateFormatTo.setDateFormatSymbols(dateFormatSymbols);
    
    Date date = simpleDateFormatFrom.parse(s.next());
    System.out.println(simpleDateFormatTo.format(date));
    
    0 讨论(0)
  • 2021-01-14 11:21

    You can use WordUtils.capitalize(..) from commons-lang (on the result string)

    (this is in case you want to capitalize - i.e. uppercase only the first letter. Otherwise you can simpyl use .toUpperCase())

    Update: Since it appears this is android, you can open the sources of WordUtils and copy the implementation from there, rather than getting the whole library.

    0 讨论(0)
提交回复
热议问题