2

I am trying to make an alarm clock app and am having difficulty setting the time for the alarm to go off. My app has the user select which days they would like the alarm to go off, similar to the android alarm clock, with a boolean array of seven to represent this.

My idea was to set the clock for the closest day i.e if today is Monday and the alarm is set for Wednesday and Friday, the alarm will go off on Wednesday and then reset itself for Friday.

I was wondering if the calendar could return a week, I think this would make things easier.

user3074140
  • 733
  • 3
  • 13
  • 30

2 Answers2

3

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

You can get the current day and month like this:

Calendar c = Calendar.getInstance();
int day = c.get(Calendar.DAY_OF_MONTH);
int month = c.get(Calendar.MONTH);

and the day of the week (like Monday, Tuesday ... ) with

int day = c.get(Calendar.DAY_OF_WEEK);

http://developer.android.com/reference/java/util/Calendar.html#DAY_OF_WEEK

Mihai Coman
  • 73
  • 1
  • 9