How to format date in uppercase?

I am trying to format the date like this:

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 do not want to use any split method or do something like that. Is there any pattern that I can include in the regular expression so that it is capitalized at the beginning of each word?

UPDATE I am from a Spanish-speaking country, something like new Locale("es", "ES") I get "martes 7, noviembre, 2013" when I need "Martes 7, Noviembre, 2013".

+6
source share
2 answers

You can change the lines that SimpleDateFormat produces by setting the DateFormatSymbols that it uses. The official guide provides an example: http://docs.oracle.com/javase/tutorial/i18n/format/dateFormatSymbols.html

Reproduction of an example from a textbook applied to โ€œshort working daysโ€:

 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 day of the week: " + result); 
+7
source

Using Locale.US , it works great:

 SimpleDateFormat sdf = new SimpleDateFormat("EEEEE d, MMMMM, yyyy", Locale.US); System.out.println(sdf.format(Date.valueOf("2013-11-07"))); 

Output:

 Thursday 7, November, 2013 
+1
source

Source: https://habr.com/ru/post/957585/


All Articles