Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Copyright 2001-2006 Stephen Colebourne
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
 package org.goda.time;
 
DateTimeUtils provide public utility methods for the datetime library.

DateTimeUtils is thread-safe although shared static variables are used.

Author(s):
Stephen Colebourne
Since:
1.0
 
 public class DateTimeUtils {

    
The singleton instance of the system millisecond provider
 
     private static final SystemMillisProvider SYSTEM_MILLIS_PROVIDER = new SystemMillisProvider();
    
    
The millisecond provider currently in use
 
     private static MillisProvider cMillisProvider = ;

    
Restrictive constructor
 
     protected DateTimeUtils() {
         super();
     }
 
     //-----------------------------------------------------------------------
     
Gets the current time in milliseconds.

By default this returns System.currentTimeMillis(). This may be changed using other methods in this class.

Returns:
the current time in milliseconds from 1970-01-01T00:00:00Z
 
     public static final long currentTimeMillis() {
         return .getMillis();
     }

    
Resets the current time to return the system time.

This method changes the behaviour of currentTimeMillis(). Whenever the current time is queried, java.lang.System.currentTimeMillis() is used.

Throws:
java.lang.SecurityException if the application does not have sufficient security rights
 
     public static final void setCurrentMillisSystem(){ // throws SecurityException {
         //checkPermission();
     }

    
Sets the current time to return a fixed millisecond time.

This method changes the behaviour of currentTimeMillis(). Whenever the current time is queried, the same millisecond time will be returned.

Parameters:
fixedMillis the fixed millisecond time to use
Throws:
java.lang.SecurityException if the application does not have sufficient security rights
 
     public static final void setCurrentMillisFixed(long fixedMillis) { //throws SecurityException {
         //checkPermission();
          = new FixedMillisProvider(fixedMillis);
     }

    
Sets the current time to return the system time plus an offset.

This method changes the behaviour of currentTimeMillis(). Whenever the current time is queried, java.lang.System.currentTimeMillis() is used and then offset by adding the millisecond value specified here.

Parameters:
offsetMillis the fixed millisecond time to use
Throws:
java.lang.SecurityException if the application does not have sufficient security rights
 
     public static final void setCurrentMillisOffset(long offsetMillis) { //throws SecurityException {
         //checkPermission();
         if (offsetMillis == 0) {
              = ;
         } else {
              = new OffsetMillisProvider(offsetMillis);
         }
    }

    
Checks whether the provider may be changed using permission 'CurrentTime.setProvider'.

Throws:
SecurityException if the provider may not be changed
//    private static void checkPermission() { //throws SecurityException {
//        SecurityManager sm = System.getSecurityManager();
//        if (sm != null) {
//            sm.checkPermission(new JodaTimePermission("CurrentTime.setProvider"));
//        }
//    }
    //-----------------------------------------------------------------------
    
Gets the millisecond instant from the specified instant object handling null.

If the instant object is null, the currentTimeMillis() will be returned. Otherwise, the millis from the object are returned.

Parameters:
instant the instant to examine, null means now
Returns:
the time in milliseconds from 1970-01-01T00:00:00Z
    public static final long getInstantMillis(ReadableInstant instant) {
        if (instant == null) {
            return DateTimeUtils.currentTimeMillis();
        }
        return instant.getMillis();
    }
    //-----------------------------------------------------------------------
    
Gets the chronology from the specified instant object handling null.

If the instant object is null, or the instant's chronology is null, org.goda.time.chrono.ISOChronology.getInstance() will be returned. Otherwise, the chronology from the object is returned.

Parameters:
instant the instant to examine, null means ISO in the default zone
Returns:
the chronology, never null
    public static final Chronology getInstantChronology(ReadableInstant instant) {
        if (instant == null) {
            return ISOChronology.getInstance();
        }
        Chronology chrono = instant.getChronology();
        if (chrono == null) {
            return ISOChronology.getInstance();
        }
        return chrono;
    }
    //-----------------------------------------------------------------------
    
Gets the chronology from the specified instant based interval handling null.

The chronology is obtained from the start if that is not null, or from the end if the start is null. The result is additionally checked, and if still null then org.goda.time.chrono.ISOChronology.getInstance() will be returned.

Parameters:
start the instant to examine and use as the primary source of the chronology
end the instant to examine and use as the secondary source of the chronology
Returns:
the chronology, never null
    public static final Chronology getIntervalChronology(ReadableInstant startReadableInstant end) {
        Chronology chrono = null;
        if (start != null) {
            chrono = start.getChronology();
        } else if (end != null) {
            chrono = end.getChronology();
        }
        if (chrono == null) {
            chrono = ISOChronology.getInstance();
        }
        return chrono;
    }
    //-----------------------------------------------------------------------
    
Gets the chronology from the specified interval object handling null.

If the interval object is null, or the interval's chronology is null, org.goda.time.chrono.ISOChronology.getInstance() will be returned. Otherwise, the chronology from the object is returned.

Parameters:
interval the interval to examine, null means ISO in the default zone
Returns:
the chronology, never null
    public static final Chronology getIntervalChronology(ReadableInterval interval) {
        if (interval == null) {
            return ISOChronology.getInstance();
        }
        Chronology chrono = interval.getChronology();
        if (chrono == null) {
            return ISOChronology.getInstance();
        }
        return chrono;
    }
    //-----------------------------------------------------------------------
    
Gets the interval handling null.

If the interval is null, an interval representing now to now in the ISOChronology will be returned. Otherwise, the interval specified is returned.

Parameters:
interval the interval to use, null means now to now
Returns:
the interval, never null
Since:
1.1
    public static final ReadableInterval getReadableInterval(ReadableInterval interval) {
        if (interval == null) {
            long now = DateTimeUtils.currentTimeMillis();
            interval = new Interval(nownow);
        }
        return interval;
    }
    //-----------------------------------------------------------------------
    
Gets the chronology handling null.

If the chronology is null, org.goda.time.chrono.ISOChronology.getInstance() will be returned. Otherwise, the chronology is returned.

Parameters:
chrono the chronology to use, null means ISO in the default zone
Returns:
the chronology, never null
    public static final Chronology getChronology(Chronology chrono) {
        if (chrono == null) {
            return ISOChronology.getInstance();
        }
        return chrono;
    }
    //-----------------------------------------------------------------------
    
Gets the zone handling null.

If the zone is null, DateTimeZone.getDefault() will be returned. Otherwise, the zone specified is returned.

Parameters:
zone the time zone to use, null means the default zone
Returns:
the time zone, never null
    public static final DateTimeZone getZone(DateTimeZone zone) {
        if (zone == null) {
            return DateTimeZone.getDefault();
        }
        return zone;
    }
    //-----------------------------------------------------------------------
    
Gets the period type handling null.

If the zone is null, PeriodType.standard() will be returned. Otherwise, the type specified is returned.

Parameters:
type the time zone to use, null means the standard type
Returns:
the type to use, never null
    public static final PeriodType getPeriodType(PeriodType type) {
        if (type == null) {
            return PeriodType.standard();
        }
        return type;
    }
    //-----------------------------------------------------------------------
    
Gets the millisecond duration from the specified duration object handling null.

If the duration object is null, zero will be returned. Otherwise, the millis from the object are returned.

Parameters:
duration the duration to examine, null means zero
Returns:
the duration in milliseconds
    public static final long getDurationMillis(ReadableDuration duration) {
        if (duration == null) {
            return 0L;
        }
        return duration.getMillis();
    }
    //-----------------------------------------------------------------------
    
Checks whether the partial is contiguous.

A partial is contiguous if one field starts where another ends.

For example LocalDate is contiguous because DayOfMonth has the same range (Month) as the unit of the next field (MonthOfYear), and MonthOfYear has the same range (Year) as the unit of the next field (Year).

Similarly, LocalTime is contiguous, as it consists of MillisOfSecond, SecondOfMinute, MinuteOfHour and HourOfDay (note how the names of each field 'join up').

However, a Year/HourOfDay partial is not contiguous because the range field Day is not equal to the next field Year. Similarly, a DayOfWeek/DayOfMonth partial is not contiguous because the range Month is not equal to the next field Day.

Parameters:
partial the partial to check
Returns:
true if the partial is contiguous
Throws:
java.lang.IllegalArgumentException if the partial is null
Since:
1.1
    public static final boolean isContiguous(ReadablePartial partial) {
        if (partial == null) {
            throw new IllegalArgumentException("Partial must not be null");
        }
        DurationFieldType lastType = null;
        for (int i = 0; i < partial.size(); i++) {
            DateTimeField loopField = partial.getField(i);
            if (i > 0) {
                if (loopField.getRangeDurationField().getType() != lastType) {
                    return false;
                }
            }
            lastType = loopField.getDurationField().getType();
        }
        return true;
    }
    //-----------------------------------------------------------------------
    
Base class defining a millisecond provider.
    abstract static class MillisProvider {
        
Gets the current time.

Returns:
the current time in millis
        abstract long getMillis();
    }

    
System millis provider.
    static class SystemMillisProvider extends MillisProvider {
        
Gets the current time.

Returns:
the current time in millis
        long getMillis() {
            return System.currentTimeMillis();
        }
    }

    
Fixed millisecond provider.
    static class FixedMillisProvider extends MillisProvider {
        
The fixed millis value.
        private final long iMillis;
        
        
Constructor.

Parameters:
offsetMillis the millis offset
        FixedMillisProvider(long fixedMillis) {
             = fixedMillis;
        }
        
        
Gets the current time.

Returns:
the current time in millis
        long getMillis() {
            return ;
        }
    }

    
Offset from system millis provider.
    static class OffsetMillisProvider extends MillisProvider {
        
The millis offset.
        private final long iMillis;
        
        
Constructor.

Parameters:
offsetMillis the millis offset
        OffsetMillisProvider(long offsetMillis) {
             = offsetMillis;
        }
        
        
Gets the current time.

Returns:
the current time in millis
        long getMillis() {
            return System.currentTimeMillis() + ;
        }
    }
New to GrepCode? Check out our FAQ X