Avoid old date-time classes
Avoid using the troublesome old legacy date-time classes. Instead use the java.time classes.
DayOfWeek
enum & EnumMap
The DayOfWeek
enum has seven objects predefined for each of the seven days of the week, Monday-Sunday.
EnumSet
and EnumMap
are very fast and low-memory implementations of Set
and Map
for handling a collection of enum values. In your case you could define an EnumMap
with all seven days, each mapping to a LocalTime
for when alarm should fire (or null for no alarm that day).
EnumMap< DayOfWeek , LocalTime > dowToTimeMap = new EnumMap< DayOfWeek , LocalTime >( DayOfWeek.class );
dowToTimeMap.put( DayOfWeek.MONDAY , LocalTime.of( 6 , 0 ) ); // 6 AM.
dowToTimeMap.put( DayOfWeek.TUESDAY , LocalTime.of( 6 , 0 ) );
…
dowToTimeMap.put( DayOfWeek.SATURDAY , LocalTime.of( 8 , 0 ) ); // Sleep in until 8 AM on Saturday.
dowToTimeMap.put( DayOfWeek.SUNDAY , LocalTime.of( 7 , 0 ) );
To get the current date, specify a time zone (ZoneId
). The LocalDate
class represents a date-only value without time-of-day and without time zone.
ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );
Ask the map for the alarm time for today’s day-of-week.
LocalTime alarmTimeToday = dowToTimeMap.get( today.getDayOfWeek() );
You may find the ZonedDateTime
, Instant
, and Duration
classes useful for determining elapsed time until next alarm.
ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );
LocalDate tomorrow = today.plusDays( 1 );
LocalTime alarmTimeTomorrow = dowToTimeMap.get( tomorrow.getDayOfWeek() );
ZonedDateTime nextAlarmZdt = ZonedDateTime.of( tomorrow , alarmTimeTomorrow , z );
Instant now = Instant.now();
Duration duration = Duration.between( now , nextAlarmZdt.toInstant() );
Check to see if the duration is negative, meaning going backwards in time. That would mean the next alarm time was either before now, or so close as to expire by the time this code ran.
if( duration.isNegative() ) {
// Oops, the next alarm was within a second or so of current time, a shorter time than it took to execute this code.
… // Handle the problem.
}
You could also verify today and tomorrow, as that could shift if this code executes at the moment of midnight.
You can then interrogate the Duration
for a number of whole seconds or milliseconds or nanoseconds, whatever you need for your alarm setting.
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.