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

时间:2014-06-19 19:48:43

标签: java datetime arraylist jodatime days

使用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 个答案:

答案 0 :(得分:18)

看起来像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);

从那里开始

答案 1 :(得分:17)

来自Joda-Time userguide

  

例如,获取特定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());