【问题标题】:how to get weekends in an Interval? joda time如何在间隔中度过周末?乔达时间
【发布时间】:2023-03-09 14:11:02
【问题描述】:
Interval interval = new Interval(new DateTime("2014-01-01"), new DateTime("2014-01-30"));

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

类似:

interval.toPeriod.getWeekendDays();

【问题讨论】:

标签: java datetime jodatime intervals


【解决方案1】:

自定义Iterator

WeekendDayIterator 是指定的Interval 中周末的Iterator

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 应该很简单。

【讨论】:

    【解决方案2】:

    这是一个使用 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));
     } 
    }
    

    如果您想要从今天 (04-16-2016) 和 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]
    

    【讨论】:

      【解决方案3】:

      包 com.test.tryings;

      导入 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 = “2019 年 13 月 12 日”;字符串 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
      

      开始 = dateStringFormat.parseDateTime(firstDate);日期时间结束 = dateStringFormat.parseDateTime(seconddate);对于(本地日期 currentdate = start.toLocalDate(); currentdate.isBefore(end.toLocalDate()) || currentdate.isEqual(end.toLocalDate()); currentdate = currentdate.plusDays(1)) { System.out.println(currentdate.toString(dateStringFormat)); } System.out.println("To no of dates for matches...");

      }

      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)) { if (currentdate.getDayOfWeek() == 日期时间常数.星期六 || currentdate.getDayOfWeek() == DateTimeConstants.SUNDAY) { 继续; } 别的 { System.out.println(currentdate.toString(dateStringFormat)); } } System.out.println("排除周末的比赛..."); }

      void 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)) { if (currentdate.getDayOfWeek() == 日期时间常数.星期六 || 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();   } }
      

      【讨论】:

        猜你喜欢
        • 2019-08-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-18
        • 1970-01-01
        • 2020-09-28
        • 1970-01-01
        • 2015-11-28
        相关资源
        最近更新 更多