KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > ristretto > message > MessageDate


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Ristretto Mail API.
15  *
16  * The Initial Developers of the Original Code are
17  * Timo Stich and Frederik Dietz.
18  * Portions created by the Initial Developers are Copyright (C) 2004
19  * All Rights Reserved.
20  *
21  * Contributor(s):
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */

36 package org.columba.ristretto.message;
37
38 import java.util.Calendar JavaDoc;
39 import java.util.Date JavaDoc;
40 import java.util.TimeZone JavaDoc;
41
42
43 /**
44  * Formater to render a Date-object to a RFC2822 compatible date
45  *
46  *
47  * @author Timo Stich <tstich@users.sourceforge.net>
48  */

49 public class MessageDate {
50     
51     private static final String JavaDoc[] dayOfWeek = {
52         "Sun",
53         "Mon",
54         "Tue",
55         "Wed",
56         "Thu",
57         "Fri",
58         "Sat"
59     };
60     
61     private static final String JavaDoc[] month = {
62         "Jan",
63         "Feb",
64         "Mar",
65         "Apr",
66         "May",
67         "Jun",
68         "Jul",
69         "Aug",
70         "Sep",
71         "Oct",
72         "Nov",
73         "Dec"
74     };
75     
76     /**
77      * Render the Date to an RFC compliant String representation.
78      *
79      * @param date
80      * @return the RFC compliant Date String.
81      */

82     public static final String JavaDoc toString( Date JavaDoc date) {
83         return toString( date, TimeZone.getDefault() );
84     }
85     
86     /**
87      * Render the Date to an RFC compliant String representation.
88      *
89      * @param date
90      * @param tz
91      * @return the RFC compliant Date String.
92      */

93     public static final String JavaDoc toString( Date JavaDoc date, TimeZone JavaDoc tz ) {
94         Calendar JavaDoc calendar = Calendar.getInstance(tz);
95         calendar.setTime( date );
96         
97         StringBuffer JavaDoc result = new StringBuffer JavaDoc(31);
98         
99         // day of week
100
result.append( dayOfWeek[calendar.get(Calendar.DAY_OF_WEEK)-1]);
101         result.append(", ");
102         
103         // day of month
104
result.append( calendar.get(Calendar.DAY_OF_MONTH));
105         result.append(' ');
106         
107         // month
108
result.append( month[calendar.get(Calendar.MONTH)]);
109         result.append(' ');
110         
111         // year
112
result.append( calendar.get(Calendar.YEAR));
113         result.append(' ');
114         
115         // hour
116
int hour = calendar.get(Calendar.HOUR_OF_DAY);
117         if (hour < 10) result.append('0');
118         result.append( hour);
119         result.append(':');
120         
121         // minute
122
int min = calendar.get(Calendar.MINUTE);
123         if( min < 10 ) result.append( '0' );
124         result.append( min );
125         result.append(':');
126         
127         // second
128
int sec = calendar.get(Calendar.SECOND);
129         if( sec < 10 ) result.append( '0' );
130         result.append(sec);
131         result.append(' ');
132         
133         // timezone
134
int rawOffset = (calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET));
135         if( rawOffset < 0 ) {
136             int hours = (-rawOffset) / 3600000;
137             int minutes = ((-rawOffset) % 3600000) / 60000;
138             
139             result.append( "-");
140             if( hours < 10 ) {
141                 result.append('0');
142             }
143             result.append( hours);
144             
145             if( minutes < 10 ) {
146                 result.append('0');
147             }
148             result.append(minutes);
149         } else {
150             int hours = rawOffset / 3600000;
151             int minutes = (rawOffset % 3600000) / 60000;
152             
153             result.append( "+" );
154             if( hours < 10 ) {
155                 result.append('0');
156             }
157             result.append( hours);
158             
159             if( minutes < 10 ) {
160                 result.append('0');
161             }
162             result.append(minutes);
163         }
164         
165         return result.toString();
166     }
167
168 }
169
Popular Tags