Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Sucher Interval Klasse in zu sammenhang mit Timer

Empfohlene Antworten

Veröffentlicht

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

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

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.