📄 daytimer.java
字号:
/*************************************************************************** * Copyright 2006-2008 by Christian Ihle * * kontakt@usikkert.net * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/package net.usikkert.kouchat.util;import java.util.Calendar;import java.util.Date;import java.util.Timer;import java.util.TimerTask;import net.usikkert.kouchat.misc.MessageController;import net.usikkert.kouchat.ui.UserInterface;/** * Notifies the user interface when the day changes. * Checks every hour, in case daylight saving changes the time. * * @author Christian Ihle */public class DayTimer extends TimerTask{ /** * Which hour of the day the timer should notify about * day change. */ private static final int NOTIFY_HOUR = 0; /** * How often the timer should check if the day has changed, * in milliseconds. Currently set to 1 hour. */ private static final long TIMER_INTERVAL = 1000 * 60 * 60; private final MessageController msgController; private boolean done; /** * Constructor. Starts the timer. * * @param ui The user interface. */ public DayTimer( final UserInterface ui ) { msgController = ui.getMessageController(); Calendar cal = Calendar.getInstance(); cal.set( Calendar.HOUR_OF_DAY, NOTIFY_HOUR ); cal.set( Calendar.MINUTE, 0 ); cal.set( Calendar.SECOND, 0 ); Timer timer = new Timer( "DayTimer" ); timer.scheduleAtFixedRate( this, new Date( cal.getTimeInMillis() ), TIMER_INTERVAL ); } /** * This method is run by the timer every hour, and * compares the current time against the time when * the day changes. */ @Override public void run() { int hour = Calendar.getInstance().get( Calendar.HOUR_OF_DAY ); // Needs an extra check, so the message only shows once a day. if ( hour == NOTIFY_HOUR && !done ) { String date = Tools.dateToString( null, "EEEE, d MMMM yyyy" ); msgController.showSystemMessage( "Day changed to " + date ); done = true; } else if ( hour != NOTIFY_HOUR && done ) { done = false; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -