001: /*
002: * <copyright>
003: *
004: * Copyright 1997-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:
027: package org.cougaar.logistics.plugin.inventory;
028:
029: import org.cougaar.core.mts.MessageAddress;
030:
031: import java.text.DateFormat;
032: import java.text.SimpleDateFormat;
033: import java.util.Date;
034: import java.util.Calendar;
035: import java.util.TimeZone;
036:
037: import org.cougaar.util.log.Logger;
038: import org.cougaar.core.logging.NullLoggingServiceImpl;
039: import org.cougaar.core.service.LoggingService;
040: import org.cougaar.core.component.ServiceBroker;
041:
042: /** Provides convenience methods. */
043: public class TimeUtils {
044: public static DateFormat dateTimeFormat_;
045: /** number of msec per day */
046: // 86400000 msec/day = 1000msec/sec * 60sec/min *60min/hr * 24 hr/day
047: public static final long MSEC_PER_WEEK = 7 * 86400000L;
048: public static final long MSEC_PER_DAY = 86400000L;
049: public static final long MSEC_PER_HOUR = 3600000L;
050: public static final long MSEC_PER_MIN = 60000L;
051: public static final long SEC_PER_DAY = 86400L;
052: public static final long SEC_PER_HOUR = 3600L;
053: public static final int HOUR_PER_DAY = 24;
054:
055: private transient Logger logger;
056:
057: public TimeUtils(UtilsProvider provider) {
058: if (provider == null) {
059: logger = NullLoggingServiceImpl.getLoggingService();
060: } else {
061: logger = (Logger) provider.getLoggingService(this );
062: }
063: }
064:
065: /** Add N days converts a time (long) into an int
066: * representing the number of days and then adds n_days
067: * therefore the resulting long will always be on the midnight boundary
068: **/
069: public static long addNDays(long time, int n_days) {
070: return addNDaysTime(time, n_days);
071: }
072:
073: public static long addNDaysTime(long time, int n_days) {
074: return (long) ((int) (time / MSEC_PER_DAY) + n_days)
075: * MSEC_PER_DAY;
076: }
077:
078: public static long subtractNDays(long time, int n_days) {
079: return (long) ((int) (time / MSEC_PER_DAY) - n_days)
080: * MSEC_PER_DAY;
081: }
082:
083: public static String dateString() {
084: return dateString(new Date());
085: }
086:
087: public static String dateString(long time) {
088: return dateString(new Date(time));
089: }
090:
091: public static String dateString(Date date) {
092: // dateTimeFormat_ = DateFormat.getDateTimeInstance(DateFormat.SHORT,
093: // DateFormat.SHORT);
094: dateTimeFormat_ = new SimpleDateFormat(
095: "MM/dd/yy HH:mm:ss.SSS z");
096: String sdate = dateTimeFormat_.format(date);
097: // mape '9/8/00 12:00 AM' to ' 9/8/00 12:00 AM'
098: while (sdate.length() < 17) {
099: sdate = " " + sdate;
100: }
101: return sdate;
102: }
103:
104: public static String msecDateString(long time) {
105: dateTimeFormat_ = DateFormat.getDateTimeInstance(
106: DateFormat.SHORT, DateFormat.SHORT);
107: String sdate = dateTimeFormat_.format(new Date(time));
108: // mape '9/8/00 12:00 AM' to ' 9/8/00 12:00 AM'
109: while (sdate.length() < 17) {
110: sdate = " " + sdate;
111: }
112: long msec = time - (time / 1000) * 1000;
113: return sdate + ":" + msec;
114: }
115:
116: public static int getDaysBetween(long today, long tomorrow) {
117: return (int) ((tomorrow - today) / MSEC_PER_DAY);
118: }
119:
120: public long pushToEndOfDay(long time) {
121: // logger.debug("pushToMidnight(), Before: "+TimeUtils.dateString(time));
122: return pushToEndOfDay(Calendar.getInstance(), time);
123: }
124:
125: public long pushToEndOfDay(Calendar calendar, long time) {
126: // logger.debug("TimeUtils", "pushToMidnight(), Before: "+TimeUtils.dateString(time));
127: calendar.setTime(new Date(time));
128: calendar.set(Calendar.HOUR, 11);
129: calendar.set(Calendar.MINUTE, 59);
130: calendar.set(Calendar.AM_PM, Calendar.PM);
131: calendar.set(Calendar.SECOND, 59);
132: calendar.set(Calendar.MILLISECOND, 999);
133: calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
134: // logger.debug("TimeUtils", "pushToEndOfDay(), After : "+TimeUtils.dateString(calendar.getTime().getTime()));
135: return calendar.getTime().getTime();
136: }
137: }
|