001: /*--------------------------------------------------------------------------
002: * <copyright>
003: *
004: * Copyright 1999-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: * --------------------------------------------------------------------------*/
026: package org.cougaar.glm.plugins;
027:
028: import java.text.DateFormat;
029: import java.text.SimpleDateFormat;
030: import java.util.Calendar;
031: import java.util.Date;
032: import java.util.TimeZone;
033:
034: /** Provides convenience methods. */
035: public class TimeUtils {
036: public static DateFormat dateTimeFormat_;
037: /** number of msec per day */
038: // 86400000 msec/day = 1000msec/sec * 60sec/min *60min/hr * 24 hr/day
039: public static final long MSEC_PER_WEEK = 7 * 86400000L;
040: public static final long MSEC_PER_DAY = 86400000L;
041: public static final long MSEC_PER_HOUR = 3600000L;
042: public static final long MSEC_PER_MIN = 60000L;
043:
044: public static long addNDays(long time, int n_days) {
045: return addNDaysTime(time, n_days);
046: }
047:
048: public static long addNDaysTime(long time, int n_days) {
049: return (long) ((int) (time / MSEC_PER_DAY) + n_days)
050: * MSEC_PER_DAY;
051: }
052:
053: public static String dateString() {
054: return dateString(new Date());
055: }
056:
057: public static String dateString(long time) {
058: return dateString(new Date(time));
059: }
060:
061: public static String dateString(Date date) {
062: // dateTimeFormat_ = DateFormat.getDateTimeInstance(DateFormat.SHORT,
063: // DateFormat.SHORT);
064: dateTimeFormat_ = new SimpleDateFormat(
065: "MM/dd/yy HH:mm:ss.SSS z");
066: String sdate = dateTimeFormat_.format(date);
067: // mape '9/8/00 12:00 AM' to ' 9/8/00 12:00 AM'
068: while (sdate.length() < 17) {
069: sdate = " " + sdate;
070: }
071: return sdate;
072: }
073:
074: public static String msecDateString(long time) {
075: dateTimeFormat_ = DateFormat.getDateTimeInstance(
076: DateFormat.SHORT, DateFormat.SHORT);
077: String sdate = dateTimeFormat_.format(new Date(time));
078: // mape '9/8/00 12:00 AM' to ' 9/8/00 12:00 AM'
079: while (sdate.length() < 17) {
080: sdate = " " + sdate;
081: }
082: long msec = time - (time / 1000) * 1000;
083: return sdate + ":" + msec;
084: }
085:
086: public static int getDaysBetween(long today, long tomorrow) {
087: return (int) ((tomorrow - today) / MSEC_PER_DAY);
088: }
089:
090: public static long pushToEndOfDay(long time) {
091: // GLMDebug.DEBUG("TimeUtils", "pushToMidnight(), Before: "+TimeUtils.dateString(time));
092: return pushToEndOfDay(Calendar.getInstance(), time);
093: }
094:
095: public static long pushToEndOfDay(Calendar calendar, long time) {
096: // GLMDebug.DEBUG("TimeUtils", "pushToMidnight(), Before: "+TimeUtils.dateString(time));
097: calendar.setTime(new Date(time));
098: calendar.set(Calendar.HOUR, 11);
099: calendar.set(Calendar.MINUTE, 59);
100: calendar.set(Calendar.AM_PM, Calendar.PM);
101: calendar.set(Calendar.SECOND, 59);
102: calendar.set(Calendar.MILLISECOND, 999);
103: calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
104: // GLMDebug.DEBUG("TimeUtils", "pushToEndOfDay(), After : "+TimeUtils.dateString(calendar.getTime().getTime()));
105: return calendar.getTime().getTime();
106: }
107: }
|