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

The following examples show how to use java.time.LocalDateTime#minusMinutes() . 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: onetwo   File: DurationTextTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
	public void testNiceDateText(){
		DurationText dtext = DurationText.createSimpleChineseDurationText();
		
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		LocalDateTime date = LocalDateTime.now();
		LocalDateTime date2 = date.minusSeconds(40);
		System.out.println("date: " + date.format(formatter)+", date2: " + date2.format(formatter));
		
//		Duration duration = Duration.ofSeconds(date2.until(date, ChronoUnit.SECONDS));
		String text = dtext.getText(date2, date);
		Assert.assertEquals("刚刚", text);
		
		date2 = date.minusMinutes(3);
		System.out.println("date: " + date.format(formatter)+", date2: " + date2.format(formatter));
		text = dtext.getText(date2, date);
		Assert.assertEquals("3分钟前", text);

		date2 = date.minusHours(4);
		text = dtext.getText(date2, date);
		Assert.assertEquals("4小时前", text);

		date2 = date.minusDays(32);
		text = dtext.getText(date2, date);
		Assert.assertEquals("32天前", text);
	}
 
Example 2
@Test
public void test_minusMinutes_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;

    for (int i = 0; i < 70; i++) {
        t = t.minusMinutes(1);
        min--;
        if (min == -1) {
            hour--;
            min = 59;

            if (hour == -1) {
                hour = 23;
            }
        }
        assertEquals(t.toLocalDate(), d);
        assertEquals(t.getHour(), hour);
        assertEquals(t.getMinute(), min);
    }
}
 
Example 3
/**
 * Returns a copy of the {@link LocalDateTime} adjusted to be compatible with the format output by
 * {@link #parseDateTimeFromSessionsForm}, i.e. either the time is 23:59, or the minute is 0 and the hour is not 0.
 * The date time is first rounded to the nearest hour, then the special case 00:00 is handled.
 * @param ldt The {@link LocalDateTime} to be adjusted for compatibility.
 * @return a copy of {@code ldt} adjusted for compatibility, or null if {@code ldt} is null.
 * @see #parseDateTimeFromSessionsForm
 */
public static LocalDateTime adjustLocalDateTimeForSessionsFormInputs(LocalDateTime ldt) {
    if (ldt == null) {
        return null;
    }
    if (ldt.getMinute() == 0 && ldt.getHour() != 0 || ldt.getMinute() == 59 && ldt.getHour() == 23) {
        return ldt;
    }

    // Round to the nearest hour
    LocalDateTime rounded;
    LocalDateTime floor = ldt.truncatedTo(ChronoUnit.HOURS);
    LocalDateTime ceiling = floor.plusHours(1);
    Duration distanceToCeiling = Duration.between(ldt, ceiling);
    if (distanceToCeiling.compareTo(Duration.ofMinutes(30)) <= 0) {
        rounded = ceiling;
    } else {
        rounded = floor;
    }

    // Adjust 00:00 -> 23:59
    if (rounded.getHour() == 0) {
        return rounded.minusMinutes(1);
    }
    return rounded;
}
 
Example 4
@Test
public void test_minusMinutes_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;

    for (int i = 0; i < 70; i++) {
        t = t.minusMinutes(1);
        min--;
        if (min == -1) {
            hour--;
            min = 59;

            if (hour == -1) {
                hour = 23;
            }
        }
        assertEquals(t.toLocalDate(), d);
        assertEquals(t.getHour(), hour);
        assertEquals(t.getMinute(), min);
    }
}
 
Example 5
@Test
public void test_minusMinutes_fromZero() {
    LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDate d = base.toLocalDate().minusDays(1);
    LocalTime t = LocalTime.of(22, 49);

    for (int i = 70; i > -70; i--) {
        LocalDateTime dt = base.minusMinutes(i);
        t = t.plusMinutes(1);

        if (t.equals(LocalTime.MIDNIGHT)) {
            d = d.plusDays(1);
        }

        assertEquals(dt.toLocalDate(), d);
        assertEquals(dt.toLocalTime(), t);
    }
}
 
Example 6
@Test
public void test_minusMinutes_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;

    for (int i = 0; i < 70; i++) {
        t = t.minusMinutes(1);
        min--;
        if (min == -1) {
            hour--;
            min = 59;

            if (hour == -1) {
                hour = 23;
            }
        }
        assertEquals(t.toLocalDate(), d);
        assertEquals(t.getHour(), hour);
        assertEquals(t.getMinute(), min);
    }
}
 
Example 7
@Test
public void test_minusMinutes_fromZero() {
    LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDate d = base.toLocalDate().minusDays(1);
    LocalTime t = LocalTime.of(22, 49);

    for (int i = 70; i > -70; i--) {
        LocalDateTime dt = base.minusMinutes(i);
        t = t.plusMinutes(1);

        if (t.equals(LocalTime.MIDNIGHT)) {
            d = d.plusDays(1);
        }

        assertEquals(dt.toLocalDate(), d);
        assertEquals(dt.toLocalTime(), t);
    }
}
 
Example 8
@Test
public void test_minusMinutes_fromZero() {
    LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDate d = base.toLocalDate().minusDays(1);
    LocalTime t = LocalTime.of(22, 49);

    for (int i = 70; i > -70; i--) {
        LocalDateTime dt = base.minusMinutes(i);
        t = t.plusMinutes(1);

        if (t.equals(LocalTime.MIDNIGHT)) {
            d = d.plusDays(1);
        }

        assertEquals(dt.toLocalDate(), d);
        assertEquals(dt.toLocalTime(), t);
    }
}
 
Example 9
@Test
public void test_minusMinutes_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;

    for (int i = 0; i < 70; i++) {
        t = t.minusMinutes(1);
        min--;
        if (min == -1) {
            hour--;
            min = 59;

            if (hour == -1) {
                hour = 23;
            }
        }
        assertEquals(t.toLocalDate(), d);
        assertEquals(t.getHour(), hour);
        assertEquals(t.getMinute(), min);
    }
}
 
Example 10
Source Project: j2objc   File: TCKLocalDateTime.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void test_minusMinutes_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;

    for (int i = 0; i < 70; i++) {
        t = t.minusMinutes(1);
        min--;
        if (min == -1) {
            hour--;
            min = 59;

            if (hour == -1) {
                hour = 23;
            }
        }
        assertEquals(t.toLocalDate(), d);
        assertEquals(t.getHour(), hour);
        assertEquals(t.getMinute(), min);
    }
}
 
Example 11
@Test
public void test_minusMinutes_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;

    for (int i = 0; i < 70; i++) {
        t = t.minusMinutes(1);
        min--;
        if (min == -1) {
            hour--;
            min = 59;

            if (hour == -1) {
                hour = 23;
            }
        }
        assertEquals(t.toLocalDate(), d);
        assertEquals(t.getHour(), hour);
        assertEquals(t.getMinute(), min);
    }
}
 
Example 12
@Test
public void test_minusMinutes_fromZero() {
    LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDate d = base.toLocalDate().minusDays(1);
    LocalTime t = LocalTime.of(22, 49);

    for (int i = 70; i > -70; i--) {
        LocalDateTime dt = base.minusMinutes(i);
        t = t.plusMinutes(1);

        if (t.equals(LocalTime.MIDNIGHT)) {
            d = d.plusDays(1);
        }

        assertEquals(dt.toLocalDate(), d);
        assertEquals(dt.toLocalTime(), t);
    }
}
 
Example 13
@Test
public void test_minusMinutes_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;

    for (int i = 0; i < 70; i++) {
        t = t.minusMinutes(1);
        min--;
        if (min == -1) {
            hour--;
            min = 59;

            if (hour == -1) {
                hour = 23;
            }
        }
        assertEquals(t.toLocalDate(), d);
        assertEquals(t.getHour(), hour);
        assertEquals(t.getMinute(), min);
    }
}
 
Example 14
@Test
public void test_minusMinutes_fromZero() {
    LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDate d = base.toLocalDate().minusDays(1);
    LocalTime t = LocalTime.of(22, 49);

    for (int i = 70; i > -70; i--) {
        LocalDateTime dt = base.minusMinutes(i);
        t = t.plusMinutes(1);

        if (t.equals(LocalTime.MIDNIGHT)) {
            d = d.plusDays(1);
        }

        assertEquals(dt.toLocalDate(), d);
        assertEquals(dt.toLocalTime(), t);
    }
}
 
Example 15
@Test
public void test_minusMinutes_fromZero() {
    LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDate d = base.toLocalDate().minusDays(1);
    LocalTime t = LocalTime.of(22, 49);

    for (int i = 70; i > -70; i--) {
        LocalDateTime dt = base.minusMinutes(i);
        t = t.plusMinutes(1);

        if (t.equals(LocalTime.MIDNIGHT)) {
            d = d.plusDays(1);
        }

        assertEquals(dt.toLocalDate(), d);
        assertEquals(dt.toLocalTime(), t);
    }
}
 
Example 16
@Test
public void test_minusMinutes_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;

    for (int i = 0; i < 70; i++) {
        t = t.minusMinutes(1);
        min--;
        if (min == -1) {
            hour--;
            min = 59;

            if (hour == -1) {
                hour = 23;
            }
        }
        assertEquals(t.toLocalDate(), d);
        assertEquals(t.getHour(), hour);
        assertEquals(t.getMinute(), min);
    }
}
 
Example 17
public void load() {
    int amount = 100;
    LocalDateTime now = LocalDateTime.now();
    final LocalDateTime publishedAt = now.minusMinutes(PUBLISHING_TIME_MAX_DIFF * amount);

    SecurityUtils.run("system", "system", new String[]{"ROLE_ADMIN"}, () -> {
        if (!stableUsersOnly) {
            LocalDateTime at = publishedAt;
            for (int i = 0; i < amount; ++i) {
                User user = nextUser();
                userRepository.save(user);

                Ad ad = nextAd(user, at);
                adRepository.save(ad);

                at = ad.getPublishedAt();
            }
        }

        setupAdmin(publishedAt.minusMinutes(10));
        setupStableUser(publishedAt);
    });
}
 
Example 18
/**
 * Simulate a job that has run longer than the next fire time such that it misfires.
 */
@Test
public void testTriggerPastDueMisfire() throws Exception {
  JobDetail jobDetail = JobBuilder.newJob(MyNonConcurrentJob.class)
      .storeDurably(true)
      .build();
  jobStore.storeJob(jobDetail, false);

  ZoneId zone = ZoneId.systemDefault();

  Date baseFireTimeDate = DateBuilder.evenMinuteDateAfterNow();
  LocalDateTime baseDateTime = LocalDateTime.ofInstant(baseFireTimeDate.toInstant(), zone);
  LocalDateTime startAt = baseDateTime.minusMinutes(5);
  LocalDateTime nextFireTime = startAt.plusMinutes(1);

  CronScheduleBuilder schedule = CronScheduleBuilder.cronSchedule("0 1 * * * ? *");
  OperableTrigger trigger = (OperableTrigger) TriggerBuilder.newTrigger()
      .forJob(jobDetail)
      .withSchedule(schedule)
      .startAt(Date.from(startAt.atZone(zone).toInstant()))
      .build();

  // misfire the trigger and set the next fire time in the past
  trigger.updateAfterMisfire(null);
  trigger.setNextFireTime(Date.from(nextFireTime.atZone(zone).toInstant()));
  jobStore.storeTrigger(trigger, false);

  List<OperableTrigger> acquiredTriggers =
      jobStore.acquireNextTriggers(DateBuilder.evenMinuteDateAfterNow().getTime(), 4, 1000L);
  assertEquals(1, acquiredTriggers.size());
}
 
Example 19
public LocalDateTime updateMinute(LocalDateTime datetime, String minute) {
  if (!Strings.isNullOrEmpty(minute)) {
    Matcher matcher = patternMonth.matcher(minute);
    if (matcher.find()) {
      Long minutes = Long.parseLong(matcher.group());
      if (minute.startsWith("+")) datetime = datetime.plusMinutes(minutes);
      else if (minute.startsWith("-")) datetime = datetime.minusMinutes(minutes);
      else datetime = datetime.withMinute(minutes.intValue());
    }
  }
  return datetime;
}
 
Example 20
/**
 * Simulate a job that has run longer than the next fire time such that it misfires, but will not fire again because
 * the end of the trigger window has passed.
 */
@Test
public void testTriggerPastDueMisfireButWillNotFire() throws Exception {
  JobDetail jobDetail = JobBuilder.newJob(MyNonConcurrentJob.class).storeDurably(true).build();
  jobStore.storeJob(jobDetail, false);

  ZoneId zone = ZoneId.systemDefault();

  Date baseFireTimeDate = DateBuilder.evenMinuteDateAfterNow();
  LocalDateTime baseDateTime = LocalDateTime.ofInstant(baseFireTimeDate.toInstant(), zone);
  LocalDateTime startAt = baseDateTime.minusMinutes(5);
  LocalDateTime endAt = baseDateTime.minusMinutes(1);
  LocalDateTime nextFireTime = startAt.plusMinutes(1);

  SimpleScheduleBuilder simple = SimpleScheduleBuilder.simpleSchedule()
      .withIntervalInMinutes(1);
  OperableTrigger trigger = (OperableTrigger) TriggerBuilder.newTrigger()
      .forJob(jobDetail)
      .withSchedule(simple)
      .startAt(Date.from(startAt.atZone(zone).toInstant()))
      .endAt(Date.from(endAt.atZone(zone).toInstant()))
      .build();

  // misfire the trigger and set the next fire time in the past
  trigger.updateAfterMisfire(null);
  trigger.setNextFireTime(Date.from(nextFireTime.atZone(zone).toInstant()));
  trigger.setMisfireInstruction(MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_EXISTING_COUNT);
  jobStore.storeTrigger(trigger, false);

  List<OperableTrigger> acquiredTriggers =
      jobStore.acquireNextTriggers(baseFireTimeDate.getTime(), 4, 1000L);
  assertEquals(0, acquiredTriggers.size());
}