3

Is there any way I can get days of week as 2 letter?

Ex: Su, Mo, Tu, We ....

I tried Java DateFormatSymbols and it gives 3 letter short names for days.

   DateFormatSymbols symbols = new DateFormatSymbols(this.locale);
   symbols.getShortWeekdays();

Also I tried using Calender class as below;

    Calendar cal = Calendar.getInstance();
    DateFormat dayFormat = new SimpleDateFormat("EE", this.locale);
    for (int i = 0; i < 7; i++) {
        cal.set(Calendar.DAY_OF_WEEK, i);
        System.out.println(dayFormat.format(cal.getTime()));
    }

But it always give 3 letter days. I couldn't use substring since it will depend on the locale.

JagKum
  • 183
  • 2
  • 18
  • _I couldn't use substring since it will depend on the locale._ Yes, there are also arguments for. Check the length and use substring afterwards? I don't see any issues in it. – asdf Oct 27 '16 at 01:57

2 Answers2

0

Looks like DateFormatSymbols has a setShortWeekdays function, which will enable you to set them yourself to (presumably) whatever you like. Not ideal or even automatic though.

crackpotHouseplant
  • 380
  • 1
  • 3
  • 12
  • I need to get it for each locale. So setting values would not resolve my issue. – JagKum Oct 27 '16 at 01:14
  • It appears `DateFormatSymbols`' `shortWeekdays` default to 3 (although it doesn't *explicitly* say that anywhere). Are you asking where you might get these 2 letter strings for every locale? – crackpotHouseplant Oct 27 '16 at 01:16
  • Thanks for your reply. Yes I need 2 letter day string for each locale. Will update the original question. – JagKum Oct 27 '16 at 01:21
0

Best way to do this is to take the 3 letter string and pass it through an switch case or if-else-if statement to return the desired 2 letter string like so...

 public static String getTwoLetters(){
    String threeLetterDay = null;
    switch (threeLetterDay) {
        case "Sun":
            return "Su";
        case "Mon":
            return "Mo";
        case "Tue":
            return "Tu";
        case "Wed":
            return "We";
        case "Thu":
            return "Th";
        case "Fri":
            return "Fr";
        default:
            //if none of the above then it must be this one Sat
            return "Sa";
    }
}