public static void main(String[] args) {
    Period period = Period.of(1, 2, 3); // 1 year, 2 months, 3 days
    Period periodTwoMonths = Period.ofMonths(2);
    System.out.println(period);
    System.out.println(periodTwoMonths);
    Period period20142015 =
        Period.between(LocalDate.of(2014, Month.JANUARY, 1), LocalDate.of(2015, Month.JANUARY, 1));
    Period period20152014 =
        Period.between(LocalDate.of(2015, Month.JANUARY, 1), LocalDate.of(2014, Month.JANUARY, 1));

    System.out.println("2014-2015:" + period20142015);
    System.out.println("2015-2014:" + period20152014);
  }
 @Test
 public void testGetNextFirstOrFifteenthOfTheMonthWithNullBaseDateReturnsValue() {
   LocalDate today = LocalDate.now();
   LocalDate adjustmentDate = AmortizationCalculator.getNextFirstOrFifteenthOfTheMonth(null);
   long daysBetween = Period.between(today, adjustmentDate).getDays();
   assertTrue("Adjustment date from the 1st should remain on the 1st", daysBetween <= 16);
 }
  @Override
  public String convert(LocalDateTime dateTime) {
    LocalDateTime now = LocalDateTime.now();
    Duration duration = Duration.between(dateTime, now);

    Period period = Period.between(dateTime.toLocalDate(), now.toLocalDate());

    if (duration.toMinutes() < 1) {
      return String.format("%s secs ago", duration.getSeconds());
    }

    if (duration.toHours() < 1) {
      return String.format("%s mins ago", duration.toMinutes());
    }

    if (duration.toDays() < 1) {
      return String.format("%s hrs ago", duration.toHours());
    }

    if (period.getYears() > 0) {
      return DateTimeFormatter.ISO_LOCAL_DATE.format(dateTime);
    }

    if (period.getMonths() > 0) {
      return String.format("%s months ago", period.getMonths());
    }

    return String.format("%s days ago", period.getDays());
  }
Exemple #4
0
  @Test
  public void testBasic() {
    System.out.println(Year.now());
    System.out.println(Year.of(2015));
    System.out.println(Year.isLeap(2016));

    Locale locale = Locale.getDefault();
    System.out.println(Month.DECEMBER.getDisplayName(TextStyle.FULL, locale));
    System.out.println(Month.DECEMBER.getDisplayName(TextStyle.SHORT, locale));
    System.out.println(Month.DECEMBER.getDisplayName(TextStyle.NARROW, locale));
    System.out.println(Month.DECEMBER.getDisplayName(TextStyle.FULL_STANDALONE, locale));
    System.out.println(Month.of(8).ordinal());
    System.out.println(Month.AUGUST.minus(2));

    System.out.println(YearMonth.now());
    System.out.println(MonthDay.now());

    System.out.println(DayOfWeek.FRIDAY.plus(2));
    System.out.println(DayOfWeek.of(1));
    System.out.println(
        DayOfWeek.from(LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault())));

    System.out.println(Period.between(Year.now().atDay(1), LocalDate.now()));
    System.out.println(ChronoUnit.DAYS.between(Year.now().atDay(1), LocalDate.now()));
  }
  public static void main(String[] args) {
    LocalDate hoje = LocalDate.now();
    System.out.println(hoje);

    LocalDate olimpiadasRio = LocalDate.of(2016, Month.JUNE, 5);
    System.out.println(olimpiadasRio);

    int dias = olimpiadasRio.getDayOfYear() - hoje.getDayOfYear();
    System.out.println(dias + " dias para as Olímpiadas no Rio!");

    Period periodo = Period.between(hoje, olimpiadasRio);
    System.out.println(periodo.getMonths() + " meses e " + periodo.getDays() + " dias.");

    DateTimeFormatter formatador = DateTimeFormatter.ofPattern("dd/MM/yyyy");
    String valorFormatado = olimpiadasRio.format(formatador);
    System.out.println(valorFormatado);

    System.out.println(" --- Medida de tempo ---");

    DateTimeFormatter formatadorComHoras = DateTimeFormatter.ofPattern("dd/MM/yyyy hh:mm:ss");
    LocalDateTime agora = LocalDateTime.now();
    System.out.println(agora);
    System.out.println(agora.format(formatadorComHoras));

    LocalTime intervalo = LocalTime.of(12, 30);
    System.out.println(intervalo);
  }
  public Period calculateSeniority() {

    LocalDate hireDate = this.getHireDate();
    if (hireDate == null) return null;

    LocalDate now = LocalDate.now();
    return Period.between(hireDate, now);
  }
Exemple #7
0
  private boolean checkList(Container container, List<Container> list) {

    if (list.size() == maxsize) {
      placed = false;

    } else if (list.isEmpty()) {
      list.add(container);
      placed = true;

    } else if (Period.between(list.get(list.size() - 1).getPickupDate(), container.getPickupDate())
        .isNegative()) {
      list.add(container);
      placed = true;
    } else if (Period.between(list.get(list.size() - 1).getPickupDate(), container.getPickupDate())
        .isZero()) {
      list.add(container);
      placed = true;
    } else {
      placed = false;
    }

    return placed;
  }
  private LocalDate getMaxMonthsThresholdDate(ExcelRow item) {
    LocalDate result = null;

    if (item.getDate() != null) {
      if (firstDate == null || firstDate.isAfter(item.getDate())) {
        firstDate = item.getDate();
      }

      final LocalDate startDate = firstDate;
      final LocalDate endDate = LocalDate.now();
      final Period period = Period.between(startDate, endDate);

      /** If the period between the first date and today is greater or equal to maxNumberOfMonth */
      if (period.toTotalMonths() >= maxNumberOfMonth) {
        result = endDate;
      }
    }

    return result;
  }
Exemple #9
0
  @Test
  public void testName() throws Exception {
    LocalDate date = LocalDate.now();
    System.out.println(date);
    LocalDate date2 = date.plusYears(3);
    System.out.println(date2);

    System.out.println(LocalDate.of(2014, 7, 11).isBefore(date));
    System.out.println(LocalDate.of(2014, 7, 11).isAfter(date));

    LocalDate date3 = date.with(lastDayOfMonth());
    System.out.println(date3);

    LocalDate date4 = date.with(next(MONDAY));
    System.out.println(date4);

    Period period = Period.between(date, date2);
    System.out.println(period);

    long between = ChronoUnit.DAYS.between(date, date2);
    System.out.println(between);
  }
  private void testPeriod() {
    out.printf("--------------------- Test Period ------------------------------\n");

    birthDays
        .entrySet()
        .stream()
        .map(
            e -> {
              // string to Time
              return Tuple.from(e.getKey(), stringToTime(e.getValue()));
            })
        .map(
            t -> {
              // Time to Period
              return Tuple.from(t._1, Period.between(LocalDate.from(t._2), LocalDate.now()));
            })
        .forEach(
            t -> {
              out.printf(
                  "%s was born %s years, %s months, %s days ago.\n",
                  t._1, t._2.getYears(), t._2.getMonths(), t._2.getDays());
            });
  }
  public void update(List<Client> clientSet, List<Payment> payments) {
    Object[] columnNames =
        new Object[] {
          "#Cliente",
          "Nombre",
          "Apellido",
          "Celular",
          "Inscripción",
          "Domicilio",
          "Correo electrónico",
          "Próxima cuota"
        };

    Map<Integer, List<Payment>> paymentsByIds = new HashMap<>(clientSet.size());
    for (Payment payment : payments) {
      paymentsByIds.getOrDefault(payment.getClientId(), new ArrayList<>()).add(payment);

      List<Payment> idPayments = paymentsByIds.get(payment.getClientId());
      if (idPayments == null) {
        idPayments = new ArrayList<>();
        paymentsByIds.put(payment.getClientId(), idPayments);
      }
      idPayments.add(payment);
    }

    Object[][] data = new Object[clientSet.size()][columnNames.length];
    int i = 0;
    for (Client client : clientSet) {
      data[i][COLUMN_CLIENT] = client;
      data[i][COLUMN_FIRSTNAME] = client.getFirstName();
      data[i][COLUMN_LASTNAME] = client.getLastName();
      data[i][COLUMN_PHONENUMBER] = client.getPhoneNumber();
      data[i][COLUMN_INSCRIPTIONDATE] = client.getInscriptionDate().format(Utils.DATE_FORMATTER);
      data[i][COLUMN_HOMEADDRESS] = client.getHomeAddress();
      data[i][COLUMN_MAIL] = client.getMail();

      String finalString = "Nunca";
      Optional<Payment> lastPayment = getClientLastPayment(client, paymentsByIds);
      if (lastPayment.isPresent()) {
        LocalDate nextMonthToPay = lastPayment.get().getMonthDate().plusMonths(1);
        Period periodBetweenNowAndNextMonthToPay = Period.between(LocalDate.now(), nextMonthToPay);
        finalString = readablePeriod(periodBetweenNowAndNextMonthToPay);
      }
      data[i][COLUMN_NEXTPAYMENT] = finalString;
      i++;
    }
    TableModel tableModel = new DefaultTableModel(data, columnNames);
    setModel(tableModel);

    RowFilter<Object, Object> filter =
        new RowFilter<Object, Object>() {
          @Override
          public boolean include(Entry entry) {
            Client client = (Client) entry.getValue(COLUMN_CLIENT);
            if (!inactiveClientsVisible && client.isInactive()) {
              return false;
            }

            if (filteringWords == null) {
              return true;
            }

            for (String word : filteringWords) {
              if (!entryContainsWord(entry, word)) {
                return false;
              }
            }
            return true;
          }
        };

    TableRowSorter<TableModel> sorter = new TableRowSorter<>(tableModel);
    sorter.setRowFilter(filter);
    setRowSorter(sorter);
  }
 public static void main(String[] args) {
   System.out.println(Period.between(LocalDate.of(2015, 7, 10), LocalDate.now()));
   System.out.println(Period.between(LocalDate.of(2015, 7, 10), LocalDate.of(2015, 9, 27)));
 }
 private long monateZwischen(LocalDate erstesDatum, LocalDate zweitesDatumInklusive) {
   return Period.between(erstesDatum, zweitesDatumInklusive.plusDays(1)).toTotalMonths();
 }