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

The following examples show how to use java.time.LocalDateTime#until() . 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
public String createRelativeDateTime(LocalDateTime dateT) {
  LocalDateTime currentDateTime = LocalDateTime.now();

  long years = currentDateTime.until(dateT, ChronoUnit.YEARS);
  currentDateTime = currentDateTime.plusYears(years);

  long months = currentDateTime.until(dateT, ChronoUnit.MONTHS);
  currentDateTime = currentDateTime.plusMonths(months);

  long days = currentDateTime.until(dateT, ChronoUnit.DAYS);
  currentDateTime = currentDateTime.plusDays(days);

  long hours = currentDateTime.until(dateT, ChronoUnit.HOURS);
  currentDateTime = currentDateTime.plusHours(hours);

  long minutes = currentDateTime.until(dateT, ChronoUnit.MINUTES);
  currentDateTime = currentDateTime.plusMinutes(minutes);

  long seconds = currentDateTime.until(dateT, ChronoUnit.SECONDS);
  if (seconds < 0 || minutes < 0 || hours < 0 || days < 0 || months < 0 || years < 0) {
    return "NOW["
        + ((years == 0) ? "" : (years + "y"))
        + ((months == 0) ? "" : (months + "M"))
        + ((days == 0) ? "" : (days + "d"))
        + ((hours == 0) ? "" : (hours + "H"))
        + ((minutes == 0) ? "" : (minutes + "m"))
        + ((seconds == 0) ? "" : (seconds + "s"))
        + "]";
  }
  return "NOW["
      + ((years == 0) ? "" : ("+" + years + "y"))
      + ((months == 0) ? "" : ("+" + months + "M"))
      + ((days == 0) ? "" : ("+" + days + "d"))
      + ((hours == 0) ? "" : ("+" + hours + "H"))
      + ((minutes == 0) ? "" : ("+" + minutes + "m"))
      + ((seconds == 0) ? "" : ("+" + seconds + "s"))
      + "]";
}
 
Example 2
@Test(dataProvider="periodUntilUnit")
public void test_until_TemporalUnit(LocalDateTime dt1, LocalDateTime dt2, TemporalUnit unit, long expected) {
    long amount = dt1.until(dt2, unit);
    assertEquals(amount, expected);
}
 
Example 3
@Test(dataProvider="periodUntilUnit")
public void test_until_TemporalUnit_negated(LocalDateTime dt1, LocalDateTime dt2, TemporalUnit unit, long expected) {
    long amount = dt2.until(dt1, unit);
    assertEquals(amount, -expected);
}
 
Example 4
@Test(expectedExceptions=DateTimeException.class)
public void test_until_invalidType() {
    LocalDateTime start = LocalDateTime.of(2010, 6, 30, 2, 30);
    start.until(LocalTime.of(11, 30), DAYS);
}
 
Example 5
@Test(dataProvider="periodUntilUnit")
public void test_until_TemporalUnit_negated(LocalDateTime dt1, LocalDateTime dt2, TemporalUnit unit, long expected) {
    long amount = dt2.until(dt1, unit);
    assertEquals(amount, -expected);
}
 
Example 6
@Test(dataProvider="periodUntilUnit")
public void test_until_TemporalUnit_negated(LocalDateTime dt1, LocalDateTime dt2, TemporalUnit unit, long expected) {
    long amount = dt2.until(dt1, unit);
    assertEquals(amount, -expected);
}
 
Example 7
@Test(dataProvider="periodUntilUnit")
public void test_until_TemporalUnit_negated(LocalDateTime dt1, LocalDateTime dt2, TemporalUnit unit, long expected) {
    long amount = dt2.until(dt1, unit);
    assertEquals(amount, -expected);
}
 
Example 8
@Test(dataProvider="periodUntilUnit")
public void test_until_TemporalUnit(LocalDateTime dt1, LocalDateTime dt2, TemporalUnit unit, long expected) {
    long amount = dt1.until(dt2, unit);
    assertEquals(amount, expected);
}
 
Example 9
@Test(dataProvider="periodUntilUnit")
public void test_until_TemporalUnit(LocalDateTime dt1, LocalDateTime dt2, TemporalUnit unit, long expected) {
    long amount = dt1.until(dt2, unit);
    assertEquals(amount, expected);
}
 
Example 10
@Test(dataProvider="periodUntilUnit")
public void test_until_TemporalUnit(LocalDateTime dt1, LocalDateTime dt2, TemporalUnit unit, long expected) {
    long amount = dt1.until(dt2, unit);
    assertEquals(amount, expected);
}
 
Example 11
@Test(dataProvider="periodUntilUnit")
public void test_until_TemporalUnit(LocalDateTime dt1, LocalDateTime dt2, TemporalUnit unit, long expected) {
    long amount = dt1.until(dt2, unit);
    assertEquals(amount, expected);
}
 
Example 12
@Test(dataProvider="periodUntilUnit")
public void test_until_TemporalUnit(LocalDateTime dt1, LocalDateTime dt2, TemporalUnit unit, long expected) {
    long amount = dt1.until(dt2, unit);
    assertEquals(amount, expected);
}
 
Example 13
@Test(dataProvider="periodUntilUnit")
public void test_until_TemporalUnit(LocalDateTime dt1, LocalDateTime dt2, TemporalUnit unit, long expected) {
    long amount = dt1.until(dt2, unit);
    assertEquals(amount, expected);
}
 
Example 14
@Test(dataProvider="periodUntilUnit")
public void test_until_TemporalUnit_negated(LocalDateTime dt1, LocalDateTime dt2, TemporalUnit unit, long expected) {
    long amount = dt2.until(dt1, unit);
    assertEquals(amount, -expected);
}
 
Example 15
@Test(expectedExceptions=DateTimeException.class)
public void test_until_invalidType() {
    LocalDateTime start = LocalDateTime.of(2010, 6, 30, 2, 30);
    start.until(LocalTime.of(11, 30), DAYS);
}
 
Example 16
@Test(expectedExceptions=DateTimeException.class)
public void test_until_invalidType() {
    LocalDateTime start = LocalDateTime.of(2010, 6, 30, 2, 30);
    start.until(LocalTime.of(11, 30), DAYS);
}
 
Example 17
@Test(dataProvider="periodUntilUnit")
public void test_until_TemporalUnit(LocalDateTime dt1, LocalDateTime dt2, TemporalUnit unit, long expected) {
    long amount = dt1.until(dt2, unit);
    assertEquals(amount, expected);
}
 
Example 18
Source Project: Lottor   File: DateUtils.java    License: MIT License 2 votes vote down vote up
/**
 * 取得两个日期之间相差的小时数
 *
 * @param t1 开始长日期
 * @param t2 结束长日期
 * @return t1到t2间的日数,如果t2 在 t1之后,返回正数,否则返回负数
 */
public static long getHoursBetween(LocalDateTime t1, LocalDateTime t2) {
    return t1.until(t2, ChronoUnit.HOURS);
}
 
Example 19
Source Project: Lottor   File: DateUtils.java    License: MIT License 2 votes vote down vote up
/**
 * 取得两个日期之间相差的秒数
 *
 * @param t1 开始长日期
 * @param t2 结束长日期
 * @return t1到t2间的日数,如果t2 在 t1之后,返回正数,否则返回负数
 */
public static long getSecondsBetween(LocalDateTime t1, LocalDateTime t2) {
    return t1.until(t2, ChronoUnit.SECONDS);
}
 
Example 20
/**
 * 取得两个日期之间相差的秒数.
 *
 * @param t1 开始长日期
 * @param t2 结束长日期
 * @return t1到t2间的日数 ,如果t2 在 t1之后,返回正数,否则返回负数
 */
public static long getSecondsBetween(final LocalDateTime t1, final LocalDateTime t2) {
    return t1.until(t2, ChronoUnit.SECONDS);
}