问题
Interval interval = new Interval(new DateTime("2014-01-01"), new DateTime("2014-01-30"));
Is there a way to get the weekend days numbers?
something like:
interval.toPeriod.getWeekendDays();
回答1:
Custom Iterator
WeekendDayIterator
is an Iterator of the weekend days in the Interval specified:
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))
Resulting in the following output:
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)
The code is written in Scala. Porting it to Java should be straightforward.
回答2:
Here is a Java implementation using Joda-Time API. You basically figure out when is the first weekend from the start date, then add 7 days and keep going in a loop till the end date. You will collect all the weekends. The trick is if the end date is on Saturday, you have to stop there.
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));
}
}
If you want all the weekends from today (04-16-2016) and 45 days from now, it will give output of
[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:
package com.test.tryings;
import java.util.ArrayList; import java.util.HashMap; import java.util.List;
import org.joda.time.DateTime; import org.joda.time.DateTimeConstants; import org.joda.time.Days; import org.joda.time.LocalDate; import org.joda.time.format.DateTimeFormat;
public class DateTest {
org.joda.time.format.DateTimeFormatter dateStringFormat = DateTimeFormat.forPattern("dd-MM-yyyy"); String firstDate = "13-12-2019"; String 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 end = dateStringFormat.parseDateTime(seconddate); for (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("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() == DateTimeConstants.SATURDAY || currentdate.getDayOfWeek() == DateTimeConstants.SUNDAY) { continue; } else { System.out.println(currentdate.toString(dateStringFormat)); } } System.out.println("Exclude weekends for the matches..."); }
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() == DateTimeConstants.SATURDAY || currentdate.getDayOfWeek() == DateTimeConstants.SUNDAY) { System.out.println(currentdate.toString(dateStringFormat)); } } System.out.println("Exclude weekdays for the matches...");
}
public static void main(String[] args) {
DateTest dt = new DateTest(); dt.noOfDays(); dt.excludeWeekEnds(); dt.excludeWeekDays(); } }
来源:https://stackoverflow.com/questions/21972361/how-to-get-weekends-in-an-interval-joda-time