@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());
  }
Exemplo n.º 2
0
  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);
  }
Exemplo n.º 3
0
 /**
  * Subtracts the period of this frequency from the specified date.
  *
  * <p>This method implements {@link TemporalAmount}. It is not intended to be called directly. Use
  * {@link LocalDate#minus(TemporalAmount)} instead.
  *
  * @param temporal the temporal object to subtract from
  * @return the result with this frequency subtracted
  * @throws DateTimeException if unable to subtract
  * @throws ArithmeticException if numeric overflow occurs
  */
 @Override
 public Temporal subtractFrom(Temporal temporal) {
   // special case for performance
   if (temporal instanceof LocalDate) {
     LocalDate date = (LocalDate) temporal;
     return date.minusMonths(period.toTotalMonths()).minusDays(period.getDays());
   }
   return period.subtractFrom(temporal);
 }
Exemplo n.º 4
0
 /**
  * Adds the period of this frequency to the specified date.
  *
  * <p>This method implements {@link TemporalAmount}. It is not intended to be called directly. Use
  * {@link LocalDate#plus(TemporalAmount)} instead.
  *
  * @param temporal the temporal object to add to
  * @return the result with this frequency added
  * @throws DateTimeException if unable to add
  * @throws ArithmeticException if numeric overflow occurs
  */
 @Override
 public Temporal addTo(Temporal temporal) {
   // special case for performance
   if (temporal instanceof LocalDate) {
     LocalDate date = (LocalDate) temporal;
     return date.plusMonths(period.toTotalMonths()).plusDays(period.getDays());
   }
   return period.addTo(temporal);
 }
Exemplo n.º 5
0
 @Test
 public void testGetAge() {
   LocalDate now = LocalDate.now();
   int currentYear = now.getYear();
   LocalDate birthDate = LocalDate.of(2010, 2, 1);
   Period age = AgeUtils.getAge(birthDate); // P6Y1M21D
   Assert.assertEquals(currentYear - birthDate.getYear(), age.getYears());
   Assert.assertEquals(now.getMonthValue() - birthDate.getMonthValue(), age.getMonths());
   Assert.assertEquals(now.getDayOfMonth() - birthDate.getDayOfMonth(), age.getDays());
 }
Exemplo n.º 6
0
 /**
  * Obtains a periodic frequency from a {@code Period}.
  *
  * <p>The period normally consists of either days and weeks, or months and years. It must also be
  * positive and non-zero.
  *
  * <p>If the number of days is an exact multiple of 7 it will be converted to weeks. Months are
  * not normalized into years.
  *
  * <p>The maximum tenor length is 1,000 years.
  *
  * @param period the period to convert to a periodic frequency
  * @return the periodic frequency
  * @throws IllegalArgumentException if the period is negative, zero or too large
  */
 public static Frequency of(Period period) {
   ArgChecker.notNull(period, "period");
   int days = period.getDays();
   long months = period.toTotalMonths();
   if (months == 0 && days != 0) {
     return ofDays(days);
   }
   if (months > MAX_MONTHS) {
     throw new IllegalArgumentException("Period must not exceed 1000 years");
   }
   return new Frequency(period);
 }
 private String readablePeriod(Period period) {
   if (period.isZero()) return "Hoy";
   String startsWith = period.isNegative() ? "Hace " : "En ";
   long months = Math.abs(period.toTotalMonths());
   int days = Math.abs(period.getDays());
   if (Math.abs(months) > 0) {
     String endsWith = (months > 1) ? "es" : "";
     return startsWith + months + " mes" + endsWith;
   } else {
     String endsWith = (days > 1) ? "s" : "";
     return startsWith + days + " dia" + endsWith;
   }
 }
Exemplo n.º 8
0
  @Test
  public void testGetAgeDetails() {
    LocalDate to = LocalDate.of(2011, 1, 10);

    LocalDate from = LocalDate.of(2010, 1, 10);
    Period age = AgeUtils.getAge(from, to);
    Assert.assertEquals(1, age.getYears());
    Assert.assertEquals(0, age.getMonths());
    Assert.assertEquals(0, age.getDays());

    from = LocalDate.of(2010, 1, 11);
    age = AgeUtils.getAge(from, to);
    Assert.assertEquals(0, age.getYears());
    Assert.assertEquals(11, age.getMonths());
    Assert.assertEquals(30, age.getDays());

    from = LocalDate.of(2010, 5, 11);
    age = AgeUtils.getAge(from, to);
    Assert.assertEquals(0, age.getYears());
    Assert.assertEquals(7, age.getMonths());
    Assert.assertEquals(30, age.getDays());
  }
Exemplo n.º 9
0
 /**
  * Calculates the number of events that occur in a year.
  *
  * <p>The number of events per year is the number of times that the period occurs per year. Not
  * all periodic frequency instances can be converted to an integer events per year. All constants
  * declared on this class will return a result.
  *
  * <p>Month-based and year-based periodic frequencies are converted by dividing 12 by the number
  * of months. Only the following periodic frequencies return a value - P1M, P2M, P3M, P4M, P6M,
  * P1Y.
  *
  * <p>Day-based and week-based periodic frequencies are converted by dividing 364 by the number of
  * days. Only the following periodic frequencies return a value - P1D, P2D, P4D, P1W, P2W, P4W,
  * P13W, P26W, P52W.
  *
  * <p>The 'Term' periodic frequency returns zero.
  *
  * @return the number of events per year
  * @throws IllegalArgumentException if unable to calculate the number of events per year
  */
 public int eventsPerYear() {
   if (isTerm()) {
     return 0;
   }
   long months = period.toTotalMonths();
   int days = period.getDays();
   if (isMonthBased()) {
     if (12 % months == 0) {
       return (int) (12 / months);
     }
   } else if (months == 0 && 364 % days == 0) {
     return (int) (364 / days);
   }
   throw new IllegalArgumentException("Unable to calculate events per year: " + this);
 }
Exemplo n.º 10
0
 /**
  * Checks if the periodic frequency is month-based.
  *
  * <p>A month-based frequency consists of an integral number of months. Any year-based frequency
  * is also counted as month-based. There must be no day or week element.
  *
  * @return true if this is month-based
  */
 public boolean isMonthBased() {
   return period.toTotalMonths() > 0 && period.getDays() == 0 && isTerm() == false;
 }
Exemplo n.º 11
0
 /**
  * Checks if the periodic frequency is week-based.
  *
  * <p>A week-based frequency consists of an integral number of weeks. There must be no day, month
  * or year element.
  *
  * @return true if this is week-based
  */
 public boolean isWeekBased() {
   return period.toTotalMonths() == 0 && period.getDays() % 7 == 0;
 }