Java Code Examples for java.time.YearMonth#atDay()

The following examples show how to use java.time.YearMonth#atDay() . 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: Date.java    From baleen with Apache License 2.0 6 votes vote down vote up
private void createDateFromMatcher(
    TextBlock block, Matcher m, Integer yearGroup, Integer monthGroup, Integer dayGroup) {
  Year y = DateTimeUtils.asYear(m.group(yearGroup));

  String month = m.group(monthGroup);
  if (month.endsWith(".")) {
    month = month.substring(0, month.length() - 1);
  }

  YearMonth ym = y.atMonth(DateTimeUtils.asMonth(month));

  LocalDate ld;
  try {
    ld = ym.atDay(Integer.parseInt(m.group(dayGroup)));
  } catch (DateTimeException dte) {
    getMonitor().warn(INVALID_DATE_FOUND, dte);
    return;
  }

  createDate(block, m.start(), m.end(), ld);
}
 
Example 2
Source File: Date.java    From baleen with Apache License 2.0 6 votes vote down vote up
private void createMonth(TextBlock block, Integer charBegin, Integer charEnd, YearMonth ym) {
  // Check the date isn't already covered by a range
  if (alreadyExtracted(block, charBegin, charEnd)) {
    return;
  }

  Temporal date = createExactSingleDate(block, charBegin, charEnd);

  LocalDate start = ym.atDay(1);
  LocalDate end = ym.atEndOfMonth();

  date.setTimestampStart(start.atStartOfDay(ZoneOffset.UTC).toEpochSecond());
  date.setTimestampStop(end.plusDays(1).atStartOfDay(ZoneOffset.UTC).toEpochSecond());

  addToJCasIndex(date);
  extracted.add(date);
}
 
Example 3
Source File: FHIRPathUtil.java    From FHIR with Apache License 2.0 6 votes vote down vote up
public static Temporal getTemporal(TemporalAccessor temporalAccessor) {
    if (temporalAccessor instanceof Year) {
        Year year = (Year) temporalAccessor;
        return year.atMonth(1).atDay(1);
    } else if (temporalAccessor instanceof YearMonth) {
        YearMonth yearMonth = (YearMonth) temporalAccessor;
        return yearMonth.atDay(1);
    } else if (temporalAccessor instanceof LocalDate) {
        return (LocalDate) temporalAccessor;
    } else if (temporalAccessor instanceof LocalDateTime) {
        return (LocalDateTime) temporalAccessor;
    } else if (temporalAccessor instanceof ZonedDateTime) {
        return (ZonedDateTime) temporalAccessor;
    } else if (temporalAccessor instanceof LocalTime) {
        return (LocalTime) temporalAccessor;
    }
    throw new IllegalArgumentException();
}
 
Example 4
Source File: TCKYearMonth.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="atDay")
public void test_atDay(YearMonth test, int day, LocalDate expected) {
    if (expected != null) {
        assertEquals(test.atDay(day), expected);
    } else {
        try {
            test.atDay(day);
            fail();
        } catch (DateTimeException ex) {
            // expected
        }
    }
}
 
Example 5
Source File: TCKYearMonth.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test()
@UseDataProvider("data_atDay")
public void test_atDay(YearMonth test, int day, LocalDate expected) {
    if (expected != null) {
        assertEquals(test.atDay(day), expected);
    } else {
        try {
            test.atDay(day);
            fail();
        } catch (DateTimeException ex) {
            // expected
        }
    }
}
 
Example 6
Source File: TCKYearMonth.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="atDay")
public void test_atDay(YearMonth test, int day, LocalDate expected) {
    if (expected != null) {
        assertEquals(test.atDay(day), expected);
    } else {
        try {
            test.atDay(day);
            fail();
        } catch (DateTimeException ex) {
            // expected
        }
    }
}
 
Example 7
Source File: JFXDatePickerContent.java    From JFoenix with Apache License 2.0 5 votes vote down vote up
protected void updateMonthYearPane() {
    // update date labels
    YearMonth yearMonth = selectedYearMonth.get();
    LocalDate value = datePicker.getValue();
    value = value == null ? LocalDate.now() : value;
    selectedDateLabel.setText(DateTimeFormatter.ofPattern("EEE, MMM dd").format(value));

    selectedYearLabel.setText(formatYear(yearMonth));
    monthYearLabel.setText(formatMonth(yearMonth) + " " + formatYear(yearMonth));

    Chronology chrono = datePicker.getChronology();
    LocalDate firstDayOfMonth = yearMonth.atDay(1);
    backMonthButton.setDisable(!isValidDate(chrono, firstDayOfMonth, -1, DAYS));
    forwardMonthButton.setDisable(!isValidDate(chrono, firstDayOfMonth, +1, MONTHS));
}
 
Example 8
Source File: TCKYearMonth.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="atDay")
public void test_atDay(YearMonth test, int day, LocalDate expected) {
    if (expected != null) {
        assertEquals(test.atDay(day), expected);
    } else {
        try {
            test.atDay(day);
            fail();
        } catch (DateTimeException ex) {
            // expected
        }
    }
}
 
Example 9
Source File: TCKYearMonth.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="atDay")
public void test_atDay(YearMonth test, int day, LocalDate expected) {
    if (expected != null) {
        assertEquals(test.atDay(day), expected);
    } else {
        try {
            test.atDay(day);
            fail();
        } catch (DateTimeException ex) {
            // expected
        }
    }
}
 
Example 10
Source File: TCKYearMonth.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="atDay")
public void test_atDay(YearMonth test, int day, LocalDate expected) {
    if (expected != null) {
        assertEquals(test.atDay(day), expected);
    } else {
        try {
            test.atDay(day);
            fail();
        } catch (DateTimeException ex) {
            // expected
        }
    }
}
 
Example 11
Source File: TCKYearMonth.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="atDay")
public void test_atDay(YearMonth test, int day, LocalDate expected) {
    if (expected != null) {
        assertEquals(test.atDay(day), expected);
    } else {
        try {
            test.atDay(day);
            fail();
        } catch (DateTimeException ex) {
            // expected
        }
    }
}
 
Example 12
Source File: TCKYearMonth.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="atDay")
public void test_atDay(YearMonth test, int day, LocalDate expected) {
    if (expected != null) {
        assertEquals(test.atDay(day), expected);
    } else {
        try {
            test.atDay(day);
            fail();
        } catch (DateTimeException ex) {
            // expected
        }
    }
}
 
Example 13
Source File: TCKYearMonth.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="atDay")
public void test_atDay(YearMonth test, int day, LocalDate expected) {
    if (expected != null) {
        assertEquals(test.atDay(day), expected);
    } else {
        try {
            test.atDay(day);
            fail();
        } catch (DateTimeException ex) {
            // expected
        }
    }
}
 
Example 14
Source File: TCKYearMonth.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="atDay")
public void test_atDay(YearMonth test, int day, LocalDate expected) {
    if (expected != null) {
        assertEquals(test.atDay(day), expected);
    } else {
        try {
            test.atDay(day);
            fail();
        } catch (DateTimeException ex) {
            // expected
        }
    }
}
 
Example 15
Source File: TCKYearMonth.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="atDay")
public void test_atDay(YearMonth test, int day, LocalDate expected) {
    if (expected != null) {
        assertEquals(test.atDay(day), expected);
    } else {
        try {
            test.atDay(day);
            fail();
        } catch (DateTimeException ex) {
            // expected
        }
    }
}
 
Example 16
Source File: TCKYearMonth.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="atDay")
public void test_atDay(YearMonth test, int day, LocalDate expected) {
    if (expected != null) {
        assertEquals(test.atDay(day), expected);
    } else {
        try {
            test.atDay(day);
            fail();
        } catch (DateTimeException ex) {
            // expected
        }
    }
}
 
Example 17
Source File: PDTFactory.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Nullable
public static LocalDate createLocalDate (@Nullable final YearMonth aYM)
{
  return aYM == null ? null : aYM.atDay (1);
}
 
Example 18
Source File: JFXDatePickerContent.java    From JFoenix with Apache License 2.0 4 votes vote down vote up
private void updateDayCells() {
    Locale locale = getLocale();
    Chronology chrono = getPrimaryChronology();
    // get the index of the first day of the month
    int firstDayOfWeek = WeekFields.of(getLocale()).getFirstDayOfWeek().getValue();
    int firstOfMonthIndex = selectedYearMonth.get().atDay(1).getDayOfWeek().getValue() - firstDayOfWeek;
    firstOfMonthIndex += firstOfMonthIndex < 0 ? daysPerWeek : 0;
    YearMonth currentYearMonth = selectedYearMonth.get();

    int daysInCurMonth = -1;

    for (int i = 0; i < 6 * daysPerWeek; i++) {
        DateCell dayCell = dayCells.get(i);
        dayCell.getStyleClass().setAll("cell", "date-cell", "day-cell");
        dayCell.setPrefSize(40, 42);
        dayCell.setDisable(false);
        dayCell.setStyle(null);
        dayCell.setGraphic(null);
        dayCell.setTooltip(null);
        dayCell.setTextFill(DEFAULT_COLOR);
        dayCell.setBackground(new Background(new BackgroundFill(Color.TRANSPARENT,
            CornerRadii.EMPTY,
            Insets.EMPTY)));

        try {
            if (daysInCurMonth == -1) {
                daysInCurMonth = currentYearMonth.lengthOfMonth();
            }

            int dayIndex = i - firstOfMonthIndex + 1;

            LocalDate date = currentYearMonth.atDay(dayIndex);
            dayCellDates[i] = date;

            // if it's today
            if (date.equals(LocalDate.now())) {
                dayCell.setTextFill(this.datePicker.getDefaultColor());
                dayCell.getStyleClass().add("today");
            }
            // if it's the current selected value
            if (date.equals(datePicker.getValue())) {
                dayCell.getStyleClass().add("selected");
                dayCell.setTextFill(Color.WHITE);
                dayCell.setBackground(
                    new Background(new BackgroundFill(this.datePicker.getDefaultColor(),
                        new CornerRadii(40),
                        Insets.EMPTY)));
            }

            ChronoLocalDate cDate = chrono.date(date);
            String cellText = dayCellFormatter.withLocale(locale)
                .withChronology(chrono)
                .withDecimalStyle(DecimalStyle.of(locale))
                .format(cDate);
            dayCell.setText(cellText);
            if (i < firstOfMonthIndex) {
                dayCell.getStyleClass().add("previous-month");
                dayCell.setText("");
            } else if (i >= firstOfMonthIndex + daysInCurMonth) {
                dayCell.getStyleClass().add("next-month");
                dayCell.setText("");
            }
            // update cell item
            dayCell.updateItem(date, false);
        } catch (DateTimeException ex) {
            // Disable day cell if its date is out of range
            dayCell.setText("");
            dayCell.setDisable(true);
        }
    }
}
 
Example 19
Source File: Date.java    From baleen with Apache License 2.0 4 votes vote down vote up
private void identifyDates(TextBlock block) {
  Pattern fullDateDayMonth =
      Pattern.compile(
          "\\b" + DAYS + DATES + DATE_SUFFIXES + "?\\s+" + MONTHS + ",?\\s+(\\d{4}|'?\\d{2}\\b)",
          Pattern.CASE_INSENSITIVE);
  String text = block.getCoveredText();
  Matcher m = fullDateDayMonth.matcher(text);

  while (m.find()) {
    createDateFromMatcher(block, m, 16, 3, 1);
  }

  Pattern fullDateMonthDay =
      Pattern.compile(
          "\\b"
              + MONTHS
              + "\\s+([0-2]?[0-9]|3[01])\\s*"
              + DATE_SUFFIXES
              + "?,?\\s+(\\d{4}|'?\\d{2}\\b)",
          Pattern.CASE_INSENSITIVE);
  m = fullDateMonthDay.matcher(text);

  while (m.find()) {
    createDateFromMatcher(block, m, 16, 1, 14);
  }

  Pattern shortDateYearFirst =
      Pattern.compile(
          "\\b(\\d{4})[-\\\\/\\.](0?[1-9]|1[0-2])[-\\\\/\\.]([0-2]?[0-9]|3[01])\\b",
          Pattern.CASE_INSENSITIVE);
  m = shortDateYearFirst.matcher(text);

  while (m.find()) {
    createDateFromMatcher(block, m, 1, 2, 3);
  }

  Pattern shortDate =
      Pattern.compile(
          "\\b([0-2]?[0-9]|3[01])[-\\\\/\\.]([0-2]?[0-9]|3[01])[-\\\\/\\.](\\d{4}|\\d{2})\\b",
          Pattern.CASE_INSENSITIVE);
  m = shortDate.matcher(text);

  while (m.find()) {
    Year y = DateTimeUtils.asYear(m.group(3));

    Integer n1 = Integer.parseInt(m.group(1));
    Integer n2 = Integer.parseInt(m.group(2));

    Integer day;
    Integer month;
    if (n1 >= 1 && n1 <= 12) {
      // n1 could be a month or a day
      if (n2 >= 12 && n2 <= 31) {
        // n2 must be a day
        month = n1;
        day = n2;
      } else if (n2 >= 1 && n2 <= 12) {
        if (americanDates) {
          day = n2;
          month = n1;
        } else {
          day = n1;
          month = n2;
        }
      } else {
        // invalid combination of n1 and n2
        continue;
      }
    } else if (n1 >= 1 && n1 <= 31) {
      // n1 must be a day
      day = n1;
      if (n2 >= 1 && n2 <= 12) {
        // n2 must be a month
        month = n2;
      } else {
        // invalid combination of n1 and n2
        continue;
      }
    } else {
      // n1 can't be a month or a day
      continue;
    }

    YearMonth ym = y.atMonth(month);

    LocalDate ld;
    try {
      ld = ym.atDay(day);
    } catch (DateTimeException dte) {
      getMonitor().warn(INVALID_DATE_FOUND, dte);
      continue;
    }

    createDate(block, m.start(), m.end(), ld);
  }
}
 
Example 20
Source File: Date.java    From baleen with Apache License 2.0 4 votes vote down vote up
private void identifyMonths(TextBlock block) {
  Pattern monthYear =
      Pattern.compile(
          "\\b((beginning of|start of|early|mid|late|end of)[- ])?"
              + MONTHS
              + "\\s+(\\d{4}|'?\\d{2}\\b)",
          Pattern.CASE_INSENSITIVE);
  String text = block.getCoveredText();
  Matcher m = monthYear.matcher(text);

  while (m.find()) {
    Year y = DateTimeUtils.asYear(m.group(16));
    String month = m.group(3);

    if (month.endsWith(".")) {
      month = month.substring(0, month.length() - 1);
    }

    YearMonth ym = y.atMonth(DateTimeUtils.asMonth(month));

    if (m.group(2) != null) {
      LocalDate ld1;
      LocalDate ld2;
      switch (m.group(2).toLowerCase()) {
        case "beginning of":
        case "start of":
          ld1 = ym.atDay(1);
          ld2 = ym.atDay(5);
          break;
        case "early":
          ld1 = ym.atDay(1);
          ld2 = ym.atDay(10);
          break;
        case "mid":
          ld1 = ym.atDay(11);
          ld2 = ym.atDay(20);
          break;
        case "late":
          ld1 = ym.atDay(21);
          ld2 = ym.atEndOfMonth();
          break;
        case "end of":
          ld1 = ym.atEndOfMonth().minusDays(5);
          ld2 = ym.atEndOfMonth();
          break;
        default:
          continue;
      }

      createDayMonthYearRange(block, m.start(), m.end(), ld1, ld2);
    } else {
      createMonth(block, m.start(), m.end(), ym);
    }
  }
}