3

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

<string name="day_format">EEEE</string>
<string name="date_format">dd. MMMM</string>
<string name="date_format_us">MMMM dd</string>

And I use them like this in the code:

    private void reinit() {
    mDayFormat = getString(R.string.day_format);
    if (!DateFormat.is24HourFormat(this))
    {
    mDateFormat = getString(R.string.date_format_us);
    }
    else {
        mDateFormat = getString(R.string.date_format);
    }
    mTimeFormat = is24HourMode(this) ? FORMAT_24_HOURS : FORMAT_12_HOURS;
    mCalendar = Calendar.getInstance();
}

But it displays the day and the month in lowercase, and I want it to capitalize both. How can I achieve that?

Janusz
  • 187,060
  • 113
  • 301
  • 369
tristan202
  • 1,081
  • 3
  • 19
  • 28

4 Answers4

5

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.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • +1, but I don't think it's worth to just include the commons jar into the Android app just because of one function. toUpperCase() should be fine; even if you just want the first letter to be capitalized, just use a helper method if you don't need anything else from commons. – Mathias Conradt Dec 11 '10 at 13:06
  • 2
    I missed the android there. (The `R` is the only thing to hint in that direction) – Bozho Dec 11 '10 at 13:07
2

String's toUpperCase() ?

madhurtanwani
  • 1,199
  • 7
  • 13
2

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));
tristan202
  • 1,081
  • 3
  • 19
  • 28
2

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));
Alexander.Iljushkin
  • 4,519
  • 7
  • 29
  • 46