@Test public void testEncodeGeneralizedTime() { Calendar cal = Calendar.getInstance(); cal.clear(); cal.set(Calendar.YEAR, 2010); cal.set(Calendar.MONTH, Calendar.JULY); cal.set(Calendar.DAY_OF_MONTH, 12); cal.set(Calendar.HOUR_OF_DAY, 21); cal.set(Calendar.MINUTE, 45); cal.set(Calendar.SECOND, 27); cal.setTimeZone(TimeZone.getTimeZone("GMT")); Date date = cal.getTime(); int size = 17; ByteBuffer buf = ByteBuffer.wrap(new byte[size]); buf.position(buf.position() + size); int len = Asn1Utils.encodeGeneralizedTime(date, buf); assertEquals(size, len); assertEquals(0, buf.position()); assertEquals((byte) 0x18, buf.get(0)); assertEquals((byte) 0xf, buf.get(1)); assertEquals((byte) 0x32, buf.get(2)); assertEquals((byte) 0x30, buf.get(3)); assertEquals((byte) 0x31, buf.get(4)); assertEquals((byte) 0x30, buf.get(5)); assertEquals((byte) 0x30, buf.get(6)); assertEquals((byte) 0x37, buf.get(7)); assertEquals((byte) 0x31, buf.get(8)); assertEquals((byte) 0x32, buf.get(9)); assertEquals((byte) 0x32, buf.get(10)); assertEquals((byte) 0x31, buf.get(11)); assertEquals((byte) 0x34, buf.get(12)); assertEquals((byte) 0x35, buf.get(13)); assertEquals((byte) 0x32, buf.get(14)); assertEquals((byte) 0x37, buf.get(15)); assertEquals((byte) 0x5a, buf.get(16)); }
public static int encodeGeneralizedTime(Date date, ByteBuffer buf) { if (date == null) { throw new IllegalArgumentException(); } int pos = buf.position(); SimpleDateFormat format = new SimpleDateFormat(GENERALIZED_TIME_FORMAT); format.setTimeZone(TimeZone.getTimeZone("GMT")); String value = format.format(date); byte[] data = value.getBytes(); for (int i = data.length - 1; i >= 0; i--) { pos--; buf.put(pos, data[i]); } buf.position(buf.position() - data.length); int headerLength = DerUtils.encodeIdAndLength(DerId.TagClass.UNIVERSAL, DerId.EncodingType.PRIMITIVE, ASN1_GENERALIZED_TIME_TAG_NUM, data.length, buf); return headerLength + data.length; }
Asn1Utils { public static int encodeGeneralizedTime(Date date, ByteBuffer buf) { if (date == null) { throw new IllegalArgumentException(); } int pos = buf.position(); SimpleDateFormat format = new SimpleDateFormat(GENERALIZED_TIME_FORMAT); format.setTimeZone(TimeZone.getTimeZone("GMT")); String value = format.format(date); byte[] data = value.getBytes(); for (int i = data.length - 1; i >= 0; i--) { pos--; buf.put(pos, data[i]); } buf.position(buf.position() - data.length); int headerLength = DerUtils.encodeIdAndLength(DerId.TagClass.UNIVERSAL, DerId.EncodingType.PRIMITIVE, ASN1_GENERALIZED_TIME_TAG_NUM, data.length, buf); return headerLength + data.length; } }
Asn1Utils { public static int encodeGeneralizedTime(Date date, ByteBuffer buf) { if (date == null) { throw new IllegalArgumentException(); } int pos = buf.position(); SimpleDateFormat format = new SimpleDateFormat(GENERALIZED_TIME_FORMAT); format.setTimeZone(TimeZone.getTimeZone("GMT")); String value = format.format(date); byte[] data = value.getBytes(); for (int i = data.length - 1; i >= 0; i--) { pos--; buf.put(pos, data[i]); } buf.position(buf.position() - data.length); int headerLength = DerUtils.encodeIdAndLength(DerId.TagClass.UNIVERSAL, DerId.EncodingType.PRIMITIVE, ASN1_GENERALIZED_TIME_TAG_NUM, data.length, buf); return headerLength + data.length; } private  Asn1Utils(); }
Asn1Utils { public static int encodeGeneralizedTime(Date date, ByteBuffer buf) { if (date == null) { throw new IllegalArgumentException(); } int pos = buf.position(); SimpleDateFormat format = new SimpleDateFormat(GENERALIZED_TIME_FORMAT); format.setTimeZone(TimeZone.getTimeZone("GMT")); String value = format.format(date); byte[] data = value.getBytes(); for (int i = data.length - 1; i >= 0; i--) { pos--; buf.put(pos, data[i]); } buf.position(buf.position() - data.length); int headerLength = DerUtils.encodeIdAndLength(DerId.TagClass.UNIVERSAL, DerId.EncodingType.PRIMITIVE, ASN1_GENERALIZED_TIME_TAG_NUM, data.length, buf); return headerLength + data.length; } private  Asn1Utils(); static BitSet decodeBitString(ByteBuffer buf); static Date decodeGeneralizedTime(ByteBuffer buf); static String decodeIA5String(ByteBuffer buf); static int decodeInteger(ByteBuffer buf); static short[] decodeOctetString(ByteBuffer buf); static int decodeSequence(ByteBuffer buf); static int encodeBitString(BitSet value, int nbits, ByteBuffer buf); static int encodeGeneralizedTime(Date date, ByteBuffer buf); static int encodeIA5String(String value, ByteBuffer buf); static int encodeInteger(int value, ByteBuffer buf); static int encodeOctetString(short[] octets, ByteBuffer buf); static int encodeSequence(int contentLength, ByteBuffer buf); static int sizeOfBitString(BitSet value, int nbits); static int sizeOfGeneralizedTime(Date date); static int sizeOfIA5String(String value); static int sizeOfInteger(int value); static int sizeOfOctetString(short[] octets); static int sizeOfSequence(int length); }
Asn1Utils { public static int encodeGeneralizedTime(Date date, ByteBuffer buf) { if (date == null) { throw new IllegalArgumentException(); } int pos = buf.position(); SimpleDateFormat format = new SimpleDateFormat(GENERALIZED_TIME_FORMAT); format.setTimeZone(TimeZone.getTimeZone("GMT")); String value = format.format(date); byte[] data = value.getBytes(); for (int i = data.length - 1; i >= 0; i--) { pos--; buf.put(pos, data[i]); } buf.position(buf.position() - data.length); int headerLength = DerUtils.encodeIdAndLength(DerId.TagClass.UNIVERSAL, DerId.EncodingType.PRIMITIVE, ASN1_GENERALIZED_TIME_TAG_NUM, data.length, buf); return headerLength + data.length; } private  Asn1Utils(); static BitSet decodeBitString(ByteBuffer buf); static Date decodeGeneralizedTime(ByteBuffer buf); static String decodeIA5String(ByteBuffer buf); static int decodeInteger(ByteBuffer buf); static short[] decodeOctetString(ByteBuffer buf); static int decodeSequence(ByteBuffer buf); static int encodeBitString(BitSet value, int nbits, ByteBuffer buf); static int encodeGeneralizedTime(Date date, ByteBuffer buf); static int encodeIA5String(String value, ByteBuffer buf); static int encodeInteger(int value, ByteBuffer buf); static int encodeOctetString(short[] octets, ByteBuffer buf); static int encodeSequence(int contentLength, ByteBuffer buf); static int sizeOfBitString(BitSet value, int nbits); static int sizeOfGeneralizedTime(Date date); static int sizeOfIA5String(String value); static int sizeOfInteger(int value); static int sizeOfOctetString(short[] octets); static int sizeOfSequence(int length);  }