diff options
author | David Hook <dgh@cryptoworkshop.com> | 2013-05-31 11:07:45 +0400 |
---|---|---|
committer | David Hook <dgh@cryptoworkshop.com> | 2013-05-31 11:07:45 +0400 |
commit | 2b976f5364cfdbc37d3086019d93483c983eb80b (patch) | |
tree | cb846af3fd1d43f9c2562a1fb2d06b997ad8f229 /core/src/main/java/org/bouncycastle/asn1/eac/PackedDate.java | |
parent | 5f714bd92fbd780d22406f4bc3681be005f6f04a (diff) |
initial reshuffle
Diffstat (limited to 'core/src/main/java/org/bouncycastle/asn1/eac/PackedDate.java')
-rw-r--r-- | core/src/main/java/org/bouncycastle/asn1/eac/PackedDate.java | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/core/src/main/java/org/bouncycastle/asn1/eac/PackedDate.java b/core/src/main/java/org/bouncycastle/asn1/eac/PackedDate.java new file mode 100644 index 00000000..29b08812 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/asn1/eac/PackedDate.java @@ -0,0 +1,103 @@ +package org.bouncycastle.asn1.eac; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.SimpleTimeZone; + +import org.bouncycastle.util.Arrays; + +/** + * EAC encoding date object + */ +public class PackedDate +{ + private byte[] time; + + public PackedDate( + String time) + { + this.time = convert(time); + } + + /** + * base constructer from a java.util.date object + */ + public PackedDate( + Date time) + { + SimpleDateFormat dateF = new SimpleDateFormat("yyMMdd'Z'"); + + dateF.setTimeZone(new SimpleTimeZone(0,"Z")); + + this.time = convert(dateF.format(time)); + } + + private byte[] convert(String sTime) + { + char[] digs = sTime.toCharArray(); + byte[] date = new byte[6]; + + for (int i = 0; i != 6; i++) + { + date[i] = (byte)(digs[i] - '0'); + } + + return date; + } + + PackedDate( + byte[] bytes) + { + this.time = bytes; + } + + /** + * return the time as a date based on whatever a 2 digit year will return. For + * standardised processing use getAdjustedDate(). + * + * @return the resulting date + * @exception java.text.ParseException if the date string cannot be parsed. + */ + public Date getDate() + throws ParseException + { + SimpleDateFormat dateF = new SimpleDateFormat("yyyyMMdd"); + + return dateF.parse("20" + toString()); + } + + public int hashCode() + { + return Arrays.hashCode(time); + } + + public boolean equals(Object o) + { + if (!(o instanceof PackedDate)) + { + return false; + } + + PackedDate other = (PackedDate)o; + + return Arrays.areEqual(time, other.time); + } + + public String toString() + { + char[] dateC = new char[time.length]; + + for (int i = 0; i != dateC.length; i++) + { + dateC[i] = (char)((time[i] & 0xff) + '0'); + } + + return new String(dateC); + } + + public byte[] getEncoding() + { + return time; + } +} |