如何在JodaTime中获取星期几的名称

18

我有一个问题,需要使用JodaTime计算整个星期的名称列表。实际上,我希望看到一个基于Locale的类似输出:

1 day: Sunday 
2 day: Monday 
3 day: Tuesday 
4 day: Wednesday 
5 day: Thursday 
6 day: Friday 
7 day: Saturday

我该怎么做?我是JodaTime库的新手...

谢谢!

2个回答

23

看起来需要使用 DateTimeFormat

我会从这里开始

 DateTime dt = new DateTime();
 DateTimeFormatter fmt = DateTimeFormat.forPattern("EEEE"); // use 'E' for short abbreviation (Mon, Tues, etc)
 String strEnglish = fmt.print(dt);
 String strFrench = fmt.withLocale(Locale.FRENCH).print(dt);
 String strWhereverUR = fmt.withLocale(Locale.getDefault()).print(dt);

然后从那里开始


谢谢! :) 很好的解决方案...但是,我能用Locale.getdefault()吗?我不想管理每个Locale... - user3449772
2
实际上,“E”模式只会打印“Fri”。要打印“Friday”,请使用“EEEE”。 - kellogs

23

来自Joda-Time 用户指南:

例如,获取特定DateTime的星期几的直接方法涉及调用该方法

int iDoW = dt.getDayOfWeek();

where iDoW can take the values (from class DateTimeConstants).

public static final int MONDAY = 1;
public static final int TUESDAY = 2;
public static final int WEDNESDAY = 3;
public static final int THURSDAY = 4;
public static final int FRIDAY = 5;
public static final int SATURDAY = 6;
public static final int SUNDAY = 7;


...
 Localized versions of these methods are also available, thus
  DateTime.Property pDoW = dt.dayOfWeek();
  String strTF = pDoW.getAsText(Locale.FRENCH); // returns "Lundi", etc.

编辑 如果使用默认语言环境

DateTime.Property pDoW = dt.dayOfWeek();
String strTF = pDoW.getAsText(Locale.getDefault());

谢谢!最后一个问题:我该如何使用Locale.getDefault()?DateTimeConstants类中的值是否可以更改? - user3449772

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接