EncodingSchemeUtils { public static DublinCoreValue encodeDate(Date date, Precision precision) { if (date == null) throw new IllegalArgumentException("The date must not be null"); if (precision == null) throw new IllegalArgumentException("The precision must not be null"); return DublinCoreValue.mk(formatDate(date, precision), DublinCore.LANGUAGE_UNDEFINED, Opt.some(DublinCore.ENC_SCHEME_W3CDTF)); } private  EncodingSchemeUtils(); static DublinCoreValue encodeDate(Date date, Precision precision); static String formatDate(Date date, Precision precision); static DublinCoreValue encodePeriod(DCMIPeriod period, Precision precision); static DublinCoreValue encodeDuration(long duration); static Long decodeDuration(String value); static Long decodeDuration(DublinCoreValue value); static Long decodeMandatoryDuration(String value); static Date decodeDate(DublinCoreValue value); static Date decodeDate(String value); static Date decodeMandatoryDate(DublinCoreValue value); static Date decodeMandatoryDate(String value); static DCMIPeriod decodePeriod(DublinCoreValue value); static DCMIPeriod decodePeriod(String value); static DCMIPeriod decodeMandatoryPeriod(DublinCoreValue value); static DCMIPeriod decodeMandatoryPeriod(String value); static Temporal decodeTemporal(DublinCoreValue value); static Temporal decodeMandatoryTemporal(DublinCoreValue value);  }
@Test public void testEncodeDate() { Date now = new Date(); assertEquals(4, encodeDate(now, Precision.Year).getValue().length()); assertEquals(3, encodeDate(now, Precision.Day).getValue().split("-").length); assertEquals("2009-01-01T00:00:00Z".length(), encodeDate(now, Precision.Second).getValue().length()); assertEquals(Opt.some(DublinCore.ENC_SCHEME_W3CDTF), encodeDate(now, Precision.Year).getEncodingScheme()); assertEquals(decodeDate(encodeDate(now, Precision.Second)), precisionSecond(now)); assertEquals(decodeDate(encodeDate(createDate(1999, 3, 21, 14, 0, 0), Precision.Day)), precisionDay(createDate( 1999, 3, 21, 14, 0, 0))); assertEquals("1724-04-22", encodeDate(createDate(1724, 4, 22, 18, 30, 0), Precision.Day).getValue()); assertEquals("1724-04-22T18:30:00Z", encodeDate(createDate(1724, 4, 22, 18, 30, 0, "UTC"), Precision.Second) .getValue()); assertEquals("1724-04-22T17:30:10Z", encodeDate(createDate(1724, 4, 22, 17, 30, 10, "UTC"), Precision.Second) .getValue()); assertEquals("1724-04-22T17:30Z", encodeDate(createDate(1724, 4, 22, 17, 30, 25, "UTC"), Precision.Minute) .getValue()); assertEquals("1999-03-21", encodeDate(createDate(1999, 3, 21, 18, 30, 25), Precision.Day).getValue()); logger.info(encodeDate(now, Precision.Day).getValue()); logger.info(encodeDate(now, Precision.Second).getValue()); }
@Test public void testEncodeFraction() { Date a = new Date(1); Date b = new Date(125); Date c = new Date(100); assertEquals("1970-01-01T00:00:00.001Z", encodeDate(a, Precision.Fraction).getValue()); assertEquals("1970-01-01T00:00:00.125Z", encodeDate(b, Precision.Fraction).getValue()); assertEquals("1970-01-01T00:00:00.100Z", encodeDate(c, Precision.Fraction).getValue()); }