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

The following examples show how to use java.time.LocalDateTime#withNano() . 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
@Test
public void test_withNanoOfSecond_normal() {
    LocalDateTime t = TEST_2007_07_15_12_30_40_987654321;
    t = t.withNano(1);
    assertEquals(t.getNano(), 1);
    t = t.withNano(10);
    assertEquals(t.getNano(), 10);
    t = t.withNano(100);
    assertEquals(t.getNano(), 100);
    t = t.withNano(999999999);
    assertEquals(t.getNano(), 999999999);
}
 
Example 2
@Test
public void test_withNanoOfSecond_normal() {
    LocalDateTime t = TEST_2007_07_15_12_30_40_987654321;
    t = t.withNano(1);
    assertEquals(t.getNano(), 1);
    t = t.withNano(10);
    assertEquals(t.getNano(), 10);
    t = t.withNano(100);
    assertEquals(t.getNano(), 100);
    t = t.withNano(999999999);
    assertEquals(t.getNano(), 999999999);
}
 
Example 3
@Test
public void test_withNanoOfSecond_normal() {
    LocalDateTime t = TEST_2007_07_15_12_30_40_987654321;
    t = t.withNano(1);
    assertEquals(t.getNano(), 1);
    t = t.withNano(10);
    assertEquals(t.getNano(), 10);
    t = t.withNano(100);
    assertEquals(t.getNano(), 100);
    t = t.withNano(999999999);
    assertEquals(t.getNano(), 999999999);
}
 
Example 4
@Test
public void test_withNanoOfSecond_normal() {
    LocalDateTime t = TEST_2007_07_15_12_30_40_987654321;
    t = t.withNano(1);
    assertEquals(t.getNano(), 1);
    t = t.withNano(10);
    assertEquals(t.getNano(), 10);
    t = t.withNano(100);
    assertEquals(t.getNano(), 100);
    t = t.withNano(999999999);
    assertEquals(t.getNano(), 999999999);
}
 
Example 5
@Test
public void test_withNanoOfSecond_normal() {
    LocalDateTime t = TEST_2007_07_15_12_30_40_987654321;
    t = t.withNano(1);
    assertEquals(t.getNano(), 1);
    t = t.withNano(10);
    assertEquals(t.getNano(), 10);
    t = t.withNano(100);
    assertEquals(t.getNano(), 100);
    t = t.withNano(999999999);
    assertEquals(t.getNano(), 999999999);
}
 
Example 6
@Test
public void test_withNanoOfSecond_normal() {
    LocalDateTime t = TEST_2007_07_15_12_30_40_987654321;
    t = t.withNano(1);
    assertEquals(t.getNano(), 1);
    t = t.withNano(10);
    assertEquals(t.getNano(), 10);
    t = t.withNano(100);
    assertEquals(t.getNano(), 100);
    t = t.withNano(999999999);
    assertEquals(t.getNano(), 999999999);
}
 
Example 7
@Test
public void test_withNanoOfSecond_normal() {
    LocalDateTime t = TEST_2007_07_15_12_30_40_987654321;
    t = t.withNano(1);
    assertEquals(t.getNano(), 1);
    t = t.withNano(10);
    assertEquals(t.getNano(), 10);
    t = t.withNano(100);
    assertEquals(t.getNano(), 100);
    t = t.withNano(999999999);
    assertEquals(t.getNano(), 999999999);
}
 
Example 8
Source Project: jetcache   File: DefaultMetricsManager.java    License: Apache License 2.0 5 votes vote down vote up
protected static LocalDateTime computeFirstResetTime(LocalDateTime baseTime, int time, TimeUnit unit) {
    if (unit != TimeUnit.SECONDS && unit != TimeUnit.MINUTES && unit != TimeUnit.HOURS && unit != TimeUnit.DAYS) {
        throw new IllegalArgumentException();
    }
    LocalDateTime t = baseTime;
    switch (unit) {
        case DAYS:
            t = t.plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
            break;
        case HOURS:
            if (24 % time == 0) {
                t = t.plusHours(time - t.getHour() % time);
            } else {
                t = t.plusHours(1);
            }
            t = t.withMinute(0).withSecond(0).withNano(0);
            break;
        case MINUTES:
            if (60 % time == 0) {
                t = t.plusMinutes(time - t.getMinute() % time);
            } else {
                t = t.plusMinutes(1);
            }
            t = t.withSecond(0).withNano(0);
            break;
        case SECONDS:
            if (60 % time == 0) {
                t = t.plusSeconds(time - t.getSecond() % time);
            } else {
                t = t.plusSeconds(1);
            }
            t = t.withNano(0);
            break;
    }
    return t;
}
 
Example 9
@Test
public void test_withNanoOfSecond_normal() {
    LocalDateTime t = TEST_2007_07_15_12_30_40_987654321;
    t = t.withNano(1);
    assertEquals(t.getNano(), 1);
    t = t.withNano(10);
    assertEquals(t.getNano(), 10);
    t = t.withNano(100);
    assertEquals(t.getNano(), 100);
    t = t.withNano(999999999);
    assertEquals(t.getNano(), 999999999);
}
 
Example 10
@Test
public void test_withNanoOfSecond_normal() {
    LocalDateTime t = TEST_2007_07_15_12_30_40_987654321;
    t = t.withNano(1);
    assertEquals(t.getNano(), 1);
    t = t.withNano(10);
    assertEquals(t.getNano(), 10);
    t = t.withNano(100);
    assertEquals(t.getNano(), 100);
    t = t.withNano(999999999);
    assertEquals(t.getNano(), 999999999);
}
 
Example 11
@Test
public void test_withNanoOfSecond_normal() {
    LocalDateTime t = TEST_2007_07_15_12_30_40_987654321;
    t = t.withNano(1);
    assertEquals(t.getNano(), 1);
    t = t.withNano(10);
    assertEquals(t.getNano(), 10);
    t = t.withNano(100);
    assertEquals(t.getNano(), 100);
    t = t.withNano(999999999);
    assertEquals(t.getNano(), 999999999);
}
 
Example 12
@Test
public void test_withNanoOfSecond_normal() {
    LocalDateTime t = TEST_2007_07_15_12_30_40_987654321;
    t = t.withNano(1);
    assertEquals(t.getNano(), 1);
    t = t.withNano(10);
    assertEquals(t.getNano(), 10);
    t = t.withNano(100);
    assertEquals(t.getNano(), 100);
    t = t.withNano(999999999);
    assertEquals(t.getNano(), 999999999);
}
 
Example 13
@Test
public void test_withNanoOfSecond_normal() {
    LocalDateTime t = TEST_2007_07_15_12_30_40_987654321;
    t = t.withNano(1);
    assertEquals(t.getNano(), 1);
    t = t.withNano(10);
    assertEquals(t.getNano(), 10);
    t = t.withNano(100);
    assertEquals(t.getNano(), 100);
    t = t.withNano(999999999);
    assertEquals(t.getNano(), 999999999);
}
 
Example 14
@Test
public void test_withNanoOfSecond_normal() {
    LocalDateTime t = TEST_2007_07_15_12_30_40_987654321;
    t = t.withNano(1);
    assertEquals(t.getNano(), 1);
    t = t.withNano(10);
    assertEquals(t.getNano(), 10);
    t = t.withNano(100);
    assertEquals(t.getNano(), 100);
    t = t.withNano(999999999);
    assertEquals(t.getNano(), 999999999);
}
 
Example 15
Source Project: j2objc   File: TCKLocalDateTime.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void test_withNanoOfSecond_normal() {
    LocalDateTime t = TEST_2007_07_15_12_30_40_987654321;
    t = t.withNano(1);
    assertEquals(t.getNano(), 1);
    t = t.withNano(10);
    assertEquals(t.getNano(), 10);
    t = t.withNano(100);
    assertEquals(t.getNano(), 100);
    t = t.withNano(999999999);
    assertEquals(t.getNano(), 999999999);
}
 
Example 16
Source Project: charts   File: Axis.java    License: Apache License 2.0 4 votes vote down vote up
private List<LocalDateTime> makeDatesEven(List<LocalDateTime> dates, LocalDateTime dateTime) {
    // If the dates contain more dates than just the lower and upper bounds, make the dates in between even.
    if (dates.size() > 2) {
        List<LocalDateTime> evenDates = new ArrayList<>();

        // For each interval, modify the date slightly by a few millis, to make sure they are different days.
        // This is because Axis stores each value and won't update the tick labels, if the value is already known.
        // This happens if you display days and then add a date many years in the future the tick label will still be displayed as day.
        for (int i = 0; i < dates.size(); i++) {
            dateTime = dates.get(i);
            switch (currentInterval.getInterval()) {
                case YEARS:
                    // If its not the first or last date (lower and upper bound), make the year begin with first month and let the months begin with first day.
                    if (i != 0 && i != dates.size() - 1) {
                        dateTime.withMonth(1);
                        dateTime.withDayOfMonth(1);
                    }
                    dateTime.withHour(0);
                    dateTime.withMinute(0);
                    dateTime.withSecond(0);
                    dateTime.withNano(6000000);
                    break;
                case MONTHS:
                    // If its not the first or last date (lower and upper bound), make the months begin with first day.
                    if (i != 0 && i != dates.size() - 1) {
                        dateTime.withDayOfMonth(1);
                    }
                    dateTime.withHour(0);
                    dateTime.withMinute(0);
                    dateTime.withSecond(0);
                    dateTime.withNano(5000000);
                    break;
                case WEEKS:
                    // Make weeks begin with first day of week?
                    dateTime.withHour(0);
                    dateTime.withMinute(0);
                    dateTime.withSecond(0);
                    dateTime.withNano(4000000);
                    break;
                case DAYS:
                    dateTime.withHour(0);
                    dateTime.withMinute(0);
                    dateTime.withSecond(0);
                    dateTime.withNano(3000000);
                    break;
                case HOURS:
                    if (i != 0 && i != dates.size() - 1) {
                        dateTime.withMinute(0);
                        dateTime.withSecond(0);
                    }
                    dateTime.withNano(2000000);
                    break;
                case MINUTES:
                    if (i != 0 && i != dates.size() - 1) {
                        dateTime.withSecond(0);
                    }
                    dateTime.withNano(1000000);
                    break;
                case SECONDS:
                    dateTime.withSecond(0);
                    break;

            }
            evenDates.add(dateTime);
        }

        return evenDates;
    } else {
        return dates;
    }
}
 
Example 17
Source Project: ph-commons   File: PDTFactory.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Get the passed date time but with micro and nanoseconds set to 0, so that
 * only the milliseconds part is present. This is helpful for XSD
 * serialization, where only milliseconds granularity is available.
 *
 * @param aLDT
 *        Source date time. May be <code>null</code>.
 * @return <code>null</code> if the parameter is <code>null</code>, the local
 *         date time with microseconds and nanoseconds set to 0 otherwise.
 * @since 9.2.0
 */
@Nullable
public static LocalDateTime getWithMillisOnly (@Nullable final LocalDateTime aLDT)
{
  return aLDT == null ? null : aLDT.withNano (aLDT.get (ChronoField.MILLI_OF_SECOND) *
                                              (int) CGlobal.NANOSECONDS_PER_MILLISECOND);
}