java.time.YearMonth.lengthOfMonth()方法的使用及代码示例

x33g5p2x  于2022-02-05 转载在 其他  
字(10.0k)|赞(0)|评价(0)|浏览(153)

本文整理了Java中java.time.YearMonth.lengthOfMonth()方法的一些代码示例,展示了YearMonth.lengthOfMonth()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。YearMonth.lengthOfMonth()方法的具体详情如下:
包路径:java.time.YearMonth
类名称:YearMonth
方法名:lengthOfMonth

YearMonth.lengthOfMonth介绍

[英]Returns the length of the month, taking account of the year.

This returns the length of the month in days. For example, a date in January would return 31.
[中]返回月份的长度,考虑年份。
这将以天为单位返回月份的长度。例如,1月份的日期将返回31。

代码示例

代码示例来源:origin: jfoenixadmin/JFoenix

  1. daysInCurMonth = currentYearMonth.lengthOfMonth();

代码示例来源:origin: com.github.seratch/java-time-backport

  1. /**
  2. * Checks if the day-of-month is valid for this year-month.
  3. * <p>
  4. * This method checks whether this year and month and the input day form
  5. * a valid date.
  6. *
  7. * @param dayOfMonth the day-of-month to validate, from 1 to 31, invalid value returns false
  8. * @return true if the day is valid for this year-month
  9. */
  10. public boolean isValidDay(int dayOfMonth) {
  11. return dayOfMonth >= 1 && dayOfMonth <= lengthOfMonth();
  12. }

代码示例来源:origin: com.github.seratch/java-time-backport

  1. /**
  2. * Returns a {@code LocalDate} at the end of the month.
  3. * <p>
  4. * This returns a {@code LocalDate} based on this year-month.
  5. * The day-of-month is set to the last valid day of the month, taking
  6. * into account leap years.
  7. * <p>
  8. * This method can be used as part of a chain to produce a date:
  9. * <pre>
  10. * LocalDate date = year.atMonth(month).atEndOfMonth();
  11. * </pre>
  12. *
  13. * @return the last valid date of this year-month, not null
  14. */
  15. public LocalDate atEndOfMonth() {
  16. return LocalDate.of(year, month, lengthOfMonth());
  17. }

代码示例来源:origin: stackoverflow.com

  1. YearMonth yearMonthObject = YearMonth.of(year, month);
  2. int daysOfCurrentMonth = yearMonthObject.lengthOfMonth();
  3. ArrayList<LocalDate> dayes = new ArrayList<LocalDate>();
  4. for(int i = 1; i <= daysOfCurrentMonth; i++){
  5. dayes.add(yearMonthObject.atDay(i));
  6. }
  7. dayes.forEach(value -> System.out.println(value.getDayOfMonth() + " " + value.getDayOfWeek()));

代码示例来源:origin: stackoverflow.com

  1. YearMonth ym = YearMonth.of( 2016 , Month.JANUARY ) ;
  2. int initialCapacity = ( ( ym.lengthOfMonth() / 7 ) + 1 ) * dows.size() ; // Maximum possible weeks * number of days per week.
  3. List<LocalDate> dates = new ArrayList<>( initialCapacity );
  4. for (int dayOfMonth = 1; dayOfMonth <= ym.lengthOfMonth() ; dayOfMonth ++) {
  5. LocalDate ld = ym.atDay( dayOfMonth ) ;
  6. DayOfWeek dow = ld.getDayOfWeek() ;
  7. if( dows.contains( dow ) ) {
  8. // Is this date *is* one of the days we care about, collect it.
  9. dates.add( ld );
  10. }
  11. }

代码示例来源:origin: AnalyticalGraphicsInc/czml-writer

  1. /**
  2. * Returns the number of days in the specified month and year.
  3. *
  4. * @param year
  5. * The year
  6. * @param month
  7. * The month (from 1 to 12)
  8. */
  9. public static int daysInMonth(int year, int month) {
  10. return YearMonth.of(year, month).lengthOfMonth();
  11. }

代码示例来源:origin: org.threeten/threeten-extra

  1. /**
  2. * Combines this day-of-month with a year-month to create a {@code LocalDate}.
  3. * <p>
  4. * This returns a {@code LocalDate} formed from this year and the specified year-month.
  5. * <p>
  6. * If this day-of-month is invalid for the year-month then it will be changed
  7. * to the last valid date for the month.
  8. *
  9. * @param yearMonth the year-month to use, not null
  10. * @return the local date formed from this year and the specified year-month, not null
  11. */
  12. public LocalDate atYearMonth(YearMonth yearMonth) {
  13. return yearMonth.atDay(Math.min(day, yearMonth.lengthOfMonth()));
  14. }

代码示例来源:origin: stackoverflow.com

  1. import java.time.DayOfWeek;
  2. import java.time.DayOfWeek;
  3. import java.time.LocalDate;
  4. import java.time.Month;
  5. import java.time.YearMonth;
  6. import java.util.stream.IntStream;
  7. class Stackoverflow{
  8. public static void main(String args[]){
  9. int year = 2010;
  10. Month month = Month.JANUARY;
  11. IntStream.rangeClosed(1,YearMonth.of(year, month).lengthOfMonth())
  12. .mapToObj(day -> LocalDate.of(year, month, day))
  13. .filter(date -> date.getDayOfWeek() == DayOfWeek.SATURDAY ||
  14. date.getDayOfWeek() == DayOfWeek.SUNDAY)
  15. .forEach(date -> System.out.print(date.getDayOfMonth() + " "));
  16. }
  17. }

代码示例来源:origin: org.tiogasolutions.dev/tioga-dev-common

  1. public LocalDate toLastDate(LocalDate date) {
  2. YearMonth yearMonth = toYearMonth(date);
  3. int lastDayOfMonth = yearMonth.lengthOfMonth();
  4. return (date == null) ? null : LocalDate.of(date.getYear(), date.getMonthValue(), lastDayOfMonth);
  5. }
  6. public LocalDateTime toLastDate(LocalDateTime date) {

代码示例来源:origin: stackoverflow.com

  1. YearMonth ym = ymStart;
  2. do {
  3. int daysInMonth = ym.lengthOfMonth ();
  4. String monthName = ym.getMonth ().getDisplayName ( TextStyle.FULL , Locale.CANADA_FRENCH );
  5. System.out.println ( ym + " : " + daysInMonth + " jours en " + monthName );
  6. // Prepare for next loop.
  7. ym = ym.plusMonths ( 1 );
  8. } while ( ym.isBefore ( ymStop ) );

代码示例来源:origin: tmobile/pacbot

  1. public List<LocalDate> getListOfLastWeekDateOfQuarter() {
  2. LocalDate today = LocalDate.now();
  3. LocalDate startDate = LocalDate.of(today.getYear(), today.getMonth().firstMonthOfQuarter(), 1);
  4. Month firstMonthOftheQuarter = startDate.getMonth();
  5. Month secMonthOftheQuarter = startDate.getMonth().plus(1);
  6. Month thirdMonthOftheQuarter = secMonthOftheQuarter.plus(1);
  7. List<Month> monthList = new ArrayList<>();
  8. monthList.add(firstMonthOftheQuarter);
  9. monthList.add(secMonthOftheQuarter);
  10. monthList.add(thirdMonthOftheQuarter);
  11. int year = today.getYear();
  12. List<LocalDate> lastWeeksOfQuarterList = new ArrayList<>();
  13. for (Month month : monthList) {
  14. IntStream.rangeClosed(1, YearMonth.of(year, month).lengthOfMonth())
  15. .mapToObj(day -> LocalDate.of(year, month, day))
  16. .filter(date -> date.getDayOfWeek() == DayOfWeek.SUNDAY)
  17. .forEach(date -> lastWeeksOfQuarterList.add(date));
  18. }
  19. return lastWeeksOfQuarterList;
  20. }

代码示例来源:origin: de.adorsys.smartanalytics/smartanalytics-classification

  1. int lastDayInMonth = YearMonth.from(nextMonth).lengthOfMonth();
  2. if (lastDayInMonth < nextBookingDay) {
  3. nextBookingDay = lastDayInMonth;

代码示例来源:origin: org.tiogasolutions.dev/tioga-dev-common

  1. public LocalDateTime toLastDate(LocalDateTime date) {
  2. YearMonth yearMonth = toYearMonth(date);
  3. int lastDayOfMonth = yearMonth.lengthOfMonth();
  4. return (date == null) ? null : LocalDateTime.of(date.getYear(), date.getMonthValue(), lastDayOfMonth,
  5. date.getHour(), date.getMinute(), date.getSecond(), date.getNano());
  6. }
  7. public ZonedDateTime toLastDate(ZonedDateTime date) {

代码示例来源:origin: stackoverflow.com

  1. YearMonth yearMonth = startYm;
  2. do {
  3. int days = 0;
  4. if ( startYm.equals ( stopYm ) ) { // If within the same (single) month.
  5. days = ( int ) ChronoUnit.DAYS.between ( start , stop );
  6. } else if ( yearMonth.equals ( startYm ) ) { // If on the first month of multiple months, count days.
  7. days = ( int ) ChronoUnit.DAYS.between ( start , startYm.plusMonths ( 1 ).atDay ( 1 ) ); // Get first of next month, to accommodate the `between` method’s use of Half-Open logic.
  8. } else if ( yearMonth.isAfter ( startYm ) && yearMonth.isBefore ( stopYm ) ) { // If on the in-between months, ask for the days of that month.
  9. days = yearMonth.lengthOfMonth ();
  10. } else if ( yearMonth.equals ( stopYm ) ) { // If on the last of multiple months.
  11. days = ( int ) ChronoUnit.DAYS.between ( stopYm.atDay ( 1 ).minusDays ( 1 ) , stop ); // Get last day of previous month, to accommodate the `between` method’s use of Half-Open logic.
  12. } else {
  13. System.out.println ( "ERROR - Reached impossible point." );
  14. // FIXME: Handle error condition.
  15. }
  16. map.put ( yearMonth , days ); // Cast long to int, auto-boxed to Integer.
  17. // Prep for next loop.
  18. yearMonth = yearMonth.plusMonths ( 1 );
  19. } while ( ! yearMonth.isAfter ( stopYm ) );

代码示例来源:origin: org.tiogasolutions.dev/tioga-dev-common

  1. public ZonedDateTime toLastDate(ZonedDateTime date) {
  2. YearMonth yearMonth = toYearMonth(date);
  3. int lastDayOfMonth = yearMonth.lengthOfMonth();
  4. return (date == null) ? null : ZonedDateTime.of(date.getYear(), date.getMonthValue(), lastDayOfMonth,
  5. date.getHour(), date.getMinute(), date.getSecond(), date.getNano(), date.getZone());
  6. }

代码示例来源:origin: frode-carlsen/cron

  1. boolean matches(LocalDate dato) {
  2. for (FieldPart part : parts) {
  3. if ("L".equals(part.modifier)) {
  4. YearMonth ym = YearMonth.of(dato.getYear(), dato.getMonth().getValue());
  5. return dato.getDayOfWeek() == DayOfWeek.of(part.from) && dato.getDayOfMonth() > (ym.lengthOfMonth() - 7);
  6. } else if ("#".equals(part.incrementModifier)) {
  7. if (dato.getDayOfWeek() == DayOfWeek.of(part.from)) {
  8. int num = dato.getDayOfMonth() / 7;
  9. return part.increment == (dato.getDayOfMonth() % 7 == 0 ? num : num + 1);
  10. }
  11. return false;
  12. } else if (matches(dato.getDayOfWeek().getValue(), part)) {
  13. return true;
  14. }
  15. }
  16. return false;
  17. }

代码示例来源:origin: io.micronaut/runtime

  1. /**
  2. * Check if the date matches the day of the week.
  3. *
  4. * @param date The date
  5. * @return Whether the date matches the day of the field
  6. */
  7. boolean matches(LocalDate date) {
  8. for (FieldPart part : parts) {
  9. if ("L".equals(part.modifier)) {
  10. YearMonth ym = YearMonth.of(date.getYear(), date.getMonth().getValue());
  11. return date.getDayOfWeek() == DayOfWeek.of(part.from) && date.getDayOfMonth() > (ym.lengthOfMonth() - DAYS_IN_WEEK);
  12. } else if ("#".equals(part.incrementModifier)) {
  13. if (date.getDayOfWeek() == DayOfWeek.of(part.from)) {
  14. int num = date.getDayOfMonth() / DAYS_IN_WEEK;
  15. return part.increment == (date.getDayOfMonth() % DAYS_IN_WEEK == 0 ? num : num + 1);
  16. }
  17. return false;
  18. } else if (matches(date.getDayOfWeek().getValue(), part)) {
  19. return true;
  20. }
  21. }
  22. return false;
  23. }

代码示例来源:origin: frode-carlsen/cron

  1. boolean matches(LocalDate dato) {
  2. for (FieldPart part : parts) {
  3. if ("L".equals(part.modifier)) {
  4. YearMonth ym = YearMonth.of(dato.getYear(), dato.getMonth().getValue());
  5. return dato.getDayOfMonth() == (ym.lengthOfMonth() - (part.from == null ? 0 : part.from));
  6. } else if ("W".equals(part.modifier)) {
  7. if (dato.getDayOfWeek().getValue() <= 5) {
  8. if (dato.getDayOfMonth() == part.from) {
  9. return true;
  10. } else if (dato.getDayOfWeek().getValue() == 5) {
  11. return dato.plusDays(1).getDayOfMonth() == part.from;
  12. } else if (dato.getDayOfWeek().getValue() == 1) {
  13. return dato.minusDays(1).getDayOfMonth() == part.from;
  14. }
  15. }
  16. } else if (matches(dato.getDayOfMonth(), part)) {
  17. return true;
  18. }
  19. }
  20. return false;
  21. }

代码示例来源:origin: io.micronaut/runtime

  1. /**
  2. * Check if the given date matches the day in the month.
  3. *
  4. * @param date The date
  5. * @return Whether the date matches the day in the month
  6. */
  7. boolean matches(LocalDate date) {
  8. for (FieldPart part : parts) {
  9. if ("L".equals(part.modifier)) {
  10. YearMonth ym = YearMonth.of(date.getYear(), date.getMonth().getValue());
  11. return date.getDayOfMonth() == (ym.lengthOfMonth() - (part.from == null ? 0 : part.from));
  12. } else if ("W".equals(part.modifier)) {
  13. if (date.getDayOfWeek().getValue() <= WEEK_DAYS) {
  14. if (date.getDayOfMonth() == part.from) {
  15. return true;
  16. } else if (date.getDayOfWeek().getValue() == WEEK_DAYS) {
  17. return date.plusDays(ONE_DAY).getDayOfMonth() == part.from;
  18. } else if (date.getDayOfWeek().getValue() == FIRST_DAY) {
  19. return date.minusDays(ONE_DAY).getDayOfMonth() == part.from;
  20. }
  21. }
  22. } else if (matches(date.getDayOfMonth(), part)) {
  23. return true;
  24. }
  25. }
  26. return false;
  27. }

代码示例来源:origin: com.jfoenix/jfoenix

  1. daysInCurMonth = currentYearMonth.lengthOfMonth();

相关文章