KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > mail > imap > protocol > INTERNALDATE


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)INTERNALDATE.java 1.14 05/08/29
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package com.sun.mail.imap.protocol;
29
30 import java.util.Date JavaDoc;
31 import java.util.TimeZone JavaDoc;
32 import java.util.Locale JavaDoc;
33 import java.text.ParseException JavaDoc;
34 import java.text.SimpleDateFormat JavaDoc;
35 import java.text.FieldPosition JavaDoc;
36
37 import javax.mail.internet.MailDateFormat JavaDoc;
38
39 import com.sun.mail.iap.*;
40
41
42 /**
43  * This class
44  *
45  * @version 1.14, 05/08/29
46  * @author John Mani
47  */

48
49 public class INTERNALDATE implements Item {
50
51     public static char [] name = {'I','N','T','E','R','N','A','L','D','A','T','E'};
52     public int msgno;
53     protected Date JavaDoc date;
54
55     /*
56      * Used to parse dates only. The parse method is thread safe
57      * so we only need to create a single object for use by all
58      * instances. We depend on the fact that the MailDateFormat
59      * class will parse dates in INTERNALDATE format as well as
60      * dates in RFC 822 format.
61      */

62     private static MailDateFormat JavaDoc mailDateFormat = new MailDateFormat JavaDoc();
63
64     /**
65      * Constructor
66      */

67     public INTERNALDATE(FetchResponse r) throws ParsingException {
68     msgno = r.getNumber();
69     r.skipSpaces();
70     String JavaDoc s = r.readString();
71     try {
72         date = mailDateFormat.parse(s);
73     } catch (ParseException JavaDoc pex) {
74         throw new ParsingException("INTERNALDATE parse error");
75     }
76     }
77
78     public Date JavaDoc getDate() {
79     return date;
80     }
81
82     // INTERNALDATE formatter
83

84     private static SimpleDateFormat JavaDoc df =
85     // Need Locale.US, the "MMM" field can produce unexpected values
86
// in non US locales !
87
new SimpleDateFormat JavaDoc("dd-MMM-yyyy HH:mm:ss ", Locale.US);
88
89     /**
90      * Format given Date object into INTERNALDATE string
91      */

92     public static String JavaDoc format(Date JavaDoc d) {
93     /*
94      * SimpleDateFormat objects aren't thread safe, so rather
95      * than create a separate such object for each request,
96      * we create one object and synchronize its use here
97      * so that only one thread is using it at a time. This
98      * trades off some potential concurrency for speed in the
99      * common case.
100      *
101      * This method is only used when formatting the date in a
102      * message that's being appended to a folder.
103      */

104     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
105     synchronized (df) {
106         df.format(d, sb, new FieldPosition JavaDoc(0));
107     }
108
109     // compute timezone offset string
110
// XXX - Yes, I know this is deprecated
111
int rawOffsetInMins = -d.getTimezoneOffset();
112     /*
113      * XXX - in JavaMail 1.4 / J2SE 1.4, possibly replace above with:
114      *
115     TimeZone tz = TimeZone.getDefault();
116     int offset = tz.getOffset(d); // get offset from GMT
117     int rawOffsetInMins = offset / 60 / 1000; // offset from GMT in mins
118      */

119     if (rawOffsetInMins < 0) {
120         sb.append('-');
121         rawOffsetInMins = (-rawOffsetInMins);
122     } else
123         sb.append('+');
124     
125     int offsetInHrs = rawOffsetInMins / 60;
126     int offsetInMins = rawOffsetInMins % 60;
127
128     sb.append(Character.forDigit((offsetInHrs/10), 10));
129     sb.append(Character.forDigit((offsetInHrs%10), 10));
130     sb.append(Character.forDigit((offsetInMins/10), 10));
131     sb.append(Character.forDigit((offsetInMins%10), 10));
132
133     return sb.toString();
134     }
135 }
136
Popular Tags