1 package com.lowagie.bc.asn1; 2 3 import java.io.IOException; 4 import java.text.ParseException; 5 import java.text.SimpleDateFormat; 6 import java.util.Date; 7 import java.util.SimpleTimeZone; 8 9 12 public class DERGeneralizedTime 13 extends DERObject 14 { 15 String time; 16 17 22 public static DERGeneralizedTime getInstance( 23 Object obj) 24 { 25 if (obj == null || obj instanceof DERGeneralizedTime) 26 { 27 return (DERGeneralizedTime)obj; 28 } 29 30 if (obj instanceof ASN1OctetString) 31 { 32 return new DERGeneralizedTime(((ASN1OctetString)obj).getOctets()); 33 } 34 35 throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName()); 36 } 37 38 47 public static DERGeneralizedTime getInstance( 48 ASN1TaggedObject obj, 49 boolean explicit) 50 { 51 return getInstance(obj.getObject()); 52 } 53 54 62 public DERGeneralizedTime( 63 String time) 64 { 65 this.time = time; 66 } 67 68 71 public DERGeneralizedTime( 72 Date time) 73 { 74 SimpleDateFormat dateF = new SimpleDateFormat("yyyyMMddHHmmss'Z'"); 75 76 dateF.setTimeZone(new SimpleTimeZone(0,"Z")); 77 78 this.time = dateF.format(time); 79 } 80 81 DERGeneralizedTime( 82 byte[] bytes) 83 { 84 char[] dateC = new char[bytes.length]; 88 89 for (int i = 0; i != dateC.length; i++) 90 { 91 dateC[i] = (char)(bytes[i] & 0xff); 92 } 93 94 this.time = new String(dateC); 95 } 96 97 109 public String getTime() 110 { 111 if (time.charAt(time.length() - 1) == 'Z') 115 { 116 return time.substring(0, time.length() - 1) + "GMT+00:00"; 117 } 118 else 119 { 120 int signPos = time.length() - 5; 121 char sign = time.charAt(signPos); 122 if (sign == '-' || sign == '+') 123 { 124 return time.substring(0, signPos) 125 + "GMT" 126 + time.substring(signPos, signPos + 3) 127 + ":" 128 + time.substring(signPos + 3); 129 } 130 else 131 { 132 signPos = time.length() - 3; 133 sign = time.charAt(signPos); 134 if (sign == '-' || sign == '+') 135 { 136 return time.substring(0, signPos) 137 + "GMT" 138 + time.substring(signPos) 139 + ":00"; 140 } 141 } 142 } 143 144 return time; 145 } 146 147 public Date getDate() 148 throws ParseException 149 { 150 SimpleDateFormat dateF = new SimpleDateFormat("yyyyMMddHHmmss'Z'"); 151 152 dateF.setTimeZone(new SimpleTimeZone(0, "Z")); 153 154 return dateF.parse(time); 155 } 156 157 private byte[] getOctets() 158 { 159 char[] cs = time.toCharArray(); 160 byte[] bs = new byte[cs.length]; 161 162 for (int i = 0; i != cs.length; i++) 163 { 164 bs[i] = (byte)cs[i]; 165 } 166 167 return bs; 168 } 169 170 171 void encode( 172 DEROutputStream out) 173 throws IOException 174 { 175 out.writeEncoded(GENERALIZED_TIME, this.getOctets()); 176 } 177 178 public boolean equals( 179 Object o) 180 { 181 if ((o == null) || !(o instanceof DERGeneralizedTime)) 182 { 183 return false; 184 } 185 186 return time.equals(((DERGeneralizedTime)o).time); 187 } 188 } 189 | Popular Tags |