/**Copyright 2010 Carlos Rincón** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at** http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package jalarmv;import java.util.Calendar;import javax.swing.JOptionPane;/**** @author Carlos Eduardo Hernández Rincón* @fecha 10/07/2010 06:26:54 PM* @universidad Universidad del Valle de México Campus Toluca* @materia --* @tema --*/public class ClockManagement {//Tanto alarmaHoras como alarmaMinutos permite tratar la hora como entero, que aunque no// está implementado aún, permite hacer la modularizaciónprivate String horaDeAlarma;private int alarmaHoras;private int alarmaMinutos;//Definiendo este constructor se forza a siempre inicializar la hora de la alarma en un formato de// HH:MM en 24 horas (no formato de 12 horas).public ClockManagement(String horaAlarma) {setHoraDeAlarma(horaAlarma);}private void setHoraDeAlarma(String horaDeAlarma) {this.horaDeAlarma = horaDeAlarma;}//Este método sí está como público y además static porque la hora será siempre la misma para todos // los objetos instanciados, además de que permite obtener la hora del sistema de una manera más // amigable incluso para aquellos que no deseen instanciar una alarmapublic static String getDate() {String fecha;Calendar hora = Calendar.getInstance();fecha = hora.get(Calendar.HOUR_OF_DAY) + ":" + hora.get(Calendar.MINUTE);return fecha;}//Se debe tener muchísimo cuidado al manejar este método (el único público de la clase) ya que se // usa una pausa de hilo dentro de un ciclo y si no se implementa correctamente se puede caer en // un bucle infinito del cual no podremos terminar nuestro proceso.//Este es el método que comienza la cuenta regresivapublic void esperarLaHora() throws InterruptedException {boolean yaEsHora = false;while (yaEsHora == false) {if (getDate().equals(horaDeAlarma)) {yaEsHora = true;} else if ((horaActualMinutos() > alarmaMinutos) && (horaActualHoras() == alarmaHoras)) {yaEsHora = true;} else {Thread.sleep(1000 * 20);}}llamarAlarma();}private int horaActualMinutos() {String fecha;Calendar hora = Calendar.getInstance();fecha = hora.get(Calendar.HOUR_OF_DAY) + ":" + hora.get(Calendar.MINUTE);int minutos = Integer.parseInt(fecha.substring(3));return minutos;}private void setAlarmaMinutos() {this.alarmaMinutos = Integer.parseInt(horaDeAlarma.substring(3));}private int horaActualHoras() {String fecha;Calendar hora = Calendar.getInstance();fecha = hora.get(Calendar.HOUR_OF_DAY) + ":" + hora.get(Calendar.MINUTE);int horas = Integer.parseInt(fecha.substring(0, 2));return horas;}private void setAlarmaHoras() {this.alarmaHoras = Integer.parseInt(horaDeAlarma.substring(0, 2));}//En este método va todo el bloque de código que quieren que se ejecute cuando la alarma se// dispareprivate void llamarAlarma() {JOptionPane.showMessageDialog(null, "Ya es hora señor, son las\n\n" + getDate(), "LA ALARMA", JOptionPane.WARNING_MESSAGE);}}