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

The following examples show how to use java.time.LocalDateTime#until() . 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 File: DataBackupCreateService.java    From axelor-open-suite with GNU Affero General Public License v3.0 5 votes vote down vote up
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
Source File: TCKLocalDateTime.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
@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
Source File: TCKLocalDateTime.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@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 4
Source File: TCKLocalDateTime.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@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
Source File: TCKLocalDateTime.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@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 6
Source File: TCKLocalDateTime.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@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
Source File: TCKLocalDateTime.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
@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 8
Source File: TCKLocalDateTime.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@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
Source File: TCKLocalDateTime.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
@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
Source File: TCKLocalDateTime.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@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
Source File: TCKLocalDateTime.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@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
Source File: TCKLocalDateTime.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@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
Source File: TCKLocalDateTime.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
@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 14
Source File: TCKLocalDateTime.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@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
Source File: TCKLocalDateTime.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@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 16
Source File: TCKLocalDateTime.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
@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
Source File: TCKLocalDateTime.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@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 18
Source File: DateUtils.java    From Lottor with 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 File: DateUtils.java    From Lottor with 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
Source File: DateUtils.java    From Raincat with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * 取得两个日期之间相差的秒数.
 *
 * @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);
}