1 17 18 package org.apache.geronimo.util.asn1.x509; 19 20 import java.text.ParsePosition ; 21 import java.text.SimpleDateFormat ; 22 import java.util.Date ; 23 import java.util.SimpleTimeZone ; 24 25 import org.apache.geronimo.util.asn1.ASN1Choice; 26 import org.apache.geronimo.util.asn1.ASN1Encodable; 27 import org.apache.geronimo.util.asn1.ASN1TaggedObject; 28 import org.apache.geronimo.util.asn1.DERGeneralizedTime; 29 import org.apache.geronimo.util.asn1.DERObject; 30 import org.apache.geronimo.util.asn1.DERUTCTime; 31 32 public class Time 33 extends ASN1Encodable 34 implements ASN1Choice 35 { 36 DERObject time; 37 38 public static Time getInstance( 39 ASN1TaggedObject obj, 40 boolean explicit) 41 { 42 return getInstance(obj.getObject()); } 44 45 public Time( 46 DERObject time) 47 { 48 if (!(time instanceof DERUTCTime) 49 && !(time instanceof DERGeneralizedTime)) 50 { 51 throw new IllegalArgumentException ("unknown object passed to Time"); 52 } 53 54 this.time = time; 55 } 56 57 62 public Time( 63 Date date) 64 { 65 SimpleTimeZone tz = new SimpleTimeZone (0, "Z"); 66 SimpleDateFormat dateF = new SimpleDateFormat ("yyyyMMddHHmmss"); 67 68 dateF.setTimeZone(tz); 69 70 String d = dateF.format(date) + "Z"; 71 int year = Integer.parseInt(d.substring(0, 4)); 72 73 if (year < 1950 || year > 2049) 74 { 75 time = new DERGeneralizedTime(d); 76 } 77 else 78 { 79 time = new DERUTCTime(d.substring(2)); 80 } 81 } 82 83 public static Time getInstance( 84 Object obj) 85 { 86 if (obj instanceof Time) 87 { 88 return (Time)obj; 89 } 90 else if (obj instanceof DERUTCTime) 91 { 92 return new Time((DERUTCTime)obj); 93 } 94 else if (obj instanceof DERGeneralizedTime) 95 { 96 return new Time((DERGeneralizedTime)obj); 97 } 98 99 throw new IllegalArgumentException ("unknown object in factory"); 100 } 101 102 public String getTime() 103 { 104 if (time instanceof DERUTCTime) 105 { 106 return ((DERUTCTime)time).getAdjustedTime(); 107 } 108 else 109 { 110 return ((DERGeneralizedTime)time).getTime(); 111 } 112 } 113 114 public Date getDate() 115 { 116 SimpleDateFormat dateF = new SimpleDateFormat ("yyyyMMddHHmmssz"); 117 118 return dateF.parse(this.getTime(), new ParsePosition (0)); 119 } 120 121 129 public DERObject toASN1Object() 130 { 131 return time; 132 } 133 } 134 | Popular Tags |