@Override public void writeData(DataOutput dataOutput, MonthDay value) throws IOException { if (value == null) { dataOutput.writeBoolean(false); } else { dataOutput.writeBoolean(true); dataOutput.write(value.getMonthValue()); dataOutput.write(value.getDayOfMonth()); } }
@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); }
@Test public void testJson() throws Exception { MonthDay value = MonthDay.of(4, 29); JsonTester<MonthDay> jsonTester = new JsonTester<>(type); jsonTester.test(value); }
@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 MonthDay toMemberType(int[] vals) { if (vals == null) { return null; } return MonthDay.of(vals[0], vals[1]); }
@Test public void testSerialization02() throws Exception { MonthDay monthDay = MonthDay.of(Month.AUGUST, 21); String value = this.mapper.writeValueAsString(monthDay); assertNotNull("The value should not be null.", value); assertEquals("The value is not correct.", "\"--08-21\"", value); }
@Test public void testDeserialization02() throws Exception { MonthDay monthDay = MonthDay.of(Month.AUGUST, 21); MonthDay value = this.mapper.readValue("\"--08-21\"", MonthDay.class); assertNotNull("The value should not be null.", value); assertEquals("The value is not correct.", monthDay, value); }
@Override public MonthDay readData(DataInput dataInput) throws IOException { if (!dataInput.readBoolean()) { return null; } else { int month = dataInput.readInt(); int day = dataInput.readInt(); return MonthDay.of(month, day); } }
@Test public void testFormatParse() throws Exception { MonthDay value = MonthDay.of(4, 29); String val1 = type.formatValue(value); MonthDay monthDay = type.parse(val1); assertEquals("--04-29", val1); assertEquals(value, monthDay); }
@Test public void testSerializationWithTypeInfo01() throws Exception { MonthDay monthDay = MonthDay.of(Month.NOVEMBER, 5); this.mapper.addMixIn(TemporalAccessor.class, MockObjectConfiguration.class); String value = this.mapper.writeValueAsString(monthDay); assertNotNull("The value should not be null.", value); assertEquals( "The value is not correct.", "[\"" + MonthDay.class.getName() + "\",\"--11-05\"]", value); }
@Test public void testToBeanType() throws Exception { MonthDay value = MonthDay.of(4, 29); Date date = Date.valueOf(LocalDate.of(2000, 4, 29)); MonthDay val1 = type.toBeanType(value); MonthDay val2 = type.toBeanType(date); assertEquals(value, val1); assertEquals(value, val2); }
@Test public void testDeserializationWithTypeInfo01() throws Exception { MonthDay monthDay = MonthDay.of(Month.NOVEMBER, 5); this.mapper.addMixIn(TemporalAccessor.class, MockObjectConfiguration.class); TemporalAccessor value = this.mapper.readValue( "[\"" + MonthDay.class.getName() + "\",\"--11-05\"]", TemporalAccessor.class); assertNotNull("The value should not be null.", value); assertTrue("The value should be a MonthDay.", value instanceof MonthDay); assertEquals("The value is not correct.", monthDay, value); }
@Override MonthDay parse(String stringValue) throws DateTimeParseException { return MonthDay.parse(stringValue, FORMATTER); }
public int[] toDatastoreType(MonthDay md) { if (md == null) { return null; } return new int[] {md.getMonthValue(), md.getDayOfMonth()}; }
private MonthDay convertFromDate(Date ts) { LocalDate localDate = ts.toLocalDate(); return MonthDay.of(localDate.getMonthValue(), localDate.getDayOfMonth()); }
private Date convertToDate(MonthDay value) { return Date.valueOf(LocalDate.of(2000, value.getMonthValue(), value.getDayOfMonth())); }
@Override public String formatValue(MonthDay value) { return value.toString(); }
@Override public MonthDay parse(String value) { return MonthDay.parse(value); }