【问题标题】:How can I get the names of days of week in JodaTime如何在 JodaTime 中获取星期几的名称
【发布时间】:2014-06-19 19:48:43
【问题描述】:

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

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

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

谢谢!

【问题讨论】:

    标签: java datetime arraylist jodatime days


    【解决方案1】:

    来自 Joda-Time userguide:

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

    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 更改值?
    【解决方案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);
    

    从那里出发

    【讨论】:

    • 谢谢你! :) 很好的解决方案...但是,我可以使用 Locale.getdefault() 吗?我不会管理每个区域设置...
    • 实际上“E”模式只打印“Fri”。对于“星期五”,请使用“EEEE”。
    猜你喜欢
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    • 2021-07-10
    • 2018-08-09
    • 1970-01-01
    • 2019-05-18
    相关资源
    最近更新 更多