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

The following examples show how to use java.time.LocalDateTime#with() . 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: WorkingDaysAdjustersTest.java    From alf.io with GNU General Public License v3.0 5 votes vote down vote up
@Test
@DisplayName("do nothing if the date is within range (middle)")
void doNothingIfDateWithinRangeMiddle() {
    LocalDateTime localDateTime = LocalDateTime.of(2015, Month.JUNE, 17, 13, 0);
    LocalDateTime adjusted = localDateTime.with(WorkingDaysAdjusters.defaultWorkingDays());
    assertNotNull(adjusted);
    assertEquals(DayOfWeek.WEDNESDAY, adjusted.getDayOfWeek());
    assertEquals(17, adjusted.getDayOfMonth());
    assertEquals(13, adjusted.getHour());
    assertEquals(0, adjusted.getMinute());
}
 
Example 2
Source File: TCKDateTimeTextPrinting.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="printText")
public void test_appendText1arg_format(TemporalField field, TextStyle style, int value, String expected) throws Exception {
    if (style == TextStyle.FULL) {
        DateTimeFormatter f = builder.appendText(field).toFormatter(Locale.ENGLISH);
        LocalDateTime dt = LocalDateTime.of(2010, 1, 1, 0, 0);
        dt = dt.with(field, value);
        String text = f.format(dt);
        assertEquals(text, expected);
    }
}
 
Example 3
Source File: TCKDateTimeTextPrinting.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="printText")
public void test_appendText1arg_format(TemporalField field, TextStyle style, int value, String expected) throws Exception {
    if (style == TextStyle.FULL) {
        DateTimeFormatter f = builder.appendText(field).toFormatter(Locale.ENGLISH);
        LocalDateTime dt = LocalDateTime.of(2010, 1, 1, 0, 0);
        dt = dt.with(field, value);
        String text = f.format(dt);
        assertEquals(text, expected);
    }
}
 
Example 4
Source File: BaseSchedule.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public Iterator<Instant> iterator() {
  LocalDateTime at = LocalDateTime.ofInstant(this.at, zoneId);
  LocalDateTime adjustedAt = at.with(adjuster);
  final LocalDateTime start = adjustedAt.isBefore(at)
      ? at.plus(amount).with(adjuster)
      : adjustedAt;

  return new Iterator<Instant>() {
    private LocalDateTime next = start;

    @Override
    public boolean hasNext() {
      return true;
    }

    @Override
    public Instant next() {
      Instant result = next.atZone(zoneId).toInstant();
      next = next.plus(amount).with(adjuster);

      return result;
    }

    @Override
    public void remove() {
      throw new UnsupportedOperationException("Schedule iterator does not support remove operation.");
    }
  };
}
 
Example 5
Source File: WorkingDaysAdjustersTest.java    From alf.io with GNU General Public License v3.0 5 votes vote down vote up
@Test
@DisplayName("do nothing if the date is within range (end)")
void doNothingIfDateWithinRangeEnd() {
    LocalDateTime localDateTime = LocalDateTime.of(2015, Month.JUNE, 17, 19, 59);
    LocalDateTime adjusted = localDateTime.with(WorkingDaysAdjusters.defaultWorkingDays());
    assertNotNull(adjusted);
    assertEquals(DayOfWeek.WEDNESDAY, adjusted.getDayOfWeek());
    assertEquals(17, adjusted.getDayOfMonth());
    assertEquals(19, adjusted.getHour());
    assertEquals(59, adjusted.getMinute());
}
 
Example 6
Source File: TCKDateTimeTextPrinting.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="printText")
public void test_appendText1arg_format(TemporalField field, TextStyle style, int value, String expected) throws Exception {
    if (style == TextStyle.FULL) {
        DateTimeFormatter f = builder.appendText(field).toFormatter(Locale.ENGLISH);
        LocalDateTime dt = LocalDateTime.of(2010, 1, 1, 0, 0);
        dt = dt.with(field, value);
        String text = f.format(dt);
        assertEquals(text, expected);
    }
}
 
Example 7
Source File: TCKDateTimeTextPrinting.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="printText")
public void test_appendText2arg_format(TemporalField field, TextStyle style, int value, String expected) throws Exception {
    DateTimeFormatter f = builder.appendText(field, style).toFormatter(Locale.ENGLISH);
    LocalDateTime dt = LocalDateTime.of(2010, 1, 1, 0, 0);
    dt = dt.with(field, value);
    String text = f.format(dt);
    assertEquals(text, expected);
}
 
Example 8
Source File: TCKDateTimeTextPrinting.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="printText")
public void test_appendText1arg_format(TemporalField field, TextStyle style, int value, String expected) throws Exception {
    if (style == TextStyle.FULL) {
        DateTimeFormatter f = builder.appendText(field).toFormatter(Locale.ENGLISH);
        LocalDateTime dt = LocalDateTime.of(2010, 1, 1, 0, 0);
        dt = dt.with(field, value);
        String text = f.format(dt);
        assertEquals(text, expected);
    }
}
 
Example 9
Source File: TCKDateTimeTextPrinting.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
@UseDataProvider("data_text")
public void test_appendText1arg_format(TemporalField field, TextStyle style, int value, String expected) throws Exception {
    if (style == TextStyle.FULL) {
        DateTimeFormatter f = builder.appendText(field).toFormatter(Locale.ENGLISH);
        LocalDateTime dt = LocalDateTime.of(2010, 1, 1, 0, 0);
        dt = dt.with(field, value);
        String text = f.format(dt);
        assertEquals(text, expected);
    }
}
 
Example 10
Source File: TCKDateTimeTextPrinting.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="printText")
public void test_appendText1arg_format(TemporalField field, TextStyle style, int value, String expected) throws Exception {
    if (style == TextStyle.FULL) {
        DateTimeFormatter f = builder.appendText(field).toFormatter(Locale.ENGLISH);
        LocalDateTime dt = LocalDateTime.of(2010, 1, 1, 0, 0);
        dt = dt.with(field, value);
        String text = f.format(dt);
        assertEquals(text, expected);
    }
}
 
Example 11
Source File: TCKDateTimeTextPrinting.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="printText")
public void test_appendText2arg_format(TemporalField field, TextStyle style, int value, String expected) throws Exception {
    DateTimeFormatter f = builder.appendText(field, style).toFormatter(Locale.ENGLISH);
    LocalDateTime dt = LocalDateTime.of(2010, 1, 1, 0, 0);
    dt = dt.with(field, value);
    String text = f.format(dt);
    assertEquals(text, expected);
}
 
Example 12
Source File: TCKDateTimeTextPrinting.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="printText")
public void test_appendText1arg_format(TemporalField field, TextStyle style, int value, String expected) throws Exception {
    if (style == TextStyle.FULL) {
        DateTimeFormatter f = builder.appendText(field).toFormatter(Locale.ENGLISH);
        LocalDateTime dt = LocalDateTime.of(2010, 1, 1, 0, 0);
        dt = dt.with(field, value);
        String text = f.format(dt);
        assertEquals(text, expected);
    }
}
 
Example 13
Source File: SyslogDecoder.java    From datacollector with Apache License 2.0 5 votes vote down vote up
/**
 * Parse the RFC3164 date format. This is trickier than it sounds because this
 * format does not specify a year so we get weird edge cases at year
 * boundaries. This implementation tries to "do what I mean".
 * @param ts RFC3164-compatible timestamp to be parsed
 * @return Typical (for Java) milliseconds since the UNIX epoch
 */
public static long parseRfc3164Time(String ts) throws OnRecordErrorException {
  LocalDateTime now = LocalDateTime.now();
  int year = now.getYear();
  ts = TWO_SPACES.matcher(ts).replaceFirst(" ");
  LocalDateTime date;
  try {
    MonthDay monthDay = MonthDay.parse(ts, rfc3164Format);
    LocalTime time = LocalTime.parse(ts, rfc3164Format);
    // this is overly complicated because of the way Java 8 Time API works, as compared to Joda
    // essentially, we just want to pull year out of "now" and set all other fields based on
    // what was parsed
    date = now;
    // zero out millis since we aren't actually parsing those
    date = date.with(ChronoField.MILLI_OF_SECOND, 0);
    // set month and day of month from parsed
    date = date.withMonth(monthDay.getMonthValue()).withDayOfMonth(monthDay.getDayOfMonth());
    // set time fields from parsed
    date = date.withHour(time.getHour()).withMinute(time.getMinute()).withSecond(time.getSecond());
  } catch (DateTimeParseException e) {
    throw new OnRecordErrorException(Errors.SYSLOG_10, ts, e);
  }
  // The RFC3164 is a bit weird date format - it contains day and month, but no year. So we have to somehow guess
  // the year. The current logic is to provide a sliding window - going 11 months to the past and 1 month to the
  // future. If the message is outside of this window, it will have incorrectly guessed year. We go 11 months to the
  // past as we're expecting that more messages will be from the past (syslog usually contains historical data).
  LocalDateTime fixed = date;
  if (fixed.isAfter(now) && fixed.minusMonths(1).isAfter(now)) {
    fixed = date.withYear(year - 1);
  } else if (fixed.isBefore(now) && fixed.plusMonths(11).isBefore(now)) {
    fixed = date.withYear(year + 1);
  }
  date = fixed;
  return date.toInstant(ZoneOffset.UTC).toEpochMilli();
}
 
Example 14
Source File: TCKDateTimeTextPrinting.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="printText")
public void test_appendText1arg_format(TemporalField field, TextStyle style, int value, String expected) throws Exception {
    if (style == TextStyle.FULL) {
        DateTimeFormatter f = builder.appendText(field).toFormatter(Locale.ENGLISH);
        LocalDateTime dt = LocalDateTime.of(2010, 1, 1, 0, 0);
        dt = dt.with(field, value);
        String text = f.format(dt);
        assertEquals(text, expected);
    }
}
 
Example 15
Source File: TCKDateTimeTextPrinting.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="printText")
public void test_appendText2arg_format(TemporalField field, TextStyle style, int value, String expected) throws Exception {
    DateTimeFormatter f = builder.appendText(field, style).toFormatter(Locale.ENGLISH);
    LocalDateTime dt = LocalDateTime.of(2010, 1, 1, 0, 0);
    dt = dt.with(field, value);
    String text = f.format(dt);
    assertEquals(text, expected);
}
 
Example 16
Source File: TCKDateTimeTextPrinting.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="printText")
public void test_appendText2arg_format(TemporalField field, TextStyle style, int value, String expected) throws Exception {
    DateTimeFormatter f = builder.appendText(field, style).toFormatter(Locale.ENGLISH);
    LocalDateTime dt = LocalDateTime.of(2010, 1, 1, 0, 0);
    dt = dt.with(field, value);
    String text = f.format(dt);
    assertEquals(text, expected);
}
 
Example 17
Source File: TCKDateTimeTextPrinting.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="printText")
public void test_appendText1arg_format(TemporalField field, TextStyle style, int value, String expected) throws Exception {
    if (style == TextStyle.FULL) {
        DateTimeFormatter f = builder.appendText(field).toFormatter(Locale.ENGLISH);
        LocalDateTime dt = LocalDateTime.of(2010, 1, 1, 0, 0);
        dt = dt.with(field, value);
        String text = f.format(dt);
        assertEquals(text, expected);
    }
}
 
Example 18
Source File: Main.java    From Java-Coding-Problems with MIT License 4 votes vote down vote up
public static void main(String[] args) {

        LocalDate localDate = LocalDate.of(2019, 2, 28);

        System.out.println("LocalDate start of the day:");
        LocalDateTime ldDayStart1 = localDate.atStartOfDay();
        ZonedDateTime ldDayStartZone1 = localDate.atStartOfDay(ZoneId.of("Australia/Perth"));
        System.out.println("Start of the day without time zone via atStartOfDay(): " + ldDayStart1);
        System.out.println("Start of the day with time zone, Australia/Perth via atStartOfDay(): " + ldDayStartZone1);

        LocalDateTime ldDayStart2 = LocalDateTime.of(localDate, LocalTime.MIN);
        ZonedDateTime ldDayStartZone2 = LocalDateTime
                .of(localDate, LocalTime.MIN).atZone(ZoneId.of("Australia/Perth"));
        System.out.println("\nStart of the day without time zone via of(): " + ldDayStart2);
        System.out.println("Start of the day with time zone, Australia/Perth via of(): " + ldDayStartZone2);

        System.out.println("\n\nLocalDate end of the day:");
        LocalDateTime ldDayEnd1 = localDate.atTime(LocalTime.MAX);
        ZonedDateTime ldDayEndZone1 = localDate.atTime(LocalTime.MAX)
                .atZone(ZoneId.of("Australia/Perth"));
        System.out.println("End of the day without time zone via atTime(): " + ldDayEnd1);
        System.out.println("End of the day with time zone, Australia/Perth via atTime(): " + ldDayEndZone1);

        LocalDateTime ldDayEnd2 = LocalTime.MAX.atDate(localDate);
        ZonedDateTime ldDayEndZone2 = LocalTime.MAX.atDate(localDate)
                .atZone(ZoneId.of("Australia/Perth"));
        System.out.println("\nEnd of the day without time zone via atDate(): " + ldDayEnd2);
        System.out.println("End of the day with time zone, Australia/Perth via atDate(): " + ldDayEndZone2);

        System.out.println("\n\nLocalDateTime start of the day:");
        LocalDateTime localDateTime = LocalDateTime.of(2019, 2, 28, 18, 0, 0);
        LocalDateTime ldtDayStart = localDateTime.with(ChronoField.NANO_OF_DAY, LocalTime.MIN.toNanoOfDay());
        //LocalDateTime ldtDayStart = localDateTime.with(ChronoField.HOUR_OF_DAY, 0);
        ZonedDateTime ldtDayStartZone = localDateTime
                .with(ChronoField.NANO_OF_DAY, LocalTime.MIN.toNanoOfDay())
                .atZone(ZoneId.of("Australia/Perth"));
        System.out.println("\nStart of the day without time zone via with(): " + ldtDayStart);
        System.out.println("Start of the day with time zone, Australia/Perth via with(): " + ldtDayStartZone);

        LocalDateTime ldtDayEnd = localDateTime.with(ChronoField.NANO_OF_DAY, LocalTime.MAX.toNanoOfDay());
        // LocalDateTime ldtDayEnd = localDateTime.with(ChronoField.NANO_OF_DAY, 86399999999999L);
        ZonedDateTime ldtDayEndZone = localDateTime
                .with(ChronoField.NANO_OF_DAY, LocalTime.MAX.toNanoOfDay())
                .atZone(ZoneId.of("Australia/Perth"));
        System.out.println("\nEnd of the day without time zone via with(): " + ldtDayEnd);
        System.out.println("End of the day with time zone, Australia/Perth via with(): " + ldtDayEndZone);

        System.out.println("\n\nUTC start and end of the day:");
        ZonedDateTime zdt = ZonedDateTime.now(ZoneOffset.UTC);
        ZonedDateTime dayStartZdt = zdt.toLocalDate().atStartOfDay(zdt.getZone());
        ZonedDateTime dayEndZdt = zdt.toLocalDate().atTime(LocalTime.MAX).atZone(zdt.getZone());

        System.out.println("UTC time: " + zdt);
        System.out.println("Start day with UTC: " + dayStartZdt);
        System.out.println("End day with UTC: " + dayEndZdt);
    }
 
Example 19
Source File: UseLocalDateTime.java    From tutorials with MIT License 4 votes vote down vote up
LocalDateTime getEndOfDayFromLocalDateTimeDirectly(LocalDateTime localDateTime) {
    LocalDateTime endOfDate = localDateTime.with(ChronoField.NANO_OF_DAY, LocalTime.MAX.toNanoOfDay());
    return endOfDate;
}
 
Example 20
Source File: NinetyDayWeekendAdjuster.java    From levelup-java-examples with Apache License 2.0 3 votes vote down vote up
@Test
public void test() {

	LocalDateTime currentDateTime = LocalDateTime.of(2014, Month.APRIL, 30,
			0, 0);

	LocalDateTime adjustedDate = currentDateTime
			.with(new NinetyDayWeekendAdjuster());

	assertEquals(LocalDateTime.of(2014, Month.SEPTEMBER, 3, 0, 0),
			adjustedDate);

}