001: /*
002: The contents of this file are subject to the Common Public Attribution License
003: Version 1.0 (the "License"); you may not use this file except in compliance with
004: the License. You may obtain a copy of the License at
005: http://www.projity.com/license . The License is based on the Mozilla Public
006: License Version 1.1 but Sections 14 and 15 have been added to cover use of
007: software over a computer network and provide for limited attribution for the
008: Original Developer. In addition, Exhibit A has been modified to be consistent
009: with Exhibit B.
010:
011: Software distributed under the License is distributed on an "AS IS" basis,
012: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
013: specific language governing rights and limitations under the License. The
014: Original Code is OpenProj. The Original Developer is the Initial Developer and
015: is Projity, Inc. All portions of the code written by Projity are Copyright (c)
016: 2006, 2007. All Rights Reserved. Contributors Projity, Inc.
017:
018: Alternatively, the contents of this file may be used under the terms of the
019: Projity End-User License Agreeement (the Projity License), in which case the
020: provisions of the Projity License are applicable instead of those above. If you
021: wish to allow use of your version of this file only under the terms of the
022: Projity License and not to allow others to use your version of this file under
023: the CPAL, indicate your decision by deleting the provisions above and replace
024: them with the notice and other provisions required by the Projity License. If
025: you do not delete the provisions above, a recipient may use your version of this
026: file under either the CPAL or the Projity License.
027:
028: [NOTE: The text of this license may differ slightly from the text of the notices
029: in Exhibits A and B of the license at http://www.projity.com/license. You should
030: use the latest text at http://www.projity.com/license for your modifications.
031: You may not remove this license text from the source files.]
032:
033: Attribution Information: Attribution Copyright Notice: Copyright � 2006, 2007
034: Projity, Inc. Attribution Phrase (not exceeding 10 words): Powered by OpenProj,
035: an open source solution from Projity. Attribution URL: http://www.projity.com
036: Graphic Image as provided in the Covered Code as file: openproj_logo.png with
037: alternatives listed on http://www.projity.com/logo
038:
039: Display of Attribution Information is required in Larger Works which are defined
040: in the CPAL as a work which combines Covered Code or portions thereof with code
041: not governed by the terms of the CPAL. However, in addition to the other notice
042: obligations, all copies of the Covered Code in Executable and Source Code form
043: distributed must, as a form of attribution of the original author, include on
044: each user interface screen the "OpenProj" logo visible to all users. The
045: OpenProj logo should be located horizontally aligned with the menu bar and left
046: justified on the top left of the screen adjacent to the File menu. The logo
047: must be at least 100 x 25 pixels. When users click on the "OpenProj" logo it
048: must direct them back to http://www.projity.com.
049: */
050: package com.projity.util;
051:
052: import java.text.DateFormat;
053: import java.text.SimpleDateFormat;
054: import java.util.Calendar;
055: import java.util.Date;
056: import java.util.GregorianCalendar;
057:
058: import org.apache.commons.lang.time.DateUtils;
059:
060: import com.projity.timescale.ExtendedDateFormat;
061:
062: /**
063: * Utility methods on Date
064: */
065: public class DateTime {
066:
067: public static GregorianCalendar calendarInstance() {
068: GregorianCalendar cal = new GregorianCalendar();
069: cal.setTimeZone(DateUtils.UTC_TIME_ZONE);
070: return cal;
071: }
072:
073: public static GregorianCalendar calendarInstance(int year,
074: int month, int day) {
075: GregorianCalendar cal = calendarInstance();
076: setCalendar(year, month, day, cal);
077: return cal;
078: }
079:
080: public static void setCalendar(int year, int month, int day,
081: Calendar cal) {
082: cal.set(Calendar.YEAR, year);
083: cal.set(Calendar.MONTH, month);
084: cal.set(Calendar.DAY_OF_MONTH, day);
085: cal.set(Calendar.HOUR_OF_DAY, 0);
086: cal.set(Calendar.MINUTE, 0);
087: cal.set(Calendar.SECOND, 0);
088: cal.set(Calendar.MILLISECOND, 0);
089: }
090:
091: public static SimpleDateFormat dateFormatInstance() {
092: return (SimpleDateFormat) SimpleDateFormat.getInstance();
093: // SimpleDateFormat f = new SimpleDateFormat();
094: // f.setTimeZone(DateUtils.UTC_TIME_ZONE);
095: // return f;
096: }
097:
098: public static SimpleDateFormat utcDateFormatInstance() {
099: SimpleDateFormat f = new SimpleDateFormat();
100: f.setTimeZone(DateUtils.UTC_TIME_ZONE);
101: return f;
102: }
103:
104: public static ExtendedDateFormat extendedUtcDateFormatInstance() {
105: ExtendedDateFormat f = new ExtendedDateFormat();
106: f.setTimeZone(DateUtils.UTC_TIME_ZONE);
107: return f;
108: }
109:
110: public static DateFormat utcShortDateFormatInstance() {
111: DateFormat f = DateFormat.getDateInstance(DateFormat.SHORT);
112: f.setTimeZone(DateUtils.UTC_TIME_ZONE);
113: return f;
114: }
115:
116: public static SimpleDateFormat dateFormatInstance(String pattern) {
117: SimpleDateFormat f = new SimpleDateFormat(pattern);
118: // SimpleDateFormat f = new SimpleDateFormat();
119: f.setTimeZone(DateUtils.UTC_TIME_ZONE);
120: return f;
121: }
122:
123: public static long midnightToday() {
124: GregorianCalendar cal = calendarInstance();
125: return dayFloor(cal.getTimeInMillis());
126: }
127:
128: public static long midnightTomorrow() {
129: GregorianCalendar cal = calendarInstance();
130: cal.add(Calendar.DATE, 1);
131: return dayFloor(cal.getTimeInMillis());
132: }
133:
134: public static long midnightNextDay(long d) {
135: d = dayFloor(d);
136: GregorianCalendar cal = calendarInstance();
137: cal.setTimeInMillis(d);
138: cal.add(Calendar.DATE, 1);
139: return cal.getTimeInMillis();
140: }
141:
142: private static Date zeroDateInstance = null; // flyweight for 0 date
143:
144: public static Date getZeroDate() {
145: if (zeroDateInstance == null)
146: zeroDateInstance = new Date(0);
147: return zeroDateInstance;
148: }
149:
150: public static Date NA_TIME = new Date(1); // 1 ms after time 0
151:
152: private static Calendar maxCalendarInstance = null; // flyweight for maximum allowed date
153:
154: public static Calendar getMaxCalendar() {
155: if (maxCalendarInstance == null)
156: maxCalendarInstance = calendarInstance(2050,
157: Calendar.JANUARY, 0);
158: return maxCalendarInstance;
159: }
160:
161: private static Calendar zeroCalendarInstance = null; // flyweight for zeroimum allowed date
162:
163: public static Calendar getZeroCalendar() {
164: if (zeroCalendarInstance == null) {
165: zeroCalendarInstance = DateTime.calendarInstance();
166: zeroCalendarInstance.setTimeInMillis(0);
167: }
168: return zeroCalendarInstance;
169: }
170:
171: private static Date maxDateInstance = null; // flyweight for maximum allowed date
172:
173: public static Date getMaxDate() {
174: if (maxDateInstance == null)
175: maxDateInstance = getMaxCalendar().getTime();
176: return maxDateInstance;
177: }
178:
179: public static Date max(Date date1, Date date2) {
180: return date1.after(date2) ? date1 : date2;
181: }
182:
183: public static Date min(Date date1, Date date2) {
184: return date1.before(date2) ? date1 : date2;
185: }
186:
187: public static long closestDate(double value) {
188: return closestDate(Math.round(value));
189: }
190:
191: public static long closestDate(long date) {
192: Calendar cal = DateTime.calendarInstance();
193: cal.setTimeInMillis(date);
194: // cal.set(Calendar.SECOND,0); // now going to seconds
195: cal.set(Calendar.MILLISECOND, 0);
196: return cal.getTimeInMillis();
197: }
198:
199: public static long hourFloor(long date) {
200: Calendar cal = DateTime.calendarInstance();
201: cal.setTimeInMillis(date);
202: cal.set(Calendar.MINUTE, 0);
203: cal.set(Calendar.SECOND, 0);
204: cal.set(Calendar.MILLISECOND, 0);
205:
206: return cal.getTimeInMillis();
207: }
208:
209: public static long dayFloor(long date) {
210: Calendar cal = DateTime.calendarInstance();
211: cal.setTimeInMillis(date);
212: cal.set(Calendar.MINUTE, 0);
213: cal.set(Calendar.SECOND, 0);
214: cal.set(Calendar.MILLISECOND, 0);
215: cal.set(Calendar.HOUR_OF_DAY, 0);
216: return cal.getTimeInMillis();
217: }
218:
219: public static Date dayFloor(Date date) {
220: return new Date(dayFloor(date.getTime()));
221: }
222:
223: public static long minuteFloor(long date) {
224: Calendar cal = DateTime.calendarInstance();
225: cal.setTimeInMillis(date);
226: cal.set(Calendar.MILLISECOND, 0);
227: cal.set(Calendar.SECOND, 0);
228:
229: return cal.getTimeInMillis();
230: }
231:
232: public static long nextDay(long day) {
233: GregorianCalendar d = DateTime.calendarInstance();
234: d.setTimeInMillis(day);
235: d.add(GregorianCalendar.DAY_OF_MONTH, 1);
236: return d.getTimeInMillis();
237: }
238:
239: public static long hour24() {
240: GregorianCalendar cal = DateTime.calendarInstance();
241: cal.setTimeInMillis(0);
242: cal.set(GregorianCalendar.HOUR_OF_DAY, 24);
243: return cal.getTimeInMillis();
244: }
245:
246: public static long gmt(Date date) {
247: if (date == null)
248: return 0;
249: return date.getTime() - 60000L * date.getTimezoneOffset();
250: }
251:
252: public static Date fromGmt(Date date) {
253: if (date == null)
254: return null;
255: return new Date(date.getTime() + 60000L
256: * date.getTimezoneOffset());
257: }
258:
259: public static long fromGmt(long d) {
260: if (d == 0)
261: return 0;
262: Date date = new Date(d);
263: return new Date(date.getTime() + 60000L
264: * date.getTimezoneOffset()).getTime();
265:
266: }
267:
268: public static Date gmtDate(Date date) {
269: return new Date(gmt(date));
270: }
271:
272: /**
273: * Get an integer for the date in form YYYYMMDD where the months go from 1 to 12 (unlike calendar where they go from 0 to 11)
274: * @param date
275: * @return
276: */
277: public static int toId(long date) {
278: GregorianCalendar cal = DateTime.calendarInstance();
279: cal.setTimeInMillis(date);
280: return cal.get(Calendar.YEAR) * 10000
281: + (1 + cal.get(Calendar.MONTH)) * 100
282: + cal.get(Calendar.DAY_OF_MONTH);
283: }
284:
285: /**
286: * Get an integer for the date in form YYYYMMDD where the months go from 1 to 12 (unlike calendar where they go from 0 to 11)
287: * @param date
288: * @return
289: */
290: public static long fromId(int id) {
291: GregorianCalendar cal = DateTime.calendarInstance(id / 10000,
292: (id / 100) % 100 - 1, id % 100);
293: return cal.getTimeInMillis();
294:
295: }
296:
297: /**
298: * Get an integer for the date in form YYYYMMDD where the months go from 1 to 12 (unlike calendar where they go from 0 to 11)
299: * @param date
300: * @return
301: */
302: public static int currentToYYMM() {
303: GregorianCalendar cal = DateTime.calendarInstance();
304: return (cal.get(Calendar.YEAR) % 100) * 100
305: + (1 + cal.get(Calendar.MONTH));
306:
307: }
308:
309: private static final long ONE_HOUR = 60 * 60 * 1000L;
310:
311: public static long daysBetween(Date d1, Date d2) {
312: return ((d2.getTime() - d1.getTime() + ONE_HOUR) / (ONE_HOUR * 24));
313: }
314: }
|