如何在间隔期间获得周末?乔达时间

时间:2014-02-23 18:15:23

标签: java datetime jodatime intervals

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

有没有办法获得周末天数?

类似的东西:

interval.toPeriod.getWeekendDays();

3 个答案:

答案 0 :(得分:2)

自定义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)

代码用Scala编写。将它移植到Java应该很简单。

答案 1 :(得分:1)

这是使用Joda-Time 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年16月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]

答案 2 :(得分:0)

  

打包com.test.tryings;

     

import java.util.ArrayList;导入java.util.HashMap;进口   java.util.List;

     

导入org.joda.time.DateTime;导入org.joda.time.DateTimeConstants;   导入org.joda.time.Days;导入org.joda.time.LocalDate;进口   org.joda.time.format.DateTimeFormat;

     

公共类DateTest {

     

org.joda.time.format.DateTimeFormatter dateStringFormat =   DateTimeFormat.forPattern(“ dd-MM-yyyy”);字符串firstDate =   “ 13-12-2019”;字符串seconddate =“ 23-12-2019”;

     

void noOfDays(){

  System.out.println("To no of dates for matches...");

  HashMap<Integer, String> totalDays = new HashMap<Integer, String>();
  System.out.println("No of days between this days..");       DateTime
     

start = dateStringFormat.parseDateTime(firstDate); DateTime结束=   dateStringFormat.parseDateTime(seconddate);对于(LocalDate   currentdate = start.toLocalDate();   currentdate.isBefore(end.toLocalDate())                 || currentdate.isEqual(end.toLocalDate()); currentdate = currentdate.plusDays(1)){             System.out.println(currentdate.toString(dateStringFormat)); }         System.out.println(“至匹配日期为止...”);

     

}

     

void excludeWeekEnds(){

  DateTime start = dateStringFormat.parseDateTime(firstDate);
  DateTime end = dateStringFormat.parseDateTime(seconddate);

  System.out.println("Exclude weekends for the matches...");

  for (LocalDate currentdate = start.toLocalDate();
     

currentdate.isBefore(end.toLocalDate())                 || currentdate.isEqual(end.toLocalDate()); currentdate = currentdate.plusdays(1)){如果(currentdate.getDayOfWeek()==   DateTimeConstants.SATURDAY                     || currentdate.getDayOfWeek()== DateTimeConstants.SUNDAY){                 继续; }其他{                 System.out.println(currentdate.toString(dateStringFormat)); }} System.out.println(“排除周末比赛...”); }

     

避免excludeWeekDays(){

  DateTime start = dateStringFormat.parseDateTime(firstDate);
  DateTime end = dateStringFormat.parseDateTime(seconddate);

  System.out.println("Exclude weekdays for the matches...");

  for (LocalDate currentdate = start.toLocalDate();
     

currentdate.isBefore(end.toLocalDate())                 || currentdate.isEqual(end.toLocalDate()); currentdate = currentdate.plusdays(1)){如果(currentdate.getDayOfWeek()==   DateTimeConstants.SATURDAY                     || currentdate.getDayOfWeek()== DateTimeConstants.SUNDAY){                 System.out.println(currentdate.toString(dateStringFormat)); }} System.out.println(“排除比赛的工作日...”);

     

}

     

public static void main(String [] args){

  DateTest dt = new DateTest();       dt.noOfDays();
  dt.excludeWeekEnds();       dt.excludeWeekDays();   } }