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

问题描述 投票:3回答:3

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

java datetime date jodatime
3个回答
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());
        }

    }
}

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);

        }


    }
}

0
投票

tl;dr

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班。

定义你的停止和启动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)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]


About java.time

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

现在在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

© www.soinside.com 2019 - 2024. All rights reserved.