/** * See {@link android.text.format.DateUtils#isToday} for full docs. * * @return true if the supplied when is today else false */ public static boolean isToday(ReadableInstant time) { return LocalDate.now().compareTo(new LocalDate(time)) == 0; }
private void sampleLocalDate() { List<String> text = new ArrayList<String>(); LocalDate now = LocalDate.now(); text.add("Now: " + now); text.add("Now + 2 days: " + now.plusDays(2)); text.add("Now + 3 months: " + now.plusMonths(3)); addSample("LocalDate", text); }
private void sampleIsToday() { List<String> text = new ArrayList<String>(); LocalDate today = LocalDate.now(); text.add("Today: " + DateUtils.isToday(today)); text.add("Tomorrow: " + DateUtils.isToday(today.plusDays(1))); text.add("Yesterday: " + DateUtils.isToday(today.minusDays(1))); addSample("DateUtils.isToday()", text); }
/** * See {@link android.text.format.DateUtils#isToday} for full docs. * * @return true if the supplied when is today else false */ public static boolean isToday(ReadablePartial time) { if (!time.isSupported(DateTimeFieldType.dayOfMonth()) || !time.isSupported(DateTimeFieldType.monthOfYear()) || !time.isSupported(DateTimeFieldType.year())) { throw new IllegalArgumentException("isToday() must be passed a ReadablePartial that supports day of " + "month, month of year and year."); } LocalDate localDate = time instanceof LocalDate ? (LocalDate) time : new LocalDate(time); return LocalDate.now().compareTo(localDate) == 0; }
@Test(expected = MissingSchemaException.class) public void testSpecificByteArrayIncompatibleWithoutLogicalTypes() throws IOException { TestRecordWithLogicalTypes withLogicalTypes = new TestRecordWithLogicalTypes( true, 34, 35L, 3.14F, 3019.34, null, LocalDate.now(), LocalTime.now(), DateTime.now().withZone(DateTimeZone.UTC), new BigDecimal("123.45") ); ByteBuffer b = withLogicalTypes.toByteBuffer(); TestRecordWithoutLogicalTypes.fromByteBuffer(b); } }
@Test public void testNullableLogicalTypeInArray() { final NullableLogicalTypesArray logicalTypesArray = NullableLogicalTypesArray.newBuilder().setArrayOfLogicalType(Collections.singletonList(LocalDate.now())).build(); verifySerDeAndStandardMethods(logicalTypesArray); }
@Test public void testSpecificToFromByteBufferWithLogicalTypes() throws IOException { TestRecordWithLogicalTypes record = new TestRecordWithLogicalTypes( true, 34, 35L, 3.14F, 3019.34, null, LocalDate.now(), LocalTime.now(), DateTime.now().withZone(DateTimeZone.UTC), new BigDecimal("123.45") ); ByteBuffer b = record.toByteBuffer(); TestRecordWithLogicalTypes copy = TestRecordWithLogicalTypes.fromByteBuffer(b); assertEquals(record, copy); }
@Test public void testRecordWithLogicalTypes() throws IOException { TestRecordWithLogicalTypes record = new TestRecordWithLogicalTypes( true, 34, 35L, 3.14F, 3019.34, null, LocalDate.now(), LocalTime.now(), DateTime.now().withZone(DateTimeZone.UTC), new BigDecimal(123.45f).setScale(2, RoundingMode.HALF_DOWN) ); File data = write(TestRecordWithLogicalTypes.getClassSchema(), record); List<TestRecordWithLogicalTypes> actual = read( TestRecordWithLogicalTypes.getClassSchema(), data); Assert.assertEquals("Should match written record", record, actual.get(0)); } @Test
@Test public void testDefaultValueOfNullableField() throws IOException { LogicalTypesWithDefaults instanceOfGeneratedClass = LogicalTypesWithDefaults.newBuilder() .setNonNullDate(LocalDate.now()) .build(); verifySerDeAndStandardMethods(instanceOfGeneratedClass); }
@Test public void testDate() throws IOException { NullableLogicalTypes instanceOfGeneratedClass = NullableLogicalTypes.newBuilder() .setNullableDate(LocalDate.now()) .build(); verifySerDeAndStandardMethods(instanceOfGeneratedClass); }
@Test public void testNullableLogicalTypeInRecordInArray() { final NestedLogicalTypesArray nestedLogicalTypesArray = NestedLogicalTypesArray.newBuilder().setArrayOfRecords(Collections.singletonList( RecordInArray.newBuilder().setNullableDateField(LocalDate.now()).build())).build(); verifySerDeAndStandardMethods(nestedLogicalTypesArray); }
@Test public void testNullableLogicalTypeInRecordInMap() { final NestedLogicalTypesMap nestedLogicalTypesMap = NestedLogicalTypesMap.newBuilder().setMapOfRecords(Collections.singletonMap("key", RecordInMap.newBuilder().setNullableDateField(LocalDate.now()).build())).build(); verifySerDeAndStandardMethods(nestedLogicalTypesMap); } }
@Test public void testDefaultValueOfNonNullField() throws IOException { LogicalTypesWithDefaults instanceOfGeneratedClass = LogicalTypesWithDefaults.newBuilder() .setNullableDate(LocalDate.now()) .build(); Assert.assertEquals(DEFAULT_VALUE, instanceOfGeneratedClass.getNonNullDate()); verifySerDeAndStandardMethods(instanceOfGeneratedClass); }
@Test public void testNullableLogicalTypeInNestedRecord() { final NestedLogicalTypesRecord nestedLogicalTypesRecord = NestedLogicalTypesRecord.newBuilder() .setNestedRecord(NestedRecord.newBuilder() .setNullableDateField(LocalDate.now()).build()).build(); verifySerDeAndStandardMethods(nestedLogicalTypesRecord); }
@Test public void testNullableLogicalTypeInRecordInUnion() { final NestedLogicalTypesUnion nestedLogicalTypesUnion = NestedLogicalTypesUnion.newBuilder().setUnionOfRecords( RecordInUnion.newBuilder().setNullableDateField(LocalDate.now()).build()).build(); verifySerDeAndStandardMethods(nestedLogicalTypesUnion); }
@Test public void testRecordWithoutLogicalTypes() throws IOException { // the significance of the record without logical types is that it has the // same schema (besides record name) as the one with logical types, // including the type annotations. this verifies that the type annotations // are only applied if the record was compiled to use those types. this // ensures compatibility with already-compiled code. TestRecordWithoutLogicalTypes record = new TestRecordWithoutLogicalTypes( true, 34, 35L, 3.14F, 3019.34, null, new DateConversion().toInt(LocalDate.now(), null, null), new TimeConversion().toInt(LocalTime.now(), null, null), new TimestampConversion().toLong( DateTime.now().withZone(DateTimeZone.UTC), null, null), new Conversions.DecimalConversion().toBytes( new BigDecimal(123.45f).setScale(2, RoundingMode.HALF_DOWN), null, LogicalTypes.decimal(9, 2)) ); File data = write(TestRecordWithoutLogicalTypes.getClassSchema(), record); List<TestRecordWithoutLogicalTypes> actual = read( TestRecordWithoutLogicalTypes.getClassSchema(), data); Assert.assertEquals("Should match written record", record, actual.get(0)); }
@Test(expected = MissingSchemaException.class) public void testSpecificByteArrayIncompatibleWithLogicalTypes() throws IOException { TestRecordWithoutLogicalTypes withoutLogicalTypes = new TestRecordWithoutLogicalTypes( true, 34, 35L, 3.14F, 3019.34, null, new TimeConversions.DateConversion().toInt(LocalDate.now(), null, null), new TimeConversions.TimeConversion().toInt(LocalTime.now(), null, null), new TimeConversions.TimestampConversion().toLong( DateTime.now().withZone(DateTimeZone.UTC), null, null), new Conversions.DecimalConversion().toBytes( new BigDecimal("123.45"), null, LogicalTypes.decimal(9, 2)) ); ByteBuffer b = withoutLogicalTypes.toByteBuffer(); TestRecordWithLogicalTypes.fromByteBuffer(b); }
@Test public void testSpecificToFromByteBufferWithoutLogicalTypes() throws IOException { TestRecordWithoutLogicalTypes record = new TestRecordWithoutLogicalTypes( true, 34, 35L, 3.14F, 3019.34, null, new TimeConversions.DateConversion().toInt(LocalDate.now(), null, null), new TimeConversions.TimeConversion().toInt(LocalTime.now(), null, null), new TimeConversions.TimestampConversion().toLong( DateTime.now().withZone(DateTimeZone.UTC), null, null), new Conversions.DecimalConversion().toBytes( new BigDecimal("123.45"), null, LogicalTypes.decimal(9, 2)) ); ByteBuffer b = record.toByteBuffer(); TestRecordWithoutLogicalTypes copy = TestRecordWithoutLogicalTypes.fromByteBuffer(b); assertEquals(record, copy); }
@Test public void testGetRelativeTimeSpanStringWithPreposition() { Context ctx = InstrumentationRegistry.getContext(); LocalDate today = LocalDate.now(); LocalDate tomorrow = today.plusDays(1); LocalDate nextYear = today.plusYears(1); assertEquals("12:35", DateUtils.getRelativeTimeSpanString(ctx, today, false)); assertEquals("at 12:35", DateUtils.getRelativeTimeSpanString(ctx, today, true)); assertEquals("Oct 23, 1995", DateUtils.getRelativeTimeSpanString(ctx, tomorrow, false)); assertEquals("on Oct 23, 1995", DateUtils.getRelativeTimeSpanString(ctx, tomorrow, true)); assertEquals("10/22/1996", DateUtils.getRelativeTimeSpanString(ctx, nextYear, false)); assertEquals("on 10/22/1996", DateUtils.getRelativeTimeSpanString(ctx, nextYear, true)); DateTime todayDt = DateTime.now(); DateTime tomorrowDt = todayDt.plusDays(1); DateTime nextYearDt = todayDt.plusYears(1); assertEquals("12:35", DateUtils.getRelativeTimeSpanString(ctx, todayDt, false)); assertEquals("at 12:35", DateUtils.getRelativeTimeSpanString(ctx, todayDt, true)); assertEquals("Oct 23, 1995", DateUtils.getRelativeTimeSpanString(ctx, tomorrowDt, false)); assertEquals("on Oct 23, 1995", DateUtils.getRelativeTimeSpanString(ctx, tomorrowDt, true)); assertEquals("10/22/1996", DateUtils.getRelativeTimeSpanString(ctx, nextYearDt, false)); assertEquals("on 10/22/1996", DateUtils.getRelativeTimeSpanString(ctx, nextYearDt, true)); }