@Test
  public void testInsert() {

    SomePeriodBean bean = new SomePeriodBean();
    bean.setPeriod(Period.of(3, 4, 5));
    bean.setAnniversary(MonthDay.of(4, 29));
    Ebean.save(bean);

    SomePeriodBean bean1 = Ebean.find(SomePeriodBean.class, bean.getId());
    assertEquals(bean.getPeriod(), bean1.getPeriod());
    assertEquals(bean.getAnniversary(), bean1.getAnniversary());

    // insert fetch null value
    SomePeriodBean bean2 = new SomePeriodBean();
    Ebean.save(bean2);

    SomePeriodBean bean3 = Ebean.find(SomePeriodBean.class, bean2.getId());
    assertNull(bean3.getPeriod());
    assertNull(bean3.getAnniversary());

    List<SomePeriodBean> anniversaryList =
        Ebean.find(SomePeriodBean.class)
            .where()
            .eq("anniversary", MonthDay.of(4, 29))
            .eq("period_years", 3)
            .eq("period_months", 4)
            .findList();

    assertEquals(1, anniversaryList.size());

    // must use year 2000 for range predicates
    // ... using 2001 here so not finding anything
    anniversaryList =
        Ebean.find(SomePeriodBean.class)
            .where()
            .gt("anniversary", Date.valueOf(LocalDate.of(2001, 4, 29)))
            .findList();

    assertEquals(0, anniversaryList.size());

    // can use year 2000 for range predicates
    // ... and can use LocalDate to bind
    anniversaryList =
        Ebean.find(SomePeriodBean.class)
            .where()
            .gt("anniversary", LocalDate.of(2000, 4, 22))
            .findList();

    assertEquals(1, anniversaryList.size());

    Ebean.delete(bean);
    Ebean.delete(bean2);
  }
Exemple #2
0
  @Test
  public void shouldPlayWithDates() {
    LocalDate d1 = LocalDate.now();
    LocalDate d2 = LocalDate.of(2014, 2, 15).plusDays(10);
    print(d1);
    print(d2);

    LocalDateTime d3 = d2.atTime(13, 23, 34, 45);
    assertThat(d3.toString(), is("2014-02-25T13:23:34.000000045"));
    LocalDateTime d4 = d3.plus(Period.of(1, 2, 3));
    assertThat(d4.toString(), is("2015-04-28T13:23:34.000000045"));
  }
  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);
  }
Exemple #4
0
 static Period readExternal(DataInput in) throws IOException {
   int years = in.readInt();
   int months = in.readInt();
   int days = in.readInt();
   return Period.of(years, months, days);
 }