KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > util > CheapDateFormatter


1 /*
2
3    Derby - Class org.apache.derby.iapi.util.CheapDateFormatter
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.iapi.util;
23
24 /**
25  * This class contains static methods for formatting dates into Strings.
26  * It can be used where standard Date formatting is judged to be too
27  * expensive.
28  */

29 public class CheapDateFormatter {
30     static final long SECONDS = 1000L;
31     static final long MINUTES = SECONDS * 60L;
32     static final long HOURS = MINUTES * 60L;
33     static final long DAYS = HOURS * 24L;
34     static final long NORMAL_YEAR = DAYS * 365L;
35     static final long LEAP_YEAR = NORMAL_YEAR + DAYS;
36     static final long FOURYEARS = (NORMAL_YEAR * 3L) + LEAP_YEAR;
37     static final long END_OF_FIRST_YEAR = NORMAL_YEAR;
38     static final long END_OF_SECOND_YEAR = END_OF_FIRST_YEAR + LEAP_YEAR;
39     static final long END_OF_THIRD_YEAR = END_OF_SECOND_YEAR + NORMAL_YEAR;
40     static final int[] DAYS_IN_MONTH = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
41     static final int FEBRUARY = 1;
42
43     /**
44      * This method formats the current date into a String. The input is
45      * a long representing the number of milliseconds since Jan. 1, 1970.
46      * The output is a String in the form yyyy/mm/dd hh:mm:ss.ddd GMT.
47      *
48      * The purpose of this class is to format date strings without paying
49      * the price of instantiating ResourceBundles and Locales, which the
50      * java.util.Date class does whenever you format a date string.
51      * As a result, the output of this class is not localized, it does
52      * not take the local time zone into account, and it is possible that
53      * it will not be as accurate as the standard Date class. It is OK
54      * to use this method when, for example, formatting timestamps to
55      * write to db2j.LOG, but not for manipulating dates in language
56      * processing.
57      *
58      * @param time The current time in milliseconds since Jan. 1, 1970
59      *
60      * @return The date formatted as yyyy/mm/dd hh:mm:ss.ddd GMT.
61      */

62     public static String JavaDoc formatDate(long time) {
63         // Assume not a leap year until we know otherwise
64
boolean leapYear = false;
65
66         // How many four year periods since Jan. 1, 1970?
67
long year = ((time / FOURYEARS) * 4L);
68
69         // How much time is left over after the four-year periods?
70
long leftover = time % FOURYEARS;
71         time -= (year / 4L) * FOURYEARS;
72
73         year += 1970L;
74
75         // Does time extend past end of first year in four-year period?
76
if (leftover >= END_OF_FIRST_YEAR) {
77             year++;
78             time -= NORMAL_YEAR;
79         }
80
81         // Does time extend past end of second year in four-year period?
82
if (leftover >= END_OF_SECOND_YEAR) {
83             year++;
84             time -= NORMAL_YEAR;
85         }
86
87         // Does time extend past end of third year in four-year period?
88
if (leftover >= END_OF_THIRD_YEAR) {
89             year++;
90             time -= LEAP_YEAR;
91         }
92
93         // It's a leap year if divisible by 4, unless divisible by 100,
94
// unless divisible by 400.
95
if ((year % 4L) == 0) {
96             if ((year % 100L) == 0) {
97                 if ((year % 400L) == 0) {
98                     leapYear = true;
99                 }
100             }
101             leapYear = true;
102         }
103
104         // What day of the year is this, starting at 1?
105
long days = (time / DAYS) + 1;
106
107         // What month is this, starting at 1?
108
int month = 1;
109         for (int i = 0; i < DAYS_IN_MONTH.length; i++) {
110             int daysInMonth;
111
112             if (leapYear && (i == FEBRUARY)) {
113                 // February has 29 days in a leap year
114
daysInMonth = 29;
115             } else {
116                 // Get number of days in next month
117
daysInMonth = DAYS_IN_MONTH[i];
118             }
119
120             // Is date after the month we are looking at?
121
if (days > daysInMonth) {
122                 // Count number of months
123
month++;
124
125                 // Subtract number of days in month
126
days -= daysInMonth;
127             } else {
128                 // Don't bother to look any more - the date is within
129
// the current month.
130
break;
131             }
132         }
133
134         // How much time is left after days are accounted for?
135
time %= DAYS;
136
137         long hours = time / HOURS;
138
139         // How much time is left after hours are accounted for?
140
time %= HOURS;
141
142         long minutes = time / MINUTES;
143
144         // How much time is left after minutes are accounted for?
145
time %= MINUTES;
146
147         long seconds = time / SECONDS;
148
149         // How much time is left after seconds are accounted for?
150
time %= SECONDS;
151
152         return year + "-" +
153                 twoDigits(month) + "-" +
154                 twoDigits(days) + " " +
155                 twoDigits(hours) + ":" +
156                 twoDigits(minutes) + ":" +
157                 twoDigits(seconds) + "." +
158                 threeDigits(time) + " GMT";
159     }
160
161     private static String JavaDoc twoDigits(long val) {
162         String JavaDoc retval;
163
164         if (val < 10) {
165             retval = "0" + val;
166         } else {
167             retval = Long.toString(val);
168         }
169
170         return retval;
171     }
172
173     private static String JavaDoc threeDigits(long val) {
174         String JavaDoc retval;
175
176         if (val < 10) {
177             retval = "00" + val;
178         } else if (val < 100) {
179             retval = "0" + val;
180         } else {
181             retval = Long.toString(val);
182         }
183
184         return retval;
185     }
186 }
187
Popular Tags