Reputation: 1211
I have a requirement to show a list of dates between the start and end dates in my app. I have a list of checkboxes which have the days of the week beside them ie; Monday to Sunday and two date pickers which helps the user to select a start and end date. If a user doesn't select any checkbox containing the days of the week, I need to display all the dates alongwith the days of the week coming in between the start and end date. If a user selects some checkboxes ie; Monday, Wednesday, Friday I need to show only those dates and days of the week between the start and end date. Could you please let me know if there is any way in which this can be achieved? Thank you.
Upvotes: 1
Views: 3169
Reputation: 338496
The java.time classes supplant the troublesome old date-time classes bundled with earliest versions of Java.
LocalDate
The LocalDate
class represents a date-only value without time-of-day and without time zone.
Define your start and stop dates. Then loop to populate your list of all possible dates.
LocalDate start = LocalDate( 2016 , 1 , 2 );
LocalDate stop = start.plusWeeks( 1 );
List<LocalDate> allDates = new ArrayList<>();
LocalDate ld = start ;
while( ld.isBefore( stop ) ) {
allDates.add( ld );
// Set up next loop.
ld = ld.plusDays( 1 );
}
DayOfWeek
The DayOfWeek
enum defines objects to represent each of the seven days of the week, Monday-Sunday.
The EnumSet
in Java is an extremely fast and low-memory implementation of Set
to represent a collection of enum objects. As the user enables/disables the day-of-week checkboxes, redefine your EnumSet
collection of DayOfWeek
values.
for( DayOfWeek dow in DayOfWeek.values() ) {
if ( … ) { // if your checkbox is checked for this day-of-week
dows.add( dow );
}
}
Next, define a collection of dates from which we allow the user to choose. This collection backs the user-interface widget.
List<LocalDate> dates = new ArrayList<>();
Now loop all the possible dates, testing each one’s day-of-week against the user-selected days.
for( LocalDate localDate in allDates ) {
if( dows.contains( localDate.getDayOfWeek() ) ) {
dates.add( localDate );
}
}
To populate your user-interface widget, you will likely need Strings to represent the value of each LocalDate
object. You can create such Strings with an explicit formatting pattern you define. But I suggest letting the DateTimeFormatter
class localize automatically.
Locale l = Locale.CANADA_FRENCH; // Defines how to create the string: translation, abbreviation, punctuation, order-of-parts, etc.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.MEDIUM ).withLocale( l );
Loop your dates you want to present, generating a String for each one’s presentation.
String output = localDate.format( f );
I would expect there are some clever ways to optimize the code shown above. For example:
EnumSet
and look at each of the checkboxes, you could add or remove individual DayOfWeek
objects for just the one single checkbox checked/unchecked by the user.days
list on every user-interaction, you could probably scrounge up a List
implementation that acts as a mask over the allDates
list with a feature to include/exclude certain elements.But test real-world performance before prematurely optimizing. I suspect that any savings in execution time would be insignificant.
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date
, .Calendar
, & java.text.SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).
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.
Update: The Joda-Time project, now in maintenance mode, advises migration to java.time classes.
Write a loop using the Joda-Time library with its LocalDate (no time of day) class.
To get you started…
LocalDate start = new LocalDate( year, month, day );
LocalDate next = start.plusDays( 1 );
int dayOfWeek = next.getDayOfWeek();
if ( dayOfWeek == DateTimeConstants.MONDAY ) { // Add to list };
Upvotes: 0
Reputation: 5764
This can give you a list of the dates between two dates
List<Calendar> datelist = new ArrayList<Calendar>();
Calendar cStart = Calendar.getInstance();
cStart.set(2014, 04, 01);
Calendar cEnd = Calendar.getInstance();
cEnd.set(2014, 05, 2);
while(cStart.compareTo(cEnd) < 1){
datelist.add(cStart);
cStart.add(Calendar.DAY_OF_MONTH, 1);
}
msg = String.valueOf(datelist.size());
Upvotes: 1