【问题标题】:Joda Time, Get week days [duplicate]Joda Time,获取工作日[重复]
【发布时间】:2016-03-02 22:53:16
【问题描述】:

如何使本地日期只计算周日期?

例如

LocalDate date = new LocalDate();
date.plusDays(10); //it returns plus days including sat and sun as 2013-03-21
//i am looking for a way
date.plusDays(10); //should return as 2013-03-26

我正在寻找一种方法来消除周末?

【问题讨论】:

    标签: java jodatime


    【解决方案1】:

    使用getDayOfWeek() 方法。回报如下。为了只得到工作日..你只需要检查返回值是否小于或等于5。

    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;
    

    【讨论】:

    • 仅供参考,如果您想直接访问这些常量,这些常量位于org.joda.time.DateTimeConstants
    • 为什么第一天从1而不是0开始我永远不会知道...这给我带来了一个小时左右的麻烦...
    • 它与 java.time.DayOfWeek 匹配,因此您可以使用它。喜欢:DayOfWeek.FRIDAY.getValue()
    【解决方案2】:
    LocalDate newDate = new LocalDate();
        int i=0;
        while(i<days)//days == as many as u wanted
        {
            newDate = newDate.plusDays(1);
            System.out.println("new date"+newDate);
            if(newDate.getDayOfWeek()<=5)
            {
                i++;
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 2014-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-18
      • 2011-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多