如何用大写格式化日期?

4gu*_*71n 6 java formatting calendar date

我正在尝试以这种方式格式化日期:

Monday 4, November, 2013
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

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);
}
Run Code Online (Sandbox Code Playgroud)

但我不想使用某种split方法或做类似的事情.我是否可以在regexp中包含一些模式,使其在每个单词的开头都是大写的?

更新 我来自一个西班牙裔国家,就像new Locale("es", "ES")我得到的"martes 7,noviembre,2013"​​,我需要的是"Martes 7,Noviembre,2013"​​.

Jon*_*oni 7

您可以SimpleDateFormat通过设置DateFormatSymbols它使用来更改输出的字符串.官方教程包括以下示例:http: //docs.oracle.com/javase/tutorial/i18n/format/dateFormatSymbols.html

从教程中复制示例,应用于"短工作日":

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);
Run Code Online (Sandbox Code Playgroud)