0

I want to print localized one character day name in java using something like DateFormat. Like: "S" for Sunday "M" for Monday

But should be able to work for other locales too.

Aamir Abro
  • 838
  • 12
  • 24
  • 2
    Take the full name and print only first character may be. – Rohit5k2 Jan 11 '16 at 12:37
  • You will write some codes. Date and pattern list --> https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html – nurisezgin Jan 11 '16 at 12:48
  • For English locales, days don't all begin with different letters (Tuesday, Thursday; Saturday, Sunday), so a single-letter representation doesn't work (or at least is ambiguous). A 2-letter representation is common, though (Su, Mo, Tu, We, Th, Fr, Sa). – Andy Turner Jan 11 '16 at 13:11

1 Answers1

2

You can try like this :

public class test {
    public static void main(String[] args) {
        Locale locale = Locale.getDefault();
        DateFormat weekdayNameFormat = new SimpleDateFormat("E", locale);
        String weekday = weekdayNameFormat.format(new Date());
        System.out.println(weekday.charAt(0));
    }

}
soorapadman
  • 4,451
  • 7
  • 35
  • 47
  • @René thanks edited. – soorapadman Jan 11 '16 at 12:51
  • Note this doesn't actually print a one-character day name, as requested in question title (at least in UK locale http://ideone.com/YFqDvH). – Andy Turner Jan 11 '16 at 13:10
  • 1
    This shouldn't be the correct answer because the value returned is not the real localized value. For example, in Spanish Wednesday is "miércoles" but the abbreviation with one char is 'X' and with this solution the value returned is the first char of "mie." that is "M". The solution for me was: Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); calendar.set(Calendar.DAY_OF_WEEK,weekDay); SimpleDateFormat formatter = new SimpleDateFormat("E"); String dayAbbreviation = formatter.format(calendar.getTime()) – ezefire Apr 07 '17 at 07:29