Source Code Cross Referenced for DERUTCTime.java in  » Security » Bouncy-Castle » org » bouncycastle » asn1 » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Security » Bouncy Castle » org.bouncycastle.asn1 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.bouncycastle.asn1;
002:
003:        import java.io.IOException;
004:        import java.text.ParseException;
005:        import java.text.SimpleDateFormat;
006:        import java.util.Date;
007:        import java.util.SimpleTimeZone;
008:
009:        /**
010:         * UTC time object.
011:         */
012:        public class DERUTCTime extends ASN1Object {
013:            String time;
014:
015:            /**
016:             * return an UTC Time from the passed in object.
017:             *
018:             * @exception IllegalArgumentException if the object cannot be converted.
019:             */
020:            public static DERUTCTime getInstance(Object obj) {
021:                if (obj == null || obj instanceof  DERUTCTime) {
022:                    return (DERUTCTime) obj;
023:                }
024:
025:                if (obj instanceof  ASN1OctetString) {
026:                    return new DERUTCTime(((ASN1OctetString) obj).getOctets());
027:                }
028:
029:                throw new IllegalArgumentException(
030:                        "illegal object in getInstance: "
031:                                + obj.getClass().getName());
032:            }
033:
034:            /**
035:             * return an UTC Time from a tagged object.
036:             *
037:             * @param obj the tagged object holding the object we want
038:             * @param explicit true if the object is meant to be explicitly
039:             *              tagged false otherwise.
040:             * @exception IllegalArgumentException if the tagged object cannot
041:             *               be converted.
042:             */
043:            public static DERUTCTime getInstance(ASN1TaggedObject obj,
044:                    boolean explicit) {
045:                return getInstance(obj.getObject());
046:            }
047:
048:            /**
049:             * The correct format for this is YYMMDDHHMMSSZ (it used to be that seconds were
050:             * never encoded. When you're creating one of these objects from scratch, that's
051:             * what you want to use, otherwise we'll try to deal with whatever gets read from
052:             * the input stream... (this is why the input format is different from the getTime()
053:             * method output).
054:             * <p>
055:             *
056:             * @param time the time string.
057:             */
058:            public DERUTCTime(String time) {
059:                this .time = time;
060:                try {
061:                    this .getDate();
062:                } catch (ParseException e) {
063:                    throw new IllegalArgumentException("invalid date string: "
064:                            + e.getMessage());
065:                }
066:            }
067:
068:            /**
069:             * base constructer from a java.util.date object
070:             */
071:            public DERUTCTime(Date time) {
072:                SimpleDateFormat dateF = new SimpleDateFormat("yyMMddHHmmss'Z'");
073:
074:                dateF.setTimeZone(new SimpleTimeZone(0, "Z"));
075:
076:                this .time = dateF.format(time);
077:            }
078:
079:            DERUTCTime(byte[] bytes) {
080:                //
081:                // explicitly convert to characters
082:                //
083:                char[] dateC = new char[bytes.length];
084:
085:                for (int i = 0; i != dateC.length; i++) {
086:                    dateC[i] = (char) (bytes[i] & 0xff);
087:                }
088:
089:                this .time = new String(dateC);
090:            }
091:
092:            /**
093:             * return the time as a date based on whatever a 2 digit year will return. For
094:             * standardised processing use getAdjustedDate().
095:             *
096:             * @return the resulting date
097:             * @exception ParseException if the date string cannot be parsed.
098:             */
099:            public Date getDate() throws ParseException {
100:                SimpleDateFormat dateF = new SimpleDateFormat("yyMMddHHmmssz");
101:
102:                return dateF.parse(getTime());
103:            }
104:
105:            /**
106:             * return the time as an adjusted date
107:             * in the range of 1950 - 2049.
108:             *
109:             * @return a date in the range of 1950 to 2049.
110:             * @exception ParseException if the date string cannot be parsed.
111:             */
112:            public Date getAdjustedDate() throws ParseException {
113:                SimpleDateFormat dateF = new SimpleDateFormat("yyyyMMddHHmmssz");
114:
115:                dateF.setTimeZone(new SimpleTimeZone(0, "Z"));
116:
117:                return dateF.parse(getAdjustedTime());
118:            }
119:
120:            /**
121:             * return the time - always in the form of 
122:             *  YYMMDDhhmmssGMT(+hh:mm|-hh:mm).
123:             * <p>
124:             * Normally in a certificate we would expect "Z" rather than "GMT",
125:             * however adding the "GMT" means we can just use:
126:             * <pre>
127:             *     dateF = new SimpleDateFormat("yyMMddHHmmssz");
128:             * </pre>
129:             * To read in the time and get a date which is compatible with our local
130:             * time zone.
131:             * <p>
132:             * <b>Note:</b> In some cases, due to the local date processing, this
133:             * may lead to unexpected results. If you want to stick the normal
134:             * convention of 1950 to 2049 use the getAdjustedTime() method.
135:             */
136:            public String getTime() {
137:                //
138:                // standardise the format.
139:                //
140:                if (time.indexOf('-') < 0 && time.indexOf('+') < 0) {
141:                    if (time.length() == 11) {
142:                        return time.substring(0, 10) + "00GMT+00:00";
143:                    } else {
144:                        return time.substring(0, 12) + "GMT+00:00";
145:                    }
146:                } else {
147:                    int index = time.indexOf('-');
148:                    if (index < 0) {
149:                        index = time.indexOf('+');
150:                    }
151:                    String d = time;
152:
153:                    if (index == time.length() - 3) {
154:                        d += "00";
155:                    }
156:
157:                    if (index == 10) {
158:                        return d.substring(0, 10) + "00GMT"
159:                                + d.substring(10, 13) + ":"
160:                                + d.substring(13, 15);
161:                    } else {
162:                        return d.substring(0, 12) + "GMT" + d.substring(12, 15)
163:                                + ":" + d.substring(15, 17);
164:                    }
165:                }
166:            }
167:
168:            /**
169:             * return a time string as an adjusted date with a 4 digit year. This goes
170:             * in the range of 1950 - 2049.
171:             */
172:            public String getAdjustedTime() {
173:                String d = this .getTime();
174:
175:                if (d.charAt(0) < '5') {
176:                    return "20" + d;
177:                } else {
178:                    return "19" + d;
179:                }
180:            }
181:
182:            private byte[] getOctets() {
183:                char[] cs = time.toCharArray();
184:                byte[] bs = new byte[cs.length];
185:
186:                for (int i = 0; i != cs.length; i++) {
187:                    bs[i] = (byte) cs[i];
188:                }
189:
190:                return bs;
191:            }
192:
193:            void encode(DEROutputStream out) throws IOException {
194:                out.writeEncoded(UTC_TIME, this .getOctets());
195:            }
196:
197:            boolean asn1Equals(DERObject o) {
198:                if (!(o instanceof  DERUTCTime)) {
199:                    return false;
200:                }
201:
202:                return time.equals(((DERUTCTime) o).time);
203:            }
204:
205:            public int hashCode() {
206:                return time.hashCode();
207:            }
208:
209:            public String toString() {
210:                return time;
211:            }
212:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.