Java 如何获得本地化的短星期名称 (Mo/Tu/We/Th...)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3790954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get localized short day-in-week name (Mo/Tu/We/Th...)
提问by fhucho
Can I get localized short day-in-week name (Mo/Tu/We/Th/Fr/Sa/Su for English) in Java?
我可以在 Java 中获得本地化的短星期几名称(英语为 Mo/Tu/We/Th/Fr/Sa/Su)吗?
采纳答案by RealHowTo
The best way is with java.text.DateFormatSymbols
最好的方法是使用 java.text.DateFormatSymbols
DateFormatSymbols symbols = new DateFormatSymbols(new Locale("it"));
// for the current Locale :
// DateFormatSymbols symbols = new DateFormatSymbols();
String[] dayNames = symbols.getShortWeekdays();
for (String s : dayNames) {
System.out.print(s + " ");
}
// output : dom lun mar mer gio ven sab
回答by rlb.usa
You can't do it with the Calendar class (unless you write your own), but you can with the Date class. (The two are usually used hand-in-hand).
你不能用 Calendar 类来做(除非你自己写),但你可以用 Date 类。(两者通常是同时使用的)。
Here's an example:
下面是一个例子:
import java.util.Date;
public class DateFormatExample {
public static void main(String[] args) {
Calendar nowCal = Calendar.getInstance(); // a Calendar date
Date now = new Date(nowCal.getTimeInMillis()); // convert to Date
System.out.printf("localized month name: %tB/%TB\n", now, now);
System.out.printf("localized, abbreviated month: %tb/%Tb\n", now, now);
System.out.printf("localized day name: %tA/%TA\n", now, now);
System.out.printf("localized, abbreviated day: %ta/%Ta\n", now, now);
}
}
Output:
输出:
localized month name: June/JUNE
localized, abbreviated month: Jun/JUN
localized day name: Friday/FRIDAY
localized, abbreviated day: Fri/FRI
回答by Greg Case
An example using SimpleDateFormat:
使用 SimpleDateFormat 的示例:
Date now = new Date();
// EEE gives short day names, EEEE would be full length.
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE", Locale.US);
String asWeek = dateFormat.format(now);
SimpleDateFormat as been around longer than the C-style String.format and System.out.printf, and I think you'd find most Java developers would be more familiar with it and more in use in existing codebases, so I'd recommend that approach.
SimpleDateFormat 比 C 风格的 String.format 和 System.out.printf 存在的时间更长,我想你会发现大多数 Java 开发人员会更熟悉它,并且更多地在现有代码库中使用,所以我建议方法。
回答by Drakanor
If standard abbreviations are fine with you, just use Calendar class like this:
如果标准缩写适合您,只需像这样使用 Calendar 类:
myCal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.US);
回答by Rahul Gholap
//you need to use joda library
List<String> dayList = new ArrayList<String>();
String[] days = new String[7];
int a=2;
//a =starting day of week 1=sunday ,2=monday
Calendar c2 = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
c2.set((Calendar.DAY_OF_WEEK),a);
int maxDay = c2.getActualMaximum(Calendar.DAY_OF_WEEK);
for(int i=0;i<maxDay;i++)
{
days[i] = df.format(c2.getTime());
c2.add(Calendar.DAY_OF_MONTH, 1);
String dayOfWeek = new LocalDate( days[i]).dayOfWeek().getAsShortText();
dayList.add(dayOfWeek);
}
for(int i=0;i<maxDay;i++)
{
System.out.print(" '"+dayList.get(i)+"'");
}
回答by WitVault
java.time
时间
Update for those using Java 8 and later.
为使用 Java 8 及更高版本的用户更新。
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.
跑的时候。在 IdeOne.com 上查看实时运行的类似代码。
Tue
T
周二
吨