tl;dr
Parse that input string into an EnumSet
of DayOfWeek
constants to represent the days of the week when the bus runs. Ask if the set contains the day-of-week for any particular date.
Set<DayOfWeek> busRunDays = EnumSet. …
…
Boolean busRunsOn20161009 =
busRunDays.contains(
DayOfWeek.from( LocalDate.of( 2016 , Month.OCTOBER , 9 ) )
)
;
LocalDate
The LocalDate
class represents a date-only value without time-of-day and without time zone.
A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );
DayOfWeek
Java includes the DayOfWeek
enum defining seven instances as constants, one for each day of the week.
DayOfWeek dow = DayOfWeek.from( ld );
You can test for equality.
if( dow.equals( DayOfWeek.TUESDAY ) ) …
They are numbered and sorted 1-7 for Monday to Sunday, per the ISO 8601 standard.
EnumSet
The EnumSet
class is a very efficient implementation of Set
for holding enum-defined objects. Represented internally as bit vectors, an EnumSet
takes very little memory and executes very quickly.
Loop through your input string characters, and if a 1
add that day of week to the set.
Set<DayOfWeek> busRunDays = EnumSet.noneOf( DayOfWeek.class );
…
busRunDays.add( DayOfWeek.SUNDAY );
Elsewhere in your code you ask if a day of the week is in the set.
if( busRunDays.contains( DayOfWeek.WEDNESDAY ) ) …
Example code, all put together and ready to run (at your own risk, no guarantees).
Set<DayOfWeek> busRunDays = EnumSet.noneOf ( DayOfWeek.class );
String input = "1101011";
DayOfWeek[] inputWeek = { DayOfWeek.SUNDAY , DayOfWeek.MONDAY , DayOfWeek.TUESDAY , DayOfWeek.WEDNESDAY , DayOfWeek.THURSDAY , DayOfWeek.FRIDAY , DayOfWeek.SATURDAY }; // Define the ordering of days in a non-standard week.
int i = 0;
for ( char c : input.toCharArray () ) {
if ( c == '1' ) {
DayOfWeek dow = inputWeek[ i ];
busRunDays.add ( dow );
}
i = ( i + 1 ); // Prepare for next loop.
}
Dump to console.
System.out.println ( "input: " + input + " | busRunDays: " + busRunDays );
input: 1101011 | busRunDays: [MONDAY, WEDNESDAY, FRIDAY, SATURDAY, SUNDAY]
You can use the same sort of logic if you need to write out strings in your 1101011
format.
Test if the bus runs today.
LocalDate today = LocalDate.now( ZoneId.of( "America/Montreal" ) ) ;
Boolean busRunsToday = busRunDays.contains( DayOfWeek.from( today ) ) ;
Test if the bus runs on a particular date. This specifically answers the Question.
LocalDate ld = LocalDate.of( 2016 , Month.OCTOBER , 9 ) ;
Boolean busRunsOnDate = busRunDays.contains( DayOfWeek.from( ld ) ) ;
I strongly suggest you get in the habit of passing around these DayOfWeek
and Set
objects rather than using mere digits like 1-7 and strings like your 1101011
. Doing so provides type-safety, ensures valid values, eliminates ambiguity, and makes your code more self-documenting.
Tip: Life is easier if you and your users stick with the standard Monday-Sunday week like much of the world. But I know that change can be challenging, especially for us Americans. So be sure to carefully document in your code that the 1101011
input values use non-standard Sun-Mon week.
About java.time
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.
Where to obtain the java.time classes?
- Java SE 8 and SE 9 and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
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.