empire Geschrieben 17. Oktober 2003 Geschrieben 17. Oktober 2003 Hallo... Ich bin auf der Suche, nach einer Art "Interval Klasse" die mir ein Datum und Uhrzeit zurück gibt. Mit den Werten möchte ich dann ein Timer setzen. Timer setzen und so das hab ich alles schon zum laufen gebracht. Angenommen ich möchte jeden 1 Montag im Monat eine Aktion ausführen. Dann brauch ich das Datum des nächsten ersten Montages im kommenden Monat. Das meine ich mit Intervall, nicht eine genaue Datums Angabe, sondern die mir das Datum ausrechnet. Danke
PerdianMG Geschrieben 17. Oktober 2003 Geschrieben 17. Oktober 2003 Ich kenne hier keine bekannten Intervall-Klassen, aber im Grunde genommen musst du dich mal genauer mit java.util.Calendar befassen, der bietet im Grunde genommen alle Methoden, die du brauchst. Als kleiner Einstieg mal ein paar Methoden, die ich zu dem Thema schon geschrieben habe: public class MiscUtil { // ... /** * Gets the date that fits into the given hour time the next time * @param hour * the hour of the time * @param minute * the minute of the time */ public static Calendar getNextDateForTime(int hour, int minute) { return MiscUtil.getNextDateForTime(hour, minute, 0); } /** * Gets the date that fits into the given hour time the next time * @param hour * the hour of the time * @param minute * the minute of the time * @param seconds * the seconds of the time */ public static Calendar getNextDateForTime(int hour, int minute, int seconds) { if(hour < 0) { Calendar newDate = Calendar.getInstance(); newDate.set(Calendar.MINUTE, minute); newDate.set(Calendar.SECOND, minute); if(newDate.getTime().getTime() < System.currentTimeMillis()) { newDate.add(Calendar.HOUR_OF_DAY, 1); } return newDate; } else { Calendar currentDayDate = MiscUtil.getTimeCurrentDay(hour, minute, seconds); if(currentDayDate.getTime().getTime() < System.currentTimeMillis()) { return MiscUtil.getTimeNextDay(hour, minute, seconds); } else { return currentDayDate; } } } /** * Gets the specified time at the next day * @param hour * the hour of the time * @param minute * the minute of the time */ public static Calendar getTimeNextDay(int hour, int minute) { return MiscUtil.getTimeNextDay(hour, minute, 0); } /** * Gets the specified time at the next day * @param hour * the hour of the time * @param minute * the minute of the time * @param seconds * the seconds of the time */ public static Calendar getTimeNextDay(int hour, int minute, int seconds) { Calendar rightNow = Calendar.getInstance(); // Jump to next day rightNow.set(Calendar.DAY_OF_YEAR, rightNow.get(Calendar.DAY_OF_YEAR) + 1); rightNow.set(Calendar.HOUR_OF_DAY, hour); rightNow.set(Calendar.MINUTE, minute); rightNow.set(Calendar.SECOND, seconds); return rightNow; } /** * Gets the specified time at the current day * @param hour * the hour of the time * @param minute * the minute of the time */ public static Calendar getTimeCurrentDay(int hour, int minute) { return MiscUtil.getTimeCurrentDay(hour, minute, 0); } /** * Gets the specified time at the current day * @param hour * the hour of the time * @param minute * the minute of the time * @param seconds * the seconds of the time */ public static Calendar getTimeCurrentDay(int hour, int minute, int seconds) { Calendar rightNow = Calendar.getInstance(); // Jump to next day rightNow.set(Calendar.HOUR_OF_DAY, hour); rightNow.set(Calendar.MINUTE, minute); rightNow.set(Calendar.SECOND, seconds); return rightNow; } /** * Gets the next hour that is approching as <code>Date</code> object */ public static Calendar getNextHour() { Calendar rightNow = Calendar.getInstance(); int currentHour = rightNow.get(Calendar.HOUR_OF_DAY); rightNow.set(Calendar.HOUR_OF_DAY, currentHour + 1); rightNow.set(Calendar.MINUTE, 0); rightNow.set(Calendar.SECOND, 0); return rightNow; } } Willst du beispielweise das Runnable Object R1 zur nächsten vollen Stunde starten lassen bietet sich folgendes Konstrukt an: Timer timer = new Timer(); TimerTask myTimerTask = new IrgendeinTimerTask(); timer.schedule(myTimerTask, MiscUtil.getNextHour().getTime()); Ciao Chris
Empfohlene Beiträge
Erstelle ein Benutzerkonto oder melde Dich an, um zu kommentieren
Du musst ein Benutzerkonto haben, um einen Kommentar verfassen zu können
Benutzerkonto erstellen
Neues Benutzerkonto für unsere Community erstellen. Es ist einfach!
Neues Benutzerkonto erstellenAnmelden
Du hast bereits ein Benutzerkonto? Melde Dich hier an.
Jetzt anmelden