tags:

views:

64

answers:

4

Can I get localized short day-in-week name (Mo/Tu/We/Th/Fr/Sa/Su for English) in Java?

A: 

Look at SimpleDateFormatter.

Etienne de Martel
+1  A: 

You can't do it with the Calendar class (unless you write your own), but you can with the Date class. (The two are usually used hand-in-hand).

Here's an example :

import java.util.Date;

public class DateFormatExample {

  public static void main(String[] args) {
    Calendar nowCal = Calendar.getInstance(); // a Calendar date
    Date now = new Date(nowCal.getTimeInMillis()); // convert to Date
    System.out.printf("localized month name: %tB/%TB\n", now, now); 
    System.out.printf("localized, abbreviated month: %tb/%Tb\n", now, now); 
    System.out.printf("localized day name: %tA/%TA\n", now, now);
    System.out.printf("localized, abbreviated day: %ta/%Ta\n", now, now); 
  }
}

Output:

localized month name: June/JUNE 
localized, abbreviated month: Jun/JUN 
localized day name: Friday/FRIDAY 
localized, abbreviated day: Fri/FRI
rlb.usa
Thanks! What is the simplest way to put it into String?
fhucho
+1  A: 

The best way is with java.text.DateFormatSymbols

DateFormatSymbols symbols = new DateFormatSymbols(new Locale("it"));
// for the current Locale :
//   DateFormatSymbols symbols = new DateFormatSymbols(); 
String[] dayNames = symbols.getShortWeekdays();
for (String s : dayNames) { 
   System.out.print(s + " ");
}
// output :  dom lun mar mer gio ven sab 
RealHowTo
+1  A: 

An example using SimpleDateFormat:

Date now = new Date();
// EEE gives short day names, EEEE would be full length.
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE", Locale.US); 
String asWeek = dateFormat.format(now);

SimpleDateFormat as been around longer than the C-style String.format and System.out.printf, and I think you'd find most Java developers would be more familiar with it and more in use in existing codebases, so I'd recommend that approach.

gregcase