java - 如何将日期格式化为大写?

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

Monday 4, November, 2013

这是我的代码:

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);
}

但我不想使用一些 split 方法或做类似的事情。是不是有一些模式可以包含在正则表达式中,以使其在每个单词的开头都是大写的?

更新 我来自西类牙裔国家,类似于 new Locale("es", "ES") 当我需要的是“Martes 7, Noviembre, 2013”​​时,我得到“martes 7, noviembre, 2013” ”。

最佳答案

您可以通过设置它使用的 DateFormatSymbols 来更改 SimpleDateFormat 输出的字符串。官方教程包括一个这样的例子: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);

https://stackoverflow.com/questions/19836978/

相关文章:

excel - 我的 Excel 行被锁定在 409.5 的高度,我想增加大小

jsp - IntelliJ JSP 格式化

c# - 在 RichTextBox 中格式化单词

excel - 如何将 Excel 2010 中的条件格式复制到基于其他单元格内容的其他单元格?

ruby - 在 ERB 模板中转义换行符/行尾

objective-c - 你如何调整 Xcode 的 Objective C 参数的自动缩进?

kotlin - 如何将粗体、斜体、下划线等格式添加到 Kotlin 文档 (KDoc)

python - 条件格式 xlwt

python - 使用 Python 格式化 Excel 中的单元格

sql - 计算组百分比到小数点后 2 位 - SQL