Example usage for java.time LocalDateTime minusYears

List of usage examples for java.time LocalDateTime minusYears

Introduction

In this page you can find the example usage for java.time LocalDateTime minusYears.

Prototype

public LocalDateTime minusYears(long years) 

Source Link

Document

Returns a copy of this LocalDateTime with the specified number of years subtracted.

Usage

From source file:rmblworx.tools.timey.gui.AlarmIntegrationTest.java

/**
 * Testet// w  ww . j  a  va 2  s  .  co  m
 * <ol>
 * <li>Anlegen eines Alarms per GUI,</li>
 * <li>Senden des Ereignisses zum Auslsen des Alarms per Backend und</li>
 * <li>Reaktion der GUI auf das Ereignis.</li>
 * </ol>
 */
@Test
@Ignore("schlgt derzeit fehl") // TODO
public final void testCreateAlarmAndHandleEvent() {
    /*
     * Zeitdifferenz, die der Alarm in der Zukunft ausgelst werden soll.
     * Wert mglichst klein halten, um Test nich unntig lange dauern zu lassen, aber dennoch gro genug, um sicherzustellen, dass
     * Alarm bis zu diesem Zeitpunkt vollstndig angelegt werden kann.
     */
    final int bufferInSeconds = 2;

    final String alarmDescription = "relevanter Alarm";

    final AlarmController controller = (AlarmController) getController();
    final GuiHelper guiHelper = controller.getGuiHelper();
    final ITimey facade = guiHelper.getFacade();

    // zwei unwichtige Alarme per Fassade hinzufgen
    final LocalDateTime now = LocalDateTime.now().withNano(0);
    facade.setAlarm(AlarmDescriptorConverter
            .getAsAlarmDescriptor(new Alarm(now.minusYears(1), "alter Alarm", "", false)));
    facade.setAlarm(
            AlarmDescriptorConverter.getAsAlarmDescriptor(new Alarm(now.plusYears(1), "Zukunftsalarm")));

    // relevanten Alarm per GUI hinzufgen
    final Button alarmAddButton = (Button) stage.getScene().lookup("#alarmAddButton");
    click(alarmAddButton);
    waitForThreads();

    final Scene dialogScene = controller.getDialogStage().getScene();
    final TimePicker alarmTimePicker = (TimePicker) dialogScene.lookup("#alarmTimePicker");
    final TextField alarmDescriptionTextField = (TextField) dialogScene.lookup("#alarmDescriptionTextField");
    final Button alarmSaveButton = (Button) dialogScene.lookup("#alarmSaveButton");

    Platform.runLater(new Runnable() {
        public void run() {
            alarmTimePicker.setValue(alarmTimePicker.getValue().plusSeconds(bufferInSeconds));
            alarmDescriptionTextField.setText(alarmDescription);
        }
    });
    FXTestUtils.awaitEvents();

    final MessageHelper messageHelper = mock(MessageHelper.class);
    guiHelper.setMessageHelper(messageHelper);

    // Speichern-Schaltflche bettigen
    click(alarmSaveButton);
    waitForThreads();

    // sicherstellen, dass Ereignis genau einmal fr den korrekten Alarm eintritt und verarbeitet wird
    verify(messageHelper, timeout(WAIT_FOR_EVENT)).showTrayMessageWithFallbackToDialog(anyString(),
            eq(alarmDescription), isNull(TrayIcon.class), isA(ResourceBundle.class));
}

From source file:org.ambraproject.rhino.rest.controller.ArticleCrudController.java

/**
 * Calculate the date range using the specified rule. For example:
 *
 * <ul>/*from ww w  . j a v a 2  s .c o  m*/
 * <li>sinceRule=2y  - 2 years</li>
 * <li>sinceRule=5m  - 5 months</li>
 * <li>sinceRule=10d - 10 days</li>
 * <li>sinceRule=5h  - 5 hours</li>
 * <li>sinceRule=33  - 33 minutes</li>
 * </ul>
 *
 * The method will result in a {@link java.util.Map Map} containing the following keys:
 *
 * <ul>
 * <li><b>fromDate</b> - the starting date
 * <li><b>toDate</b> - the ending date, which will be the current system date (i.e. now())
 * </ul>
 *
 * @param sinceRule The rule to calculate the date range
 *
 * @return A {@link java.util.Map Map}
 */
public static final Map<String, LocalDateTime> calculateDateRange(String sinceRule) {
    if (StringUtils.isBlank(sinceRule)) {
        return ImmutableMap.of();
    }

    final String timeDesignation = StringUtils.right(sinceRule, 1);
    long timeDelta = 0;
    try {
        // Assume last character is NOT a letter (i.e. all characters are digits).
        timeDelta = Long.parseLong(sinceRule);
    } catch (NumberFormatException exception) {
        // If an exception, then last character MUST have been a letter,
        // so we now exclude the last character and re-try conversion.
        try {
            timeDelta = Long.parseLong(sinceRule.substring(0, sinceRule.length() - 1));
        } catch (NumberFormatException error) {
            log.warn("Failed to convert {} to a timeDelta/timeDesignation!", sinceRule);
            timeDelta = 0;
        }
    }

    if (timeDelta < 1) {
        return ImmutableMap.of();
    }

    final LocalDateTime toDate = LocalDateTime.now();
    final LocalDateTime fromDate;
    if (timeDesignation.equalsIgnoreCase("y")) {
        fromDate = toDate.minusYears(timeDelta);
    } else if (timeDesignation.equalsIgnoreCase("m")) {
        fromDate = toDate.minusMonths(timeDelta);
    } else if (timeDesignation.equalsIgnoreCase("d")) {
        fromDate = toDate.minusDays(timeDelta);
    } else if (timeDesignation.equalsIgnoreCase("h")) {
        fromDate = toDate.minus(timeDelta, ChronoUnit.HOURS);
    } else {
        fromDate = toDate.minus(timeDelta, ChronoUnit.MINUTES);
    }

    final ImmutableMap<String, LocalDateTime> dateRange = ImmutableMap.of(FROM_DATE, fromDate, TO_DATE, toDate);
    return dateRange;
}