@Test
 public void test1() {
   final LocalDate startDate = LocalDate.of(2000, 1, 1);
   final LocalDate endDate = LocalDate.of(2002, 2, 9);
   final int months = 25;
   final LocalDate[] forward = CALCULATOR.getSchedule(startDate, endDate, false, true);
   assertEquals(forward.length, months);
   final LocalDate firstDate = LocalDate.of(2000, 1, 31);
   assertEquals(forward[0], firstDate);
   final LocalDate lastDate = LocalDate.of(2002, 1, 31);
   assertEquals(forward[months - 1], lastDate);
   LocalDate d1;
   for (int i = 1; i < months; i++) {
     d1 = forward[i];
     if (d1.getYear() == forward[i - 1].getYear()) {
       assertEquals(d1.getMonthValue() - forward[i - 1].getMonthValue(), 1);
     } else {
       assertEquals(d1.getMonthValue() - forward[i - 1].getMonthValue(), -11);
     }
     assertEquals(d1.getDayOfMonth(), d1.lengthOfMonth());
   }
   assertArrayEquals(CALCULATOR.getSchedule(startDate, endDate, true, false), forward);
   assertArrayEquals(CALCULATOR.getSchedule(startDate, endDate, true, true), forward);
   assertArrayEquals(CALCULATOR.getSchedule(startDate, endDate, false, false), forward);
   assertArrayEquals(CALCULATOR.getSchedule(startDate, endDate, false, true), forward);
   assertArrayEquals(CALCULATOR.getSchedule(startDate, endDate), forward);
 }
Beispiel #2
0
  static void f1() {
    LocalDate date = LocalDate.of(2016, Month.JANUARY, 24);
    int year = date.getYear(); // 2016
    Month month = date.getMonth(); // 1月
    int dom = date.getDayOfMonth(); // 24
    DayOfWeek dow = date.getDayOfWeek(); // 星期天,SUNDAY
    int len = date.lengthOfMonth(); // 31 (1月份的天数)
    boolean leap = date.isLeapYear(); // true (是闰年)

    date = date.withYear(2015); // 2015-01-24
    date = date.plusMonths(2); // 2015-03-24
    date = date.minusDays(1); // 2015-03-23

    System.out.println(dow);
  }
  private static void useLocalDate() {
    LocalDate date = LocalDate.of(2014, 3, 18);
    int year = date.getYear(); // 2014
    Month month = date.getMonth(); // MARCH
    int day = date.getDayOfMonth(); // 18
    DayOfWeek dow = date.getDayOfWeek(); // TUESDAY
    int len = date.lengthOfMonth(); // 31 (days in March)
    boolean leap = date.isLeapYear(); // false (not a leap year)
    System.out.println(date);

    int y = date.get(ChronoField.YEAR);
    int m = date.get(ChronoField.MONTH_OF_YEAR);
    int d = date.get(ChronoField.DAY_OF_MONTH);

    LocalTime time = LocalTime.of(13, 45, 20); // 13:45:20
    int hour = time.getHour(); // 13
    int minute = time.getMinute(); // 45
    int second = time.getSecond(); // 20
    System.out.println(time);

    LocalDateTime dt1 = LocalDateTime.of(2014, Month.MARCH, 18, 13, 45, 20); // 2014-03-18T13:45
    LocalDateTime dt2 = LocalDateTime.of(date, time);
    LocalDateTime dt3 = date.atTime(13, 45, 20);
    LocalDateTime dt4 = date.atTime(time);
    LocalDateTime dt5 = time.atDate(date);
    System.out.println(dt1);

    LocalDate date1 = dt1.toLocalDate();
    System.out.println(date1);
    LocalTime time1 = dt1.toLocalTime();
    System.out.println(time1);

    Instant instant = Instant.ofEpochSecond(44 * 365 * 86400);
    Instant now = Instant.now();

    Duration d1 = Duration.between(LocalTime.of(13, 45, 10), time);
    Duration d2 = Duration.between(instant, now);
    System.out.println(d1.getSeconds());
    System.out.println(d2.getSeconds());

    Duration threeMinutes = Duration.of(3, ChronoUnit.MINUTES);
    System.out.println(threeMinutes);

    JapaneseDate japaneseDate = JapaneseDate.from(date);
    System.out.println(japaneseDate);
  }
  BudgetPeriodDescriptor(
      final LocalDate budgetDate, final int budgetYear, final BudgetPeriod budgetPeriod) {
    Objects.requireNonNull(budgetPeriod);
    Objects.requireNonNull(budgetDate);

    this.budgetYear = budgetYear;
    this.budgetPeriod = budgetPeriod;
    this.startPeriod = budgetDate.getDayOfYear() - 1; // zero based index vs. 1 based day of year

    /* Calendar day 1 is 1.  Need to add 1 to get correct dates */
    startDate = LocalDate.ofYearDay(budgetYear, startPeriod + 1);

    switch (budgetPeriod) {
      case DAILY:
        endDate = startDate;
        endPeriod = startPeriod;

        periodDescription =
            ResourceUtils.getString("Pattern.NumericDate", DateUtils.asDate(startDate));
        break;
      case WEEKLY:
        // Check to see if we are working with the start of a 53 week year.  The first week will be
        // truncated
        if (DateUtils.getNumberOfWeeksInYear(budgetYear) == DateUtils.LEAP_WEEK
            && budgetDate.getYear() < budgetYear) {
          startPeriod = 0;
          startDate = budgetDate;

          endPeriod =
              (int) ChronoUnit.DAYS.between(startDate, LocalDate.of(budgetYear, Month.JANUARY, 1));
          endDate = startDate.plusDays(ONE_WEEK_INCREMENT);
        } else {
          endDate = startDate.plusDays(ONE_WEEK_INCREMENT);
          endPeriod =
              Math.min(
                  startPeriod + ONE_WEEK_INCREMENT,
                  BudgetGoal.PERIODS - 1); // need to cap for 53 week years
        }

        periodDescription =
            ResourceUtils.getString(
                "Pattern.WeekOfYear", DateUtils.getWeekOfTheYear(startDate), budgetYear);
        break;
      case BI_WEEKLY:
        if (DateUtils.getWeekOfTheYear(startDate) != DateUtils.LEAP_WEEK) {
          endDate = startDate.plusDays(TWO_WEEK_INCREMENT);
          endPeriod = startPeriod + TWO_WEEK_INCREMENT;
        } else {
          endDate = startDate.plusDays(ONE_WEEK_INCREMENT);
          endPeriod = startPeriod + ONE_WEEK_INCREMENT;
        }
        periodDescription =
            ResourceUtils.getString(
                "Pattern.DateRangeShort", DateUtils.asDate(startDate), DateUtils.asDate(endDate));
        break;
      case MONTHLY:
        final int days = startDate.lengthOfMonth();
        endDate = DateUtils.getLastDayOfTheMonth(startDate);
        endPeriod = startPeriod + days - 1;

        periodDescription =
            ResourceUtils.getString("Pattern.MonthOfYear", DateUtils.asDate(startDate));
        break;
      case QUARTERLY:
        endDate = DateUtils.getLastDayOfTheQuarter(startDate);
        endPeriod = startPeriod + (int) ChronoUnit.DAYS.between(startDate, endDate);

        periodDescription =
            ResourceUtils.getString(
                "Pattern.QuarterOfYear", DateUtils.getQuarterNumber(startDate), budgetYear);
        break;
      case YEARLY:
        endDate = DateUtils.getLastDayOfTheYear(startDate);
        endPeriod = startPeriod + (int) ChronoUnit.DAYS.between(startDate, endDate);

        periodDescription = Integer.toString(budgetYear);
        break;
      default:
        endPeriod = startPeriod;
        endDate = LocalDate.now();
        periodDescription = "";
    }

    // Periods especially bi-weekly can get weird, correct ending period if needed.
    if (endPeriod > BudgetGoal.PERIODS) {
      endPeriod = BudgetGoal.PERIODS - 1;
      endDate = DateUtils.getLastDayOfTheYear(startDate);
    }
  }