Sergio Viudes
Sergio Viudes

Reputation: 2854

Get one letter abbreviation of week day of a date in java

As far I know, in Java I can get weekdays in normal (Friday) or short mode (Fri). But, there is any way to obtain only first letter?

I thought I can get first letter using "substring", but it won't be correct for all languages. For example, spanish weekdays are: Lunes, Martes, Miércoles, Jueves, Viernes, Sábado and Domingo, and first letter for "Miércoles" is X instead of M to difference it from "Martes".

Upvotes: 20

Views: 12593

Answers (6)

flar2
flar2

Reputation: 78

DateFormatSymbols.getWeekdays with a width of NARROW will give you the first letter of each week day. It works for every language. However, it requires API 24.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    String[] weekDays = DateFormatSymbols.getInstance(Locale.getDefault())
            .getWeekdays(DateFormatSymbols.STANDALONE, DateFormatSymbols.NARROW);
}

Upvotes: 1

Husky
Husky

Reputation: 306

In Android you can use SimpleDateFormat with "EEEEE". In the next example you can see it.

SimpleDateFormat formatLetterDay = new SimpleDateFormat("EEEEE",Locale.getDefault());
String letter = formatLetterDay.format(new Date());

EDIT: it's actually not entirely true. The result on Android could have more than a single letter (and also non-unique, if this matters), but this is what we have. Here's proof that you won't get these characteristics on Android, going over all locales. It's written in Kotlin, but should work for Java too, of course:

val charCountStats = SparseIntArray()
Locale.getAvailableLocales().forEach { locale ->
    val sb = StringBuilder("$locale : ")
    val formatLetterDay = SimpleDateFormat("EEEEE", locale)
    for (day in 1..7) {
        val cal = Calendar.getInstance()
        cal.set(Calendar.DAY_OF_WEEK, day)
        val letter: String = formatLetterDay.format(cal.time)
        charCountStats.put(letter.length, charCountStats.get(letter.length, 0)+1)
        sb.append(letter)
        if (day != 7)
            sb.append(',')
    }
    Log.d("AppLog", "$sb")
}
Log.d("AppLog", "stats:")
charCountStats.forEach { key, value ->
    Log.d("AppLog", "formatted days with $key characters:$value")
}

And the result is that for most cases it's indeed a single letter, but for many it's more, and can even reaches 8 characters (though it might look as less letters, even one) :

formatted days with 1 characters:4889
formatted days with 2 characters:471
formatted days with 3 characters:99
formatted days with 4 characters:58
formatted days with 5 characters:3
formatted days with 8 characters:3

Example of a locale that it shows as 3 letters (and not just has 3 letters) is "wo" ("Wolof" language), as this is the result for each of its days of the week using the above formatting:

Dib,Alt,Tal,Àla,Alx,Àjj,Ase

Upvotes: 16

Stephen C
Stephen C

Reputation: 718826

There is no standard Java API support for doing that1.

Part of the reason is that many (maybe even most) languages don't have conventional unique one-letter weekday abbreviations. In English there isn't, for example (M T W T F S S).

A (hypothetical) formatting option that doesn't work2 in many / most locales would be an impediment to internationalization rather than a help.


It has been pointed out that:

SimpleDateFormat formatLetterDay = 
    new SimpleDateFormat("EEEEE", Locale.getDefault());
String letter = formatLetterDay.format(new Date());

gives one letter abbreviations for later versions of Android (18 and above), though the javadocs do not mention this. It appears that this "5 letter" format has been borrowed from DateTimeFormatter whose javadoc says:

The count of pattern letters determines the format.

Text: The text style is determined based on the number of pattern letters used. Less than 4 pattern letters will use the short form. Exactly 4 pattern letters will use the full form. Exactly 5 pattern letters will use the narrow form. ...

If you are targeting Android API 26 or later, you should consider using the java.time.* classes rather than the legacy classes.

But either way, this isn't guaranteed to give you unique day letters.


1 - By "that" I mean mapping to unique 1-letter abbreviations.
2 - I mean it doesn't work in the human sense. You could invent a convention, but typical people wouldn't understand what the abbreviations meant; e.g. they wouldn't know that "X" meant "Miércoles", or in English that (say) "R" meant "Thursday" (see https://stackoverflow.com/a/21049169/139985).

Upvotes: 6

Brandon Rude
Brandon Rude

Reputation: 4389

As mentioned above there is no standard Java support for this. Using the formatting string "EEEEE" however is not guaranteed to work on all Android devices. The following code is guaranteed to work on any device:

public String firstLetterOfDayOfTheWeek(Date date) {

    Locale locale = Locale.getDefault(); 
    DateFormat weekdayNameFormat = new SimpleDateFormat("EEE", locale); 
    String weekday = weekdayNameFormat.format(date); 
    return weekday.charAt(0)+""; 
}

Upvotes: 7

mharr
mharr

Reputation: 604

I realize the OP was asking for standards across languages, and this does not address it. But there is/was a standard for using single character Day of Week abbreviation.

Back in mainframe days, using a 1-character abbreviation for Day of Week was common, either to store day of week in 1 character field (to save precious space), or have a report heading for single-character column. The "standard" was to use MTWRFSU, where R was for Thursday, and U for Sunday.

I could not find any definitive references to this (which is why I quoted "standard", but here are a couple of examples: http://eventguide.com/topics/one_digit_day_abbreviations.html http://www.registrar.ucla.edu/soc/definitions.htm#Anchor-Days-3800

Upvotes: 1

Rachita Nanda
Rachita Nanda

Reputation: 4659

I think there's no direct java function to get the first letter and no standard way to do it.

You can refer to this link to obtain the first letter of the string day using substring() java method

Given a string in Java, just take the first X letters

Upvotes: 0

Related Questions