DateTimeCodec implements BlockCodec<Date> { @Override public byte[] encode(Date data, int bitLength) throws BlockCodecException { long value = data.getTime(); try { byte[] temp = new byte[] { (byte) (value >> 56), (byte) (value >> 48), (byte) (value >> 40), (byte) (value >> 32), (byte) (value >> 24), (byte) (value >> 16), (byte) (value >> 8), (byte) value }; int byteStart = 8 - (int) Math.ceil((double) bitLength / 8); int bitStart = bitLength % 8; if (bitStart != 0) { bitStart = 8 - bitStart; } return ByteUtils.copyBits(temp, byteStart, bitStart, bitLength); } catch (Exception ex) { throw new BlockCodecException("Date encode failure. " + ex.getMessage(), ex); } }  @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 testEncode() throws Exception { Date now = new Date(); long time = now.getTime(); DateTimeCodec codec = new DateTimeCodec(); Assert.assertArrayEquals(ByteBuffer.allocate(8).putLong(time).array(), codec.encode(now, 64)); }