1 19 20 package com.maverick.crypto.asn1; 21 22 import java.io.IOException ; 23 import java.text.SimpleDateFormat ; 24 import java.util.Date ; 25 import java.util.SimpleTimeZone ; 26 27 30 public class DERUTCTime 31 extends DERObject 32 { 33 String time; 34 35 40 public static DERUTCTime getInstance( 41 Object obj) 42 { 43 if (obj == null || obj instanceof DERUTCTime) 44 { 45 return (DERUTCTime)obj; 46 } 47 48 if (obj instanceof ASN1OctetString) 49 { 50 return new DERUTCTime(((ASN1OctetString)obj).getOctets()); 51 } 52 53 throw new IllegalArgumentException ("illegal object in getInstance: " + obj.getClass().getName()); 54 } 55 56 65 public static DERUTCTime getInstance( 66 ASN1TaggedObject obj, 67 boolean explicit) 68 { 69 return getInstance(obj.getObject()); 70 } 71 72 82 public DERUTCTime( 83 String time) 84 { 85 this.time = time; 86 } 87 88 91 public DERUTCTime( 92 Date time) 93 { 94 SimpleDateFormat dateF = new SimpleDateFormat ("yyMMddHHmmss'Z'"); 95 96 dateF.setTimeZone(new SimpleTimeZone (0,"Z")); 97 98 this.time = dateF.format(time); 99 } 100 101 DERUTCTime( 102 byte[] bytes) 103 { 104 char[] dateC = new char[bytes.length]; 108 109 for (int i = 0; i != dateC.length; i++) 110 { 111 dateC[i] = (char)(bytes[i] & 0xff); 112 } 113 114 this.time = new String (dateC); 115 } 116 117 133 public String getTime() 134 { 135 if (time.length() == 11) 139 { 140 return time.substring(0, 10) + "00GMT+00:00"; 141 } 142 else if (time.length() == 13) 143 { 144 return time.substring(0, 12) + "GMT+00:00"; 145 } 146 else if (time.length() == 17) 147 { 148 return time.substring(0, 12) + "GMT" + time.substring(12, 15) + ":" + time.substring(15, 17); 149 } 150 151 return time; 152 } 153 154 158 public String getAdjustedTime() 159 { 160 String d = this.getTime(); 161 162 if (d.charAt(0) < '5') 163 { 164 return "20" + d; 165 } 166 else 167 { 168 return "19" + d; 169 } 170 } 171 172 private byte[] getOctets() 173 { 174 char[] cs = time.toCharArray(); 175 byte[] bs = new byte[cs.length]; 176 177 for (int i = 0; i != cs.length; i++) 178 { 179 bs[i] = (byte)cs[i]; 180 } 181 182 return bs; 183 } 184 185 void encode( 186 DEROutputStream out) 187 throws IOException 188 { 189 out.writeEncoded(UTC_TIME, this.getOctets()); 190 } 191 192 public boolean equals( 193 Object o) 194 { 195 if ((o == null) || !(o instanceof DERUTCTime)) 196 { 197 return false; 198 } 199 200 return time.equals(((DERUTCTime)o).time); 201 } 202 } 203 | Popular Tags |