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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24315101/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:28:33  来源:igfitidea点击:

How can I get the names of days of week in JodaTime

javadatetimearraylistjodatimedays

提问by user3449772

I've a problem to calculate an entire name list of days of week, using JodaTime. Pratically, I would like to see a similar output based on Locale:

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

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

How can I do? I'm new in JodaTime libraries...

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

Thanks!

谢谢!

采纳答案by dkatzel

From Joda-Time userguide:

从乔达时间userguide

For instance, the direct way to get the day of week for a particular DateTime, involves calling the method

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

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.

EDITIf using Default Locale

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

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

回答by sevensevens

Looks like a job for DateTimeFormat

看起来像是DateTimeFormat的工作

I would start with

我会开始

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

and go from there

然后从那里去