7

我第一次在 Spring 3.0 项目中使用 Joda time api。现在我有一个开始和结束日期,我想获取这两个日期之间所有星期一的日期。我怎样才能做到这一点 ?

我不知道从哪里开始,有人可以建议。我看了这篇文章Joda Time: How to get dates of weekdays on some date interval? 它提供了某种指导,但由于对 joda 的经验很少,它仍然有些模糊。

4

6 回答 6

16
LocalDate startDate = new LocalDate(2011, 11, 8);
LocalDate endDate = new LocalDate(2012, 5, 1);

LocalDate thisMonday = startDate.withDayOfWeek(DateTimeConstants.MONDAY);

if (startDate.isAfter(thisMonday)) {
    startDate = thisMonday.plusWeeks(1); // start on next monday
} else {
    startDate = thisMonday; // start on this monday
}

while (startDate.isBefore(endDate)) {
    System.out.println(startDate);
    startDate = startDate.plusWeeks(1);
}
于 2011-11-08T02:06:14.640 回答
5

我最近开发了Lamma,旨在解决这个确切的用例:

Dates.from(2011, 11, 8).to(2011, 12, 30).byWeek().on(DayOfWeek.MONDAY).build();

你会得到一个List<Date>

Date(2011,11,14)
Date(2011,11,21)
Date(2011,11,28)
Date(2011,12,5)
Date(2011,12,12)
Date(2011,12,19)
Date(2011,12,26)
于 2014-06-27T18:24:01.253 回答
4
于 2017-02-07T22:00:32.623 回答
2

此代码采用字符串日期并给出星期日的数量以及所有星期日的日期

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class FindAllSundays {


public static int getNumberofSundays(String d1, String d2) throws Exception { // object
                                                                                // in
                                                                                // Date
                                                                                // form

    Date date1 = getDate(d1);
    Date date2 = getDate(d2);

    Calendar c1 = Calendar.getInstance();
    c1.setTime(date1);
    Calendar c2 = Calendar.getInstance();
    c2.setTime(date2);
    int sundays = 0;
    while (c2.after(c1)) {
        // System.out.println(" came here ");
        //checks to see if the day1 ....so on next days are sundays if sunday goes inside to increment the counter
        if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            System.out.println(c1.getTime().toString() + " is a sunday ");
            sundays++;

        }
        c1.add(Calendar.DATE, 1);
    }

    System.out.println("number of sundays between 2 dates is " + sundays);

    return sundays;
}
// converts string to date 
public static Date getDate(String s) {
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Date date = null;
    try {
        date = format.parse(s);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return date;
}

public static void main(String[] arg) throws Exception {
    System.out.println(" " + getNumberofSundays("2005-10-07", "2006-10-01"));
}

}

于 2014-12-17T05:00:05.267 回答
1
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class Get_time {

    public  ArrayList<LocalDate> getmondays(String s,String e)
    {
        LocalDate start = LocalDate.parse(s);
        LocalDate end = LocalDate.parse(e);
        List<LocalDate> totalDates_Mondays = new ArrayList<>();

        while (!start.isAfter(end)) {
            totalDates_Mondays.add(start);
            start = start.plusWeeks(1);
        }
        return (ArrayList<LocalDate>) totalDates_Mondays;
    }

    public static void main(String ...s1) {

        String mon_start = "1600-08-01";
        String mon_end= "2016-12-29";
        Get_time t=new Get_time();
        System.out.println(t.getmondays(mon_start,mon_end));
    }
}
于 2016-08-05T11:12:44.983 回答
0

在 Java 8 中使用 Stream ,

LocalDate startDate = LocalDate.of(2019, 2, 1);
LocalDate endDate = LocalDate.of(2019, 2, 28);
long numOfDays = ChronoUnit.DAYS.between(startDate, endDate);
List<LocalDate> daysRange = Stream.iterate(startDate, date -> date.plusDays(1)).limit(numOfDays).filter( date -> date.getDayOfWeek()==DayOfWeek.MONDAY ).collect(Collectors.toList());
于 2019-07-02T16:32:19.370 回答