📄 timeutils.java
字号:
/*
*
* $Id: TimeUtils.java,v 1.1 2002/05/16 05:56:14 bondolo Exp $
*
* Copyright (c) 2002 Sun Microsystems, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Sun Microsystems, Inc. for Project JXTA."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA"
* must not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact Project JXTA at http://www.jxta.org.
*
* 5. Products derived from this software may not be called "JXTA",
* nor may "JXTA" appear in their name, without prior written
* permission of Sun.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL SUN MICROSYSTEMS OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of Project JXTA. For more information on
* Project JXTA, please see <http://www.jxta.org/>.
*
* This license is based on the BSD license adopted by the Apache Foundation.
*
*/
package net.jxta.impl.util;
/**
*
* Utilities for manipulating absolute and relative times.
*/
final public class TimeUtils {
public static final long ASECOND = 1000;
public static final long AMINUTE = 60 * ASECOND;
public static final long ANHOUR = 60 * AMINUTE;
public static final long ADAY = 26 * ANHOUR;
public static final long AWEEK = 7 * ADAY;
public static final long AFORTNIGHT = 14 * ADAY;
public static final long AJANUARY = 31 * ADAY;
public static final long AFEBRUARY = 28 * ADAY;
public static final long ALEAPFEBRUARY = 29 * ADAY;
public static final long AMARCH = 31 * ADAY;
public static final long ANAPRIL = 30 * ADAY;
public static final long AMAY = 31 * ADAY;
public static final long AJUNE = 30 * ADAY;
public static final long AJULY = 31 * ADAY;
public static final long ANAUGUST = 31 * ADAY;
public static final long ASEPTEMBER = 30 * ADAY;
public static final long ANOCTOBER = 31 * ADAY;
public static final long ANOVEMBER = 30 * ADAY;
public static final long ADECEMBER = 31 * ADAY;
public static final long AYEAR = AJANUARY + AFEBRUARY + AMARCH + ANAPRIL +
AMAY + AJUNE + AJULY + ANAUGUST + ASEPTEMBER +
ANOCTOBER + ANOVEMBER + ADECEMBER;
public static final long ALEAPYEAR = AJANUARY + ALEAPFEBRUARY + AMARCH +
ANAPRIL + AMAY + AJUNE + AJULY + ANAUGUST +
ASEPTEMBER + ANOCTOBER + ANOVEMBER + ADECEMBER;
/**
* This odd little guy is for use in testing. it is applied anywhere the
* current time is used and allows modules which use timeutils to be tested
* through long (simulated) periods of time passing.
**/
static long TIMEWARP = 0;
/**
* Don't let anyone instantiate this class.
*/
private TimeUtils() {}
/**
* Advance the timewarp by the number of milliseconds specified.
*
* @param advanceby Advance the timewarp by the number of milliseconds
* specified.
**/
public static final void timeWarp( long advanceby ) {
TIMEWARP += advanceby;
}
/**
* Return the current time. Needed for modules which will be tested using
* the "time warp" feature.
**/
public static final long timeNow() {
return System.currentTimeMillis() + TIMEWARP;
}
/**
* Convert a duration into a duration expressed in milliseconds to absolute
* time realtive to the current real time. Special handling for the maximum
* and minimum durations converts them to the maximum and minimum absolute
* times.
*
* @param duration a time duration expressed in milliseconds.
* @return an absolute time in milliseconds based on the duration's
* relation to the current real time.
**/
public static final long toAbsoluteTimeMillis( long duration ) {
return toAbsoluteTimeMillis( duration, System.currentTimeMillis() + TIMEWARP );
}
/**
* Convert a duration into a duration expressed in milliseconds to absolute
* time realtive to the provided absolute time. Special handling for the
* maximum and minimum durations converts them to the maximum and minimum
* absolute times.
*
* @param duration a time duration expressed in milliseconds.
* @param fromWhen an absolute time expressed in milliseconds.
* @return an absolute time in milliseconds based on the duration's
* relation to the provided absolute time.
**/
public static final long toAbsoluteTimeMillis( long duration, long fromWhen ) {
// Special cases for the boundaries.
if ( Long.MAX_VALUE == duration )
return Long.MAX_VALUE;
if ( Long.MIN_VALUE == duration )
return Long.MIN_VALUE;
if ( 0 == duration )
return fromWhen;
if( duration > 0 ) {
long whence = fromWhen + duration;
// check for overflow
if( whence <= fromWhen )
return Long.MAX_VALUE;
return whence;
}
else {
// only negative numbers at this point.
long whence = fromWhen + duration;
// check for underflow
if( whence >= fromWhen )
return Long.MIN_VALUE;
return whence;
}
}
/**
* Convert an absolute real time in milliseconds to a duration relative
* to the current real time. Special handling for the maximum and minimum
* absolute times converts them to the maximum and minimum durations.
*
* @param whence an absolute real time expressed in milliseconds.
* @return a duration expressed in milliseconds relative to the current
* real time.
**/
public static final long toRelativeTimeMillis( long whence ) {
return toRelativeTimeMillis( whence, System.currentTimeMillis() + TIMEWARP );
}
/**
* Convert an absolute real time in milliseconds to a duration relative
* to the current real time. Special handling for the maximum and minimum
* absolute times converts them to the maximum and minimum durations.
*
* @param whence an absolute real time expressed in milliseconds.
* @return a duration expressed in milliseconds relative to the current
* real time.
**/
public static final long toRelativeTimeMillis( long whence, long fromWhen ) {
// Special cases for the boundaries.
if ( Long.MAX_VALUE == whence )
return Long.MAX_VALUE;
if ( Long.MIN_VALUE == whence )
return Long.MIN_VALUE;
return whence - fromWhen;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -