KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > PdfDate


1 /*
2  * $Id: PdfDate.java 2383 2006-09-16 00:10:05Z xlv $
3  * $Name$
4  *
5  * Copyright 1999, 2000, 2001, 2002 Bruno Lowagie
6  *
7  * The contents of this file are subject to the Mozilla Public License Version 1.1
8  * (the "License"); you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the License.
14  *
15  * The Original Code is 'iText, a free JAVA-PDF library'.
16  *
17  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19  * All Rights Reserved.
20  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
22  *
23  * Contributor(s): all the names of the contributors are added in the source code
24  * where applicable.
25  *
26  * Alternatively, the contents of this file may be used under the terms of the
27  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
28  * provisions of LGPL are applicable instead of those above. If you wish to
29  * allow use of your version of this file only under the terms of the LGPL
30  * License and not to allow others to use your version of this file under
31  * the MPL, indicate your decision by deleting the provisions above and
32  * replace them with the notice and other provisions required by the LGPL.
33  * If you do not delete the provisions above, a recipient may use your version
34  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Library General Public License as published by the Free Software Foundation;
39  * either version 2 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
44  * details.
45  *
46  * If you didn't download this code from the following link, you should check if
47  * you aren't using an obsolete version:
48  * http://www.lowagie.com/iText/
49  */

50
51 package com.lowagie.text.pdf;
52
53 import java.text.SimpleDateFormat JavaDoc;
54 import java.util.Calendar JavaDoc;
55 import java.util.GregorianCalendar JavaDoc;
56 import java.util.SimpleTimeZone JavaDoc;
57
58 /**
59  * <CODE>PdfDate</CODE> is the PDF date object.
60  * <P>
61  * PDF defines a standard date format. The PDF date format closely follows the format
62  * defined by the international standard ASN.1 (Abstract Syntax Notation One, defined
63  * in CCITT X.208 or ISO/IEC 8824). A date is a <CODE>PdfString</CODE> of the form:
64  * <P><BLOCKQUOTE>
65  * (D: YYYYMMDDHHmmSSOHH'mm')
66  * </BLOCKQUOTE><P>
67  * This object is described in the 'Portable Document Format Reference Manual version 1.3'
68  * section 7.2 (page 183-184)
69  *
70  * @see PdfString
71  * @see java.util.GregorianCalendar
72  */

73
74 public class PdfDate extends PdfString {
75
76     private static final int DATE_SPACE[] = {Calendar.YEAR, 4, 0, Calendar.MONTH, 2, -1, Calendar.DAY_OF_MONTH, 2, 0,
77         Calendar.HOUR_OF_DAY, 2, 0, Calendar.MINUTE, 2, 0, Calendar.SECOND, 2, 0};
78     
79     // constructors
80

81 /**
82  * Constructs a <CODE>PdfDate</CODE>-object.
83  *
84  * @param d the date that has to be turned into a <CODE>PdfDate</CODE>-object
85  */

86     
87     public PdfDate(Calendar JavaDoc d) {
88         super();
89         StringBuffer JavaDoc date = new StringBuffer JavaDoc("D:");
90         date.append(setLength(d.get(Calendar.YEAR), 4));
91         date.append(setLength(d.get(Calendar.MONTH) + 1, 2));
92         date.append(setLength(d.get(Calendar.DATE), 2));
93         date.append(setLength(d.get(Calendar.HOUR_OF_DAY), 2));
94         date.append(setLength(d.get(Calendar.MINUTE), 2));
95         date.append(setLength(d.get(Calendar.SECOND), 2));
96         int timezone = (d.get(Calendar.ZONE_OFFSET) + d.get(Calendar.DST_OFFSET)) / (60 * 60 * 1000);
97         if (timezone == 0) {
98             date.append('Z');
99         }
100         else if (timezone < 0) {
101             date.append('-');
102             timezone = -timezone;
103         }
104         else {
105             date.append('+');
106         }
107         if (timezone != 0) {
108             date.append(setLength(timezone, 2)).append('\'');
109             int zone = Math.abs((d.get(Calendar.ZONE_OFFSET) + d.get(Calendar.DST_OFFSET)) / (60 * 1000)) - (timezone * 60);
110             date.append(setLength(zone, 2)).append('\'');
111         }
112         value = date.toString();
113     }
114     
115 /**
116  * Constructs a <CODE>PdfDate</CODE>-object, representing the current day and time.
117  */

118     
119     public PdfDate() {
120         this(new GregorianCalendar JavaDoc());
121     }
122     
123 /**
124  * Adds a number of leading zeros to a given <CODE>String</CODE> in order to get a <CODE>String</CODE>
125  * of a certain length.
126  *
127  * @param i a given number
128  * @param length the length of the resulting <CODE>String</CODE>
129  * @return the resulting <CODE>String</CODE>
130  */

131     
132     private String JavaDoc setLength(int i, int length) { // 1.3-1.4 problem fixed by Finn Bock
133
StringBuffer JavaDoc tmp = new StringBuffer JavaDoc();
134         tmp.append(i);
135         while (tmp.length() < length) {
136             tmp.insert(0, "0");
137         }
138         tmp.setLength(length);
139         return tmp.toString();
140     }
141     
142     /**
143      * Gives the W3C format of the PdfDate.
144      * @return a formatted date
145      */

146     public String JavaDoc getW3CDate() {
147         return getW3CDate(value);
148     }
149     
150     /**
151      * Gives the W3C format of the PdfDate.
152      * @param d
153      * @return a formatted date
154      */

155     public static String JavaDoc getW3CDate(String JavaDoc d) {
156         SimpleDateFormat JavaDoc w3c = new SimpleDateFormat JavaDoc("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
157         Calendar JavaDoc c = decode(d);
158         return w3c.format(c.getTime());
159     }
160     
161     /**
162      * Converts a PDF string representing a date into a Calendar.
163      * @param s the PDF string representing a date
164      * @return a <CODE>Calendar</CODE> representing the date or <CODE>null</CODE> if the string
165      * was not a date
166      */

167     public static Calendar JavaDoc decode(String JavaDoc s) {
168         try {
169             if (s.startsWith("D:"))
170                 s = s.substring(2);
171             GregorianCalendar JavaDoc calendar;
172             int slen = s.length();
173             int idx = s.indexOf('Z');
174             if (idx >= 0) {
175                 slen = idx;
176                 calendar = new GregorianCalendar JavaDoc(new SimpleTimeZone JavaDoc(0, "ZPDF"));
177             }
178             else {
179                 int sign = 1;
180                 idx = s.indexOf('+');
181                 if (idx < 0) {
182                     idx = s.indexOf('-');
183                     if (idx >= 0)
184                         sign = -1;
185                 }
186                 if (idx < 0)
187                     calendar = new GregorianCalendar JavaDoc();
188                 else {
189                     int offset = Integer.parseInt(s.substring(idx + 1, idx + 3)) * 60;
190                     if (idx + 5 < s.length())
191                         offset += Integer.parseInt(s.substring(idx + 4, idx + 6));
192                     calendar = new GregorianCalendar JavaDoc(new SimpleTimeZone JavaDoc(offset * sign * 60000, "ZPDF"));
193                     slen = idx;
194                 }
195             }
196             calendar.clear();
197             idx = 0;
198             for (int k = 0; k < DATE_SPACE.length; k += 3) {
199                 if (idx >= slen)
200                     break;
201                 calendar.set(DATE_SPACE[k], Integer.parseInt(s.substring(idx, idx + DATE_SPACE[k + 1])) + DATE_SPACE[k + 2]);
202                 idx += DATE_SPACE[k + 1];
203             }
204             return calendar;
205         }
206         catch (Exception JavaDoc e) {
207             return null;
208         }
209     }
210 }
Popular Tags