// See https://en.wikipedia.org/wiki/Day_count_convention#30E.2F360_ISDA
 private int diff360EIsda(final LocalDate start, final LocalDate end) {
   if (start.equals(end)) {
     return 0;
   }
   int dayStart = start.getDayOfMonth();
   int dayEnd = end.getDayOfMonth();
   if (start.getMonth().length(start.isLeapYear()) == dayStart) {
     dayStart = MONTH_30_DAYS;
   }
   if (end.getMonth() != Month.FEBRUARY && end.getMonth().length(end.isLeapYear()) == dayEnd) {
     dayEnd = MONTH_30_DAYS;
   }
   return (end.getYear() - start.getYear()) * YEAR_360
       + (end.getMonthValue() - start.getMonthValue()) * MONTH_30_DAYS
       + dayEnd
       - dayStart;
 }
  private Month create(final LocalDate year, final LocalDate localMonth) {

    log.debug("Entering with {}, {}", year, localMonth);

    Month month = new Month();

    month.setMonth(localMonth.getMonth());

    for (int i = 0; i < localMonth.getMonth().length(year.isLeapYear()); i++)
      month.getDays().add(create(localMonth.plusDays(i)));

    createWeeks(month);

    return month;
  }
Exemplo n.º 3
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);
  }
Exemplo n.º 4
0
  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);
  }