Java – How to format a date in uppercase

calendardateformattingjava

I'm trying to format a date in this way:

Monday 4, November, 2013

This is my code:

private static String formatDate(Date date) {
  Calendar calenDate = Calendar.getInstance();
  calenDate.setTime(date);
  Calendar today = Calendar.getInstance();
  if (calenDate.get(Calendar.DAY_OF_MONTH) == today.get(Calendar.DAY_OF_MONTH)) {
    return "Today";
  }
  today.roll(Calendar.DAY_OF_MONTH, -1);
  if (calenDate.get(Calendar.DAY_OF_MONTH) == today.get(Calendar.DAY_OF_MONTH)) {
    return "Yesterday";
  }
  // Guess what buddy
  SimpleDateFormat sdf = new SimpleDateFormat("EEEEE d, MMMMM, yyyy");
  // This prints "monday 4, november, 2013" ALL in lowercase
  return sdf.format(date);
}

But I don't want to use some split method or do something like that. Isn't there some pattern that I can include in the regexp to make it be uppercase at the begin of each word?

UPDATE
I'm from a hispanic country, something like new Locale("es", "ES") I get "martes 7, noviembre, 2013" when what I need is "Martes 7, Noviembre, 2013".

Best Answer

You can change the strings that SimpleDateFormat outputs by setting the DateFormatSymbols it uses. The official tutorial includes an example of this: http://docs.oracle.com/javase/tutorial/i18n/format/dateFormatSymbols.html

Reproduction of the example from the tutorial, applied to the "short weekdays":

String[] capitalDays = {
    "", "SUN", "MON",
    "TUE", "WED", "THU",
    "FRI", "SAT"
};
symbols = new DateFormatSymbols( new Locale("en", "US"));
symbols.setShortWeekdays(capitalDays);

formatter = new SimpleDateFormat("E", symbols);
result = formatter.format(new Date());
System.out.println("Today's day of the week: " + result);