001package org.apache.commons.ssl.org.bouncycastle.asn1.eac; 002 003import java.text.ParseException; 004import java.text.SimpleDateFormat; 005import java.util.Date; 006import java.util.Locale; 007import java.util.SimpleTimeZone; 008 009import org.bouncycastle.util.Arrays; 010 011/** 012 * EAC encoding date object 013 */ 014public class PackedDate 015{ 016 private byte[] time; 017 018 public PackedDate( 019 String time) 020 { 021 this.time = convert(time); 022 } 023 024 /** 025 * Base constructor from a java.util.date object. 026 * 027 * @param time a date object representing the time of interest. 028 */ 029 public PackedDate( 030 Date time) 031 { 032 SimpleDateFormat dateF = new SimpleDateFormat("yyMMdd'Z'"); 033 034 dateF.setTimeZone(new SimpleTimeZone(0,"Z")); 035 036 this.time = convert(dateF.format(time)); 037 } 038 039 /** 040 * Base constructor from a java.util.date object. You may need to use this constructor if the default locale 041 * doesn't use a Gregorian calender so that the PackedDate produced is compatible with other ASN.1 implementations. 042 * 043 * @param time a date object representing the time of interest. 044 * @param locale an appropriate Locale for producing an ASN.1 GeneralizedTime value. 045 */ 046 public PackedDate( 047 Date time, 048 Locale locale) 049 { 050 SimpleDateFormat dateF = new SimpleDateFormat("yyMMdd'Z'", locale); 051 052 dateF.setTimeZone(new SimpleTimeZone(0,"Z")); 053 054 this.time = convert(dateF.format(time)); 055 } 056 057 private byte[] convert(String sTime) 058 { 059 char[] digs = sTime.toCharArray(); 060 byte[] date = new byte[6]; 061 062 for (int i = 0; i != 6; i++) 063 { 064 date[i] = (byte)(digs[i] - '0'); 065 } 066 067 return date; 068 } 069 070 PackedDate( 071 byte[] bytes) 072 { 073 this.time = bytes; 074 } 075 076 /** 077 * return the time as a date based on whatever a 2 digit year will return. For 078 * standardised processing use getAdjustedDate(). 079 * 080 * @return the resulting date 081 * @exception java.text.ParseException if the date string cannot be parsed. 082 */ 083 public Date getDate() 084 throws ParseException 085 { 086 SimpleDateFormat dateF = new SimpleDateFormat("yyyyMMdd"); 087 088 return dateF.parse("20" + toString()); 089 } 090 091 public int hashCode() 092 { 093 return Arrays.hashCode(time); 094 } 095 096 public boolean equals(Object o) 097 { 098 if (!(o instanceof PackedDate)) 099 { 100 return false; 101 } 102 103 PackedDate other = (PackedDate)o; 104 105 return Arrays.areEqual(time, other.time); 106 } 107 108 public String toString() 109 { 110 char[] dateC = new char[time.length]; 111 112 for (int i = 0; i != dateC.length; i++) 113 { 114 dateC[i] = (char)((time[i] & 0xff) + '0'); 115 } 116 117 return new String(dateC); 118 } 119 120 public byte[] getEncoding() 121 { 122 return time; 123 } 124}