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

こ雲淡風輕ζ 提交于 2020-01-03 06:49:30

问题


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".


回答1:


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());



回答2:


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)+""; 
}



回答3:


There is no standard Java API support for doing that.

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 work1 in many / most locales would be an impediment to internationalization rather than a help.


1 - I mean this in the human sense. You could invent a convention, but people wouldn't understand what the abbreviations meant.




回答4:


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




回答5:


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



来源:https://stackoverflow.com/questions/16959502/get-one-letter-abbreviation-of-week-day-of-a-date-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!