如何让一个周末的时间间隔? 乔达时间(how to get weekends in an Int

2019-10-19 09:58发布

Interval interval = new Interval(new DateTime("2014-01-01"), new DateTime("2014-01-30"));

有没有办法让这个周末天数?

就像是:

interval.toPeriod.getWeekendDays();

Answer 1:

自定义Iterator

WeekendDayIterator是一个Iterator在周末天Interval规定:

class WeekendDayIterator(interval: Interval) extends Iterator[LocalDate] {
  var day = interval.getStart.toLocalDate
  private def peek = day.plusDays(1)
  def hasNext: Boolean = peek.isBefore(interval.getEnd.toLocalDate)
  private def isWeekend(ld: LocalDate): Boolean = ld.getDayOfWeek == SATURDAY || ld.getDayOfWeek == SUNDAY
  def next(): LocalDate = { val d = day; do day = peek; while (!isWeekend(day)); d }
}

def weekendLocalDates(interval: Interval): List[LocalDate] = new WeekendDayIterator(interval).toList

// The `LocalDate`s for weekend days
println(weekendLocalDates(new Interval(new DateTime("2013-10-01"), new DateTime("2014-01-30"))))

// The days of year for weekend days
println(weekendLocalDates(new Interval(new DateTime("2013-10-01"), new DateTime("2014-01-30"))).map(_.getDayOfYear))

导致下面的输出:

List(2013-10-01, 2013-10-05, 2013-10-06, 2013-10-12, 2013-10-13, 2013-10-19, 2013-10-20, 2013-10-26, 2013-10-27, 2013-11-02, 2013-11-03, 2013-11-09, 2013-11-10, 2013-11-16, 2013-11-17, 2013-11-23, 2013-11-24, 2013-11-30, 2013-12-01, 2013-12-07, 2013-12-08, 2013-12-14, 2013-12-15, 2013-12-21, 2013-12-22, 2013-12-28, 2013-12-29, 2014-01-04, 2014-01-05, 2014-01-11, 2014-01-12, 2014-01-18, 2014-01-19, 2014-01-25, 2014-01-26)
List(274, 278, 279, 285, 286, 292, 293, 299, 300, 306, 307, 313, 314, 320, 321, 327, 328, 334, 335, 341, 342, 348, 349, 355, 356, 362, 363, 4, 5, 11, 12, 18, 19, 25, 26)

该代码是写在斯卡拉 。 它移植到Java应该直截了当。



Answer 2:

下面是使用乔达时API的Java实现。 你基本上弄清楚的时候是第一个周末,从开始日期,再加入7天,保持在一个循环,直到结束日期之后。 你会收集所有的周末。 诀窍是,如果结束日期是周六,你要停在那里。

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import org.joda.time.LocalDate;

public class DateUtil {

public static List<String> getWeekends(LocalDate fromDate, LocalDate toDate){
    List<String> weekends = new ArrayList<String>();
    SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy");
    int fromDay = fromDate.getDayOfWeek();
    int daysToFirstWeekend = 6-fromDay;
    LocalDate nextWeekend = fromDate.plusDays(daysToFirstWeekend);
    while(nextWeekend.isBefore(toDate)){
        weekends.add(format.format(nextWeekend.toDate()));
        if(nextWeekend.plusDays(1).isBefore(toDate)){
            weekends.add(format.format(nextWeekend.plusDays(1).toDate()));
        }
        nextWeekend = nextWeekend.plusDays(7);
    }       
    return weekends;
}   

public static void main(String[] args){
    LocalDate fromDate = new LocalDate();
    LocalDate toDate = fromDate.plusDays(45);
    System.out.println(getWeekends(fromDate, toDate));
 } 
}

如果你想从现在开始一切从今天(2016年4月16日)和45日的周末,它会给输出

[04-16-2016, 04-17-2016, 04-23-2016, 04-24-2016, 04-30-2016, 05-01-2016, 05-07-2016, 05-08-2016, 05-14-2016, 05-15-2016, 05-21-2016, 05-22-2016]


文章来源: how to get weekends in an Interval? joda time