How to get localized short day-in-week name (Mo/Tu/We/Th...)

Viewed 55934

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

8 Answers

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

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

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

Related