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