Joda时间:如何在某个日期间隔获取工作日的日期?

时间:2010-12-28 17:02:04

标签: java datetime date jodatime

我有两个LocalDates代表一些时间间隔。现在我必须得到所有星期五的LocalDates,这个间隔包含。 最简单的方法吗?

3 个答案:

答案 0 :(得分:6)

package org.life.java.so.questions;

import org.joda.time.DateTime;
import org.joda.time.DateTimeConstants;

/**
 *
 * @author Jigar
 */
public class JodaTimeDateTraverseQuestion {
    public static void main(String[] args) {

        DateTime startDt = new DateTime(2010,12,1,0,0,0,0);//1st Dec 2010
        DateTime endDt = new DateTime(2010,12,31,0,0,0,0);//31st Dec 2010
        DateTime tempDate = new DateTime(startDt.getMillis());
        while(tempDate.compareTo(endDt) <=0 ){
            if(tempDate.getDayOfWeek() !=  DateTimeConstants.SATURDAY && tempDate.getDayOfWeek() !=  DateTimeConstants.SUNDAY){
                System.out.println(""+tempDate);
            }
            tempDate = tempDate.plusDays(1);

        }


    }
}

答案 1 :(得分:4)

解决方案:懒洋洋地一步一周。

import org.joda.time.LocalDate;
import java.util.Iterator;

public class DayOfWeekIterator implements Iterator<LocalDate>{
    private final LocalDate end;
    private LocalDate nextDate;

    public DayOfWeekIterator(LocalDate start, LocalDate end, int dayOfWeekToIterate){
        this.end = end;
        nextDate = start.withDayOfWeek(dayOfWeekToIterate);
        if (start.getDayOfWeek() > dayOfWeekToIterate) {
            nextDate = nextDate.plusWeeks(1);
        }
    }

    public boolean hasNext() {
        return !nextDate.isAfter(end);
    }

    public LocalDate next() {
        LocalDate result = nextDate;
        nextDate = nextDate.plusWeeks(1);
        return result;
    }

    public void remove() {
        throw new UnsupportedOperationException();
    }
 }

测试

import org.joda.time.DateTimeConstants;
import org.joda.time.LocalDate;

public class DayOfWeekIteratorTest {

    public static void main(String[] args) {

        LocalDate startDate = new LocalDate(2010, 12, 1);//1st Dec 2010
        LocalDate endDate = new LocalDate(2010, 12, 31);//31st Dec 2010
        DayOfWeekIterator it = new DayOfWeekIterator(startDate, endDate, DateTimeConstants.FRIDAY);
        while (it.hasNext()) {
            System.out.println(it.next());
        }

    }
}

答案 2 :(得分:0)

TL;博士

java.time.LocalDate.of( 2018 , Month.JANUARY , 23 )  // A date-only class in the modern *java.time* classes that supplant both Joda-Time and the troublesome old date-time classes.
    .with(  
        TemporalAdjusters.next( DayOfWeek.FRIDAY )   // Nifty `TemporalAdjuster` implementation for moving to another date. Immutable Objects pattern means a new object is returned based on the original which remains unmodified.
    )
    .isBefore(                                       // Compare `LocalDate` objects with `isBefore`, `isAfter`, and `isEqual`. 
        LocalDate.of( 2018 , Month.FEBRUARY , 27 );
    )

java.time

仅供参考,Joda-Time项目现在位于maintenance mode,团队建议迁移到java.time课程。

定义你的停止&amp;启动LocalDate个对象。

LocalDate start = LocalDate.of( 2018 , Month.JANUARY , 23 );
LocalDate stop = LocalDate.of( 2018 , Month.FEBRUARY , 27 );
// TODO: Verify start.isBefore( stop ).

收集我们发现的星期五日期。您可以通过调整集合大小来优化一点。

// Pre-size the collection.
int initialCapacity = ( int ) ( ChronoUnit.WEEKS.between( start , stop ) + 2 ); // Adding two for good measure.
List < LocalDate > fridays = new ArrayList <>( initialCapacity );

确定第一个星期五,使用开始日期,如果它本身是星期五。使用TemporalAdjuster课程中提供的一对TemporalAdjusters实施:next​(DayOfWeek)&amp; nextOrSame​(DayOfWeek)。通过DayOfWeek enum传递所需的星期几,七个预定义的对象,一个星期一到星期日的每一天。

LocalDate friday = start.with( TemporalAdjusters.nextOrSame( DayOfWeek.FRIDAY ) );

while ( friday.isBefore( stop ) )
{
    fridays.add( friday );  // Remember this Friday date.
    // Setup next loop.
    friday = friday.with( TemporalAdjusters.next( DayOfWeek.FRIDAY ) );
}

System.out.println( "From " + start + " to " + stop + " = " + fridays );
  

2018-01-23至2018-02-27 = [2018-01-26,2018-02-02,2018-02-09,2018-02-16,2018-02-23]

关于 java.time

java.time框架内置于Java 8及更高版本中。这些类取代了麻烦的旧legacy日期时间类,例如java.util.DateCalendar和&amp; SimpleDateFormat

现在位于Joda-Timemaintenance mode项目建议迁移到java.time类。

要了解详情,请参阅Oracle Tutorial。并搜索Stack Overflow以获取许多示例和解释。规范是JSR 310

您可以直接与数据库交换 java.time 对象。使用符合JDBC driver或更高版本的JDBC 4.2。不需要字符串,不需要java.sql.*类。

从哪里获取java.time类?

ThreeTen-Extra项目使用其他类扩展java.time。该项目是未来可能添加到java.time的试验场。您可以在此处找到一些有用的课程,例如IntervalYearWeekYearQuartermore