1 17 18 package org.apache.geronimo.util.asn1; 19 20 import java.io.IOException ; 21 import java.text.ParseException ; 22 import java.text.SimpleDateFormat ; 23 import java.util.Date ; 24 import java.util.SimpleTimeZone ; 25 26 29 public class DERGeneralizedTime 30 extends DERObject 31 { 32 String time; 33 34 39 public static DERGeneralizedTime getInstance( 40 Object obj) 41 { 42 if (obj == null || obj instanceof DERGeneralizedTime) 43 { 44 return (DERGeneralizedTime)obj; 45 } 46 47 if (obj instanceof ASN1OctetString) 48 { 49 return new DERGeneralizedTime(((ASN1OctetString)obj).getOctets()); 50 } 51 52 throw new IllegalArgumentException ("illegal object in getInstance: " + obj.getClass().getName()); 53 } 54 55 64 public static DERGeneralizedTime getInstance( 65 ASN1TaggedObject obj, 66 boolean explicit) 67 { 68 return getInstance(obj.getObject()); 69 } 70 71 79 public DERGeneralizedTime( 80 String time) 81 { 82 this.time = time; 83 } 84 85 88 public DERGeneralizedTime( 89 Date time) 90 { 91 SimpleDateFormat dateF = new SimpleDateFormat ("yyyyMMddHHmmss'Z'"); 92 93 dateF.setTimeZone(new SimpleTimeZone (0,"Z")); 94 95 this.time = dateF.format(time); 96 } 97 98 DERGeneralizedTime( 99 byte[] bytes) 100 { 101 char[] dateC = new char[bytes.length]; 105 106 for (int i = 0; i != dateC.length; i++) 107 { 108 dateC[i] = (char)(bytes[i] & 0xff); 109 } 110 111 this.time = new String (dateC); 112 } 113 114 126 public String getTime() 127 { 128 if (time.charAt(time.length() - 1) == 'Z') 132 { 133 return time.substring(0, time.length() - 1) + "GMT+00:00"; 134 } 135 else 136 { 137 int signPos = time.length() - 5; 138 char sign = time.charAt(signPos); 139 if (sign == '-' || sign == '+') 140 { 141 return time.substring(0, signPos) 142 + "GMT" 143 + time.substring(signPos, signPos + 3) 144 + ":" 145 + time.substring(signPos + 3); 146 } 147 else 148 { 149 signPos = time.length() - 3; 150 sign = time.charAt(signPos); 151 if (sign == '-' || sign == '+') 152 { 153 return time.substring(0, signPos) 154 + "GMT" 155 + time.substring(signPos) 156 + ":00"; 157 } 158 } 159 } 160 161 return time; 162 } 163 164 public Date getDate() 165 throws ParseException 166 { 167 SimpleDateFormat dateF; 168 169 if (time.indexOf('.') == 14) 170 { 171 dateF = new SimpleDateFormat ("yyyyMMddHHmmss.SSS'Z'"); 172 } 173 else 174 { 175 dateF = new SimpleDateFormat ("yyyyMMddHHmmss'Z'"); 176 } 177 178 dateF.setTimeZone(new SimpleTimeZone (0, "Z")); 179 180 return dateF.parse(time); 181 } 182 183 private byte[] getOctets() 184 { 185 char[] cs = time.toCharArray(); 186 byte[] bs = new byte[cs.length]; 187 188 for (int i = 0; i != cs.length; i++) 189 { 190 bs[i] = (byte)cs[i]; 191 } 192 193 return bs; 194 } 195 196 197 void encode( 198 DEROutputStream out) 199 throws IOException 200 { 201 out.writeEncoded(GENERALIZED_TIME, this.getOctets()); 202 } 203 204 public boolean equals( 205 Object o) 206 { 207 if ((o == null) || !(o instanceof DERGeneralizedTime)) 208 { 209 return false; 210 } 211 212 return time.equals(((DERGeneralizedTime)o).time); 213 } 214 215 public int hashCode() 216 { 217 return time.hashCode(); 218 } 219 } 220 | Popular Tags |