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

2022-09-02 19:37:56

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

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

我该怎么办?我是JodaTime图书馆的新手...

谢谢!


答案 1

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

答案 2

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

并从那里开始