DateTimeStringCodec implements BlockCodec<Date> { @Override public Date decode(byte[] data, int bitLength) throws BlockCodecException { if (this.nullable) { boolean isNull = true; for (int i = 0; i < data.length; i++) { if (data[i] != this.nullByte) { isNull = false; break; } } if (isNull) { return null; } } try { String value = new String(data); return new SimpleDateFormat(this.format).parse(value); } catch (Exception ex) { if (this.nullable) { return null; } throw new BlockCodecException("date decode failure. " + ex.getMessage(), ex); } } DateTimeStringCodec(); String getNullable(); void setNullable(String nullable); String getNullByte(); void setNullByte(String nullByte); String getFormat(); void setFormat(String format); @Override Date zeroValue(); @Override Date decode(byte[] data, int bitLength); @Override byte[] encode(Date data, int bitLength); @Override void reset(); @Override String getValueType();  }
@Test public void testDecode() throws Exception { DateTimeStringCodec codec = new DateTimeStringCodec(); codec.setFormat("MMddyyyy"); Date dt1 = codec.decode(new byte[] { 0x31, 0x30, 0x31, 0x39, 0x32, 0x30, 0x31, 0x33 }, 8); codec.setFormat("(HHmmssyyyyMMdd)"); Date dt2 = codec.decode("(18462820080604)".getBytes(), 16); Calendar cal = Calendar.getInstance(); cal.setTime(dt1); Assert.assertEquals(2013, cal.get(Calendar.YEAR), 0); Assert.assertEquals(10, cal.get(Calendar.MONTH) + 1, 0); Assert.assertEquals(19, cal.get(Calendar.DATE), 0); cal.setTime(dt2); Assert.assertEquals(2008, cal.get(Calendar.YEAR), 0); Assert.assertEquals(6, cal.get(Calendar.MONTH) + 1, 0); Assert.assertEquals(4, cal.get(Calendar.DATE), 0); Assert.assertEquals(18, cal.get(Calendar.HOUR_OF_DAY), 0); Assert.assertEquals(46, cal.get(Calendar.MINUTE), 0); Assert.assertEquals(28, cal.get(Calendar.SECOND), 0); codec.setFormat("MMddyyyy"); codec.setNullByte("00"); codec.setNullable("y"); Assert.assertEquals(null, codec.decode(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 8)); }