KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > caramel > util > DateUtils


1 /*
2 ** Caramel - Non-GUI Java Addons
3 ** Copyright (c) 2001, 2002, 2003 by Gerald Bauer
4 **
5 ** This program is free software.
6 **
7 ** You may redistribute it and/or modify it under the terms of the GNU
8 ** Lesser General Public License as published by the Free Software Foundation.
9 ** Version 2.1 of the license should be included with this distribution in
10 ** the file LICENSE, as well as License.html. If the license is not
11 ** included with this distribution, you may find a copy at the FSF web
12 ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
13 ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
14 **
15 ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
16 ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
17 ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
18 ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
19 ** REDISTRIBUTION OF THIS SOFTWARE.
20 **
21 */

22
23 package caramel.util;
24
25 import java.text.*;
26 import java.util.*;
27
28 public class DateUtils
29 {
30    /**
31     * RFC 1123 date format example: Mon, 06 May 1996 04:57:00 GMT - days: Mon,
32     * Tue, Wed, Thu, Fri, Sat, Sun - months: Jan, Feb, Mar, Apr, May, Jun, Jul,
33     * Aug, Sep, Oct, Nov, Dec
34     */

35    private static SimpleDateFormat _df;
36
37    public static String JavaDoc getDate()
38    {
39       Calendar cal = Calendar.getInstance();
40       cal.setTime( new Date() );
41
42       int year = cal.get( Calendar.YEAR );
43       int month = cal.get( Calendar.MONTH ) + 1;
44       int day = cal.get( Calendar.DAY_OF_MONTH );
45
46       return "" + year
47              + "-" + month
48              + "-" + day;
49    }
50
51    /**
52     * convienence method returns current timestamp
53     */

54    public static String JavaDoc getHttpDate()
55    {
56       return getHttpDate( new Date() );
57    }
58
59    public static String JavaDoc getHttpDate( long timestamp )
60    {
61       return getHttpDate( new Date( timestamp ) );
62    }
63
64    public static String JavaDoc getHttpDate( Date date )
65    {
66       return _df.format( date );
67    }
68
69    public static String JavaDoc getTime()
70    {
71       Calendar cal = Calendar.getInstance();
72       cal.setTime( new Date() );
73
74       int hours = cal.get( Calendar.HOUR_OF_DAY );
75       // use 24 hour clock
76
int minutes = cal.get( Calendar.MINUTE );
77       int seconds = cal.get( Calendar.SECOND );
78       int milli = cal.get( Calendar.MILLISECOND );
79
80       return formatTime( hours, minutes, seconds, milli );
81    }
82
83    public static String JavaDoc getTimestamp()
84    {
85       Calendar cal = Calendar.getInstance();
86       cal.setTime( new Date() );
87
88       int year = cal.get( Calendar.YEAR );
89       int month = cal.get( Calendar.MONTH ) + 1;
90       int day = cal.get( Calendar.DAY_OF_MONTH );
91       int hours = cal.get( Calendar.HOUR_OF_DAY );
92       // use 24 hour clock
93
int minutes = cal.get( Calendar.MINUTE );
94       int seconds = cal.get( Calendar.SECOND );
95       int milli = cal.get( Calendar.MILLISECOND );
96
97       return "" + year
98              + "-" + month
99              + "-" + day
100              + "_" + formatTime( hours, minutes, seconds, milli );
101    }
102
103    private static String JavaDoc formatTime( int hours, int minutes, int seconds, int milli )
104    {
105       StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
106       buf.append( "" + hours );
107
108       buf.append( "." );
109
110       if( minutes < 10 )
111          buf.append( "0" + minutes );
112       else
113          buf.append( "" + minutes );
114
115       buf.append( "." );
116
117       if( seconds < 10 )
118          buf.append( "0" + seconds );
119       else
120          buf.append( "" + seconds );
121
122       buf.append( "-" );
123
124       if( milli < 10 )
125          buf.append( "00" + milli );
126       else if( milli < 100 )
127          buf.append( "0" + milli );
128       else
129          buf.append( "" + milli );
130
131       return buf.toString();
132    }
133
134    static
135    {
136       _df = new SimpleDateFormat( "EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US );
137       _df.setTimeZone( TimeZone.getTimeZone( "GMT" ) );
138    }
139
140 }
141
Popular Tags