Java Code Examples for java.time.LocalDateTime#minusSeconds()

The following examples show how to use java.time.LocalDateTime#minusSeconds() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: j2objc   File: TCKLocalDateTime.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void test_minusSeconds_one() {
    LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDate d = t.toLocalDate().minusDays(1);

    int hour = 0;
    int min = 0;
    int sec = 0;

    for (int i = 0; i < 3700; i++) {
        t = t.minusSeconds(1);
        sec--;
        if (sec == -1) {
            min--;
            sec = 59;

            if (min == -1) {
                hour--;
                min = 59;

                if (hour == -1) {
                    hour = 23;
                }
            }
        }

        assertEquals(t.toLocalDate(), d);
        assertEquals(t.getHour(), hour);
        assertEquals(t.getMinute(), min);
        assertEquals(t.getSecond(), sec);
    }
}
 
Example 2
@Test(dataProvider="minusSeconds_fromZero")
public void test_minusSeconds_fromZero(int seconds, LocalDate date, int hour, int min, int sec) {
    LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDateTime t = base.minusSeconds(seconds);

    assertEquals(date, t.toLocalDate());
    assertEquals(hour, t.getHour());
    assertEquals(min, t.getMinute());
    assertEquals(sec, t.getSecond());
}
 
Example 3
@Test(dataProvider="minusSeconds_fromZero")
public void test_minusSeconds_fromZero(int seconds, LocalDate date, int hour, int min, int sec) {
    LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDateTime t = base.minusSeconds(seconds);

    assertEquals(date, t.toLocalDate());
    assertEquals(hour, t.getHour());
    assertEquals(min, t.getMinute());
    assertEquals(sec, t.getSecond());
}
 
Example 4
@Test
public void test_toEpochSecond_beforeEpoch() {
    LocalDateTime ldt = LocalDateTime.of(1970, 1, 1, 0, 0).plusHours(1);
    for (int i = 0; i < 100000; i++) {
        ZonedDateTime a = ZonedDateTime.of(ldt, ZONE_PARIS);
        assertEquals(a.toEpochSecond(), -i);
        ldt = ldt.minusSeconds(1);
    }
}
 
Example 5
Source Project: jfairy   File: DateProducer.java    License: Apache License 2.0 5 votes vote down vote up
public LocalDateTime randomDateInThePast(int maxYearsEarlier) {
	checkArgument(maxYearsEarlier >= 0, "%s has to be >= 0", maxYearsEarlier);
	LocalDateTime currentDate = timeProvider.getCurrentTime();
	LocalDateTime latestDateInThePast = currentDate.minusSeconds(SECONDS_BEFORE_TO_BE_IN_THE_PAST);
	LocalDateTime maxYearsEarlierDate = currentDate.minusYears(maxYearsEarlier);
	return randomDateBetweenTwoDates(maxYearsEarlierDate, latestDateInThePast);
}
 
Example 6
@Test(dataProvider="minusSeconds_fromZero")
public void test_minusSeconds_fromZero(int seconds, LocalDate date, int hour, int min, int sec) {
    LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDateTime t = base.minusSeconds(seconds);

    assertEquals(date, t.toLocalDate());
    assertEquals(hour, t.getHour());
    assertEquals(min, t.getMinute());
    assertEquals(sec, t.getSecond());
}
 
Example 7
@Test
public void subtract_seconds_from_date_in_java8() {

	LocalDateTime newYearsDay = LocalDateTime.of(2013, Month.JANUARY, 1, 0,
			0);
	LocalDateTime newYearsEve = newYearsDay.minusSeconds(60);

	java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter
			.ofPattern("MM/dd/yyyy HH:mm:ss S");

	logger.info(newYearsDay.format(formatter));
	logger.info(newYearsEve.format(formatter));

	assertTrue(newYearsEve.isBefore(newYearsDay));
}
 
Example 8
@Test(dataProvider="minusSeconds_fromZero")
public void test_minusSeconds_fromZero(int seconds, LocalDate date, int hour, int min, int sec) {
    LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDateTime t = base.minusSeconds(seconds);

    assertEquals(date, t.toLocalDate());
    assertEquals(hour, t.getHour());
    assertEquals(min, t.getMinute());
    assertEquals(sec, t.getSecond());
}
 
Example 9
@Test(dataProvider="minusSeconds_fromZero")
public void test_minusSeconds_fromZero(int seconds, LocalDate date, int hour, int min, int sec) {
    LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDateTime t = base.minusSeconds(seconds);

    assertEquals(date, t.toLocalDate());
    assertEquals(hour, t.getHour());
    assertEquals(min, t.getMinute());
    assertEquals(sec, t.getSecond());
}
 
Example 10
@Test
public void test_toEpochSecond_beforeEpoch() {
    LocalDateTime ldt = LocalDateTime.of(1970, 1, 1, 0, 0).plusHours(1);
    for (int i = 0; i < 100000; i++) {
        ZonedDateTime a = ZonedDateTime.of(ldt, ZONE_PARIS);
        assertEquals(a.toEpochSecond(), -i);
        ldt = ldt.minusSeconds(1);
    }
}
 
Example 11
@Test
public void test_toEpochSecond_beforeEpoch() {
    LocalDateTime ldt = LocalDateTime.of(1970, 1, 1, 0, 0).plusHours(1);
    for (int i = 0; i < 100000; i++) {
        ZonedDateTime a = ZonedDateTime.of(ldt, ZONE_PARIS);
        assertEquals(a.toEpochSecond(), -i);
        ldt = ldt.minusSeconds(1);
    }
}
 
Example 12
private Date getOlderDateFromEvent(CloudEvent lastEvent) {
    ZoneId systemDefaultZone = ZoneId.systemDefault();
    Instant lastEventInstant = lastEvent.getTimestamp()
                                        .toInstant();
    LocalDateTime lastEventDateTime = LocalDateTime.ofInstant(lastEventInstant, systemDefaultZone);
    LocalDateTime secondBefore = lastEventDateTime.minusSeconds(1);
    Instant secondBeforeInstant = secondBefore.atZone(systemDefaultZone)
                                              .toInstant();
    return Date.from(secondBeforeInstant);
}
 
Example 13
@Test
public void test_toEpochSecond_beforeEpoch() {
    LocalDateTime ldt = LocalDateTime.of(1970, 1, 1, 0, 0).plusHours(1);
    for (int i = 0; i < 100000; i++) {
        ZonedDateTime a = ZonedDateTime.of(ldt, ZONE_PARIS);
        assertEquals(a.toEpochSecond(), -i);
        ldt = ldt.minusSeconds(1);
    }
}
 
Example 14
@Test
public void test_minusSeconds_one() {
    LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDate d = t.toLocalDate().minusDays(1);

    int hour = 0;
    int min = 0;
    int sec = 0;

    for (int i = 0; i < 3700; i++) {
        t = t.minusSeconds(1);
        sec--;
        if (sec == -1) {
            min--;
            sec = 59;

            if (min == -1) {
                hour--;
                min = 59;

                if (hour == -1) {
                    hour = 23;
                }
            }
        }

        assertEquals(t.toLocalDate(), d);
        assertEquals(t.getHour(), hour);
        assertEquals(t.getMinute(), min);
        assertEquals(t.getSecond(), sec);
    }
}
 
Example 15
@Test(dataProvider="minusSeconds_fromZero")
public void test_minusSeconds_fromZero(int seconds, LocalDate date, int hour, int min, int sec) {
    LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDateTime t = base.minusSeconds(seconds);

    assertEquals(date, t.toLocalDate());
    assertEquals(hour, t.getHour());
    assertEquals(min, t.getMinute());
    assertEquals(sec, t.getSecond());
}
 
Example 16
@Test
public void test_minusSeconds_one() {
    LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDate d = t.toLocalDate().minusDays(1);

    int hour = 0;
    int min = 0;
    int sec = 0;

    for (int i = 0; i < 3700; i++) {
        t = t.minusSeconds(1);
        sec--;
        if (sec == -1) {
            min--;
            sec = 59;

            if (min == -1) {
                hour--;
                min = 59;

                if (hour == -1) {
                    hour = 23;
                }
            }
        }

        assertEquals(t.toLocalDate(), d);
        assertEquals(t.getHour(), hour);
        assertEquals(t.getMinute(), min);
        assertEquals(t.getSecond(), sec);
    }
}
 
Example 17
@Test
public void test_toEpochSecond_beforeEpoch() {
    LocalDateTime ldt = LocalDateTime.of(1970, 1, 1, 0, 0).plusHours(1);
    for (int i = 0; i < 100000; i++) {
        ZonedDateTime a = ZonedDateTime.of(ldt, ZONE_PARIS);
        assertEquals(a.toEpochSecond(), -i);
        ldt = ldt.minusSeconds(1);
    }
}
 
Example 18
Source Project: datacollector   File: OracleCDCSource.java    License: Apache License 2.0 4 votes vote down vote up
private LocalDateTime adjustStartTime(LocalDateTime startTime) {
  return useLocalBuffering ? startTime : startTime.minusSeconds(configBean.txnWindow);
}
 
Example 19
public static LocalDateTime minusSeconds(LocalDateTime datetime, long duration) {

    return datetime.minusSeconds(duration);
  }
 
Example 20
Source Project: groovy   File: DateTimeExtensions.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Returns a {@link java.time.LocalDateTime} that is {@code seconds} seconds before this date/time.
 *
 * @param self    a LocalDateTime
 * @param seconds the number of seconds to subtract
 * @return a LocalDateTime
 * @since 2.5.0
 */
public static LocalDateTime minus(final LocalDateTime self, long seconds) {
    return self.minusSeconds(seconds);
}