1

I'm trying to return a list of dates from these parameters :

LocalDate startDate
LocalDate endDate
Boolean monday ... sunday : booleans

For example :

startDate = 01/03/2016
endDate = 10/03/2016 (included)
Monday = true;
Tuesday = false;
Wednesday = false;
Thursday = true;
Friday = true;
Saturday = false;
Sunday = false;

List of dates returned : [03/03/2016, 04/03/2016, 07/03/2016, 10/03/2016]

Is there any libray i can use ? I've only managed to returned the dates between two dates, but i have no idea how to use the days also : (I'm using java 6)

public List<LocalDate> datesBetween(LocalDate start, LocalDate end) {
    List<LocalDate> ret = new ArrayList<LocalDate>();
    for (LocalDate date = start; !date.isAfter(end); date = date.plusDays(1)) {
        ret.add(date);
    }
    return ret;
}
redAce
  • 1,558
  • 4
  • 14
  • 24
  • Joda time is quite useful. Maybe you should try with that. It has a bunch of helpful methods. [Joda link](http://www.joda.org/joda-time/) – Jernej K Mar 10 '16 at 17:40
  • @JernejK The *java.time* project is now in maintenance-mode, with its creators advising migration to the *java.time* classes. See the bottom of [my Answer](https://stackoverflow.com/a/35925326/642706) for details. – Basil Bourque Apr 30 '18 at 21:19
  • @BasilBourque he was specifically asking about Java 6 at the time. I saw on several occasions how useful Joda time can be on older Java versions. The only reason they stopped development is the new features set in Java 8 - "Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project." – Jernej K May 04 '18 at 19:15
  • @JernejK My comment was a note to the reader, not a criticism. And as for Java 6 and 7, much of the *java.time* functionality is back-ported in the *ThreeTen-Backport* project, and further adapted for earlier Android in the *ThreeTenABP* project. – Basil Bourque May 04 '18 at 19:18

3 Answers3

0

Java SE 8 Date and Time

Listing 1

Standard Java getter conventions are used in order to obtain values from Java SE 8 classes, as shown in Listing 2.

LocalDate theDate = timePoint.toLocalDate();
Month month = timePoint.getMonth();
int day = timePoint.getDayOfMonth();
timePoint.getSecond();
0x02
  • 1
  • 4
0

The problem is the signature of your method. The fact that days are passed in as names makes it very hard to do anything generic; it means you will need lots of code to solve the problem. You may want to consider passing in an array of size 7, or a list of day names that correspond to 'true' (omitting the others).

If you were to pass in a set of integers ranging from 0 to 6, encoding that a day of the week is active if it is inside the set, you could iterate through your time range and check whether the day was passed as active, or not. You could use method like date.getDayOfWeek() and set.contains().

vinntec
  • 455
  • 1
  • 4
  • 11
0

Avoid old date-time classes

Avoid the old date-time classes bundled with the earliest versions of Java, such as java.util.Date/.Calendar. They are poorly designed, confusing, and troublesome.

java.time

The java.time framework built into Java 8 and later supplants the old date-time classes. See Tutorial.

For use with Java 6 & 7, check out the ThreeTen-Backport project ('ThreeTen' refers to JSR 310 defining java.time). For Android specifically, see the Android wrapper around the Backport, ThreeTenABP.

The new classes include LocalDate for a date-only value without time-of-day and without time zone. Another handy class you need is the enum DayOfWeek. Use these objects rather than strings to make your code type-safe, self-documenting, and localization-ready.

Reminder: Java provides special high-performance Set and Map implementations for enums, EnumSet and EnumMap.

EnumSet<DayOfWeek> dayOfWeekSet = EnumSet.of( DayOfWeek.TUESDAY , DayOfWeek.THURSDAY );

Define a date interval.

LocalDate start = LocalDate.of( 2016 , Month.MARCH , 1 );
LocalDate stop = start.plusDays( 10 );

Define a collection to keep our targeted dates.

List<LocalDate> localDates = new ArrayList<>();

Loop the days in that interval. In date-time work we commonly use the Half-Open approach where the beginning in inclusive while the ending is exclusive.

LocalDate localDate = start;
while ( localDate.isBefore( stop ) ) {
    if ( dayOfWeekSet.contains ( localDate.getDayOfWeek() ) ) {
        localDates.add( localDate );
    }
    // Prepare for next loop.
    localDate = localDate.plusDays( 1 );
}

Dump to console.

System.out.println( "From: " + start + " to: " + stop + " the dates for days-of-week: " + dayOfWeekSet + " = " + localDates );

From: 2016-03-01 to: 2016-03-11 the dates for days-of-week: [TUESDAY, THURSDAY] = [2016-03-01, 2016-03-03, 2016-03-08, 2016-03-10]


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • That's a great solution, but since i HAVE to use LocalDate and java 6, i'm a bit stuck. Is it still possible to use your solution with the old java.util.Date ? – redAce Mar 10 '16 at 19:35
  • @user3557384 I am confused… There is no `LocalDate` in Java 6. – Basil Bourque Mar 10 '16 at 19:38
  • Jodatime's LocalDate – redAce Mar 15 '16 at 08:38
  • 1
    @redAce See my edits, the "About java.time" section at bottom. Much of the *java.time* functionality was back-ported to Java 6 & Java 7 in the *ThreeTen-Backport* project. Further adapted for earlier releases of Android in the *ThreeTenABP* project. Do *not* use `java.util.Date`, it is a terribly troublesome class that was replaced by `java.time.Instant`. As for Joda-Time, that project is now in maintenance mode. The *java.time* classes are its official replacement, both efforts being led by the same man, Stephen Colebourne. – Basil Bourque Apr 30 '18 at 21:14