42

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

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
fhucho
  • 34,062
  • 40
  • 136
  • 186

8 Answers8

64

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
  • 34,977
  • 11
  • 70
  • 85
  • 17
    Be aware that the dayNames array will contain 8 elements, the first being an empty string, so the array can be directly indexed with a day number where Sunday is 1 and Saturday is 7. – RenniePet Aug 17 '13 at 19:16
  • 2
    FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Dec 11 '18 at 03:25
39

If standard abbreviations are fine with you, just use Calendar class like this:

myCal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.US);
Drakanor
  • 391
  • 3
  • 2
28

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.

Greg Case
  • 3,200
  • 1
  • 19
  • 17
13

java.time

Update for those using Java 8 and later.

ZoneId zoneId = ZoneId.of("America/Los_Angeles");

Instant instant = Instant.now();

ZonedDateTime zDateTime = instant.atZone(zoneId);

DayOfWeek day = zDateTime.getDayOfWeek();

Show output.

System.out.println(day.getDisplayName(TextStyle.SHORT, Locale.US));
System.out.println(day.getDisplayName(TextStyle.NARROW, Locale.US));

When run. See similar code run live at IdeOne.com.

Tue

T

Community
  • 1
  • 1
WitVault
  • 23,445
  • 19
  • 103
  • 133
9

DateTimeFormatter#localizedBy

Starting with Java SE 10, you can use DateTimeFormatter#localizedBy.

Demo:

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {

    public static void main(String args[]) {
        DateTimeFormatter dtfHindi = DateTimeFormatter.ofPattern("E").localizedBy(Locale.forLanguageTag("hi"));
        DateTimeFormatter dtfBangla = DateTimeFormatter.ofPattern("E").localizedBy(Locale.forLanguageTag("bn"));
        DateTimeFormatter dtfGerman = DateTimeFormatter.ofPattern("E").localizedBy(Locale.forLanguageTag("de"));
        DateTimeFormatter dtfEnglish = DateTimeFormatter.ofPattern("E").localizedBy(Locale.forLanguageTag("en"));

        // Replace ZoneId.systemDefault() with the applicable timezone e.g.
        // ZoneId.of("Asia/Calcutta")
        LocalDate today = LocalDate.now(ZoneId.systemDefault());

        System.out.println(dtfHindi.format(today));
        System.out.println(dtfBangla.format(today));
        System.out.println(dtfGerman.format(today));
        System.out.println(dtfEnglish.format(today));
    }
}

Output:

शुक्र
শুক্র
Fr.
Fri

Alternatively, starting with Java SE 8, you can use DayOfWeek#getDisplayName with the applicable Locale.

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.TextStyle;
import java.util.Locale;

public class Main {

    public static void main(String args[]) {
        // Replace ZoneId.systemDefault() with the applicable timezone e.g.
        // ZoneId.of("Asia/Calcutta")
        LocalDate today = LocalDate.now(ZoneId.systemDefault());

        System.out.println(today.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.forLanguageTag("hi")));
        System.out.println(today.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.forLanguageTag("bn")));
        System.out.println(today.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.forLanguageTag("de")));
        System.out.println(today.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.forLanguageTag("en")));
    }
}

Output:

शुक्र
শুক্র
Fr.
Fri
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
3

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
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
rlb.usa
  • 14,942
  • 16
  • 80
  • 128
2

You can use this :

public String getDayString(){
    Locale locale = Locale.getDefault();
    LocalDate date = LocalDate.now();
    DayOfWeek day = date.getDayOfWeek();
    return day.getDisplayName(TextStyle.FULL, locale);
}

The result will be: Monday

0
//you need to use joda library           
List<String> dayList = new ArrayList<String>();
String[] days = new String[7];
int a=2; 
//a =starting day of week 1=sunday ,2=monday
Calendar c2 = Calendar.getInstance(); 
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");   
c2.set((Calendar.DAY_OF_WEEK),a); 
int maxDay = c2.getActualMaximum(Calendar.DAY_OF_WEEK);
for(int i=0;i<maxDay;i++)
{                 
   days[i] = df.format(c2.getTime());
   c2.add(Calendar.DAY_OF_MONTH, 1);
   String dayOfWeek = new LocalDate( days[i]).dayOfWeek().getAsShortText();
   dayList.add(dayOfWeek);         
}            
for(int i=0;i<maxDay;i++)
{
    System.out.print(" '"+dayList.get(i)+"'");
}            
shankar.parshimoni
  • 1,289
  • 5
  • 22
  • 42