Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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 com.gh.bmd.jrt.time;
 
 
Utility class for handling time in different time units.

Created by davide on 9/9/14.

 
 public class Time {

    
The number of hours in a day.
 
     public static final long HOURS_IN_DAY = 24;

    
The number of minutes in an hour.
 
     public static final long MINUTES_IN_HOUR = 60;

    
The number of minutes in a day.
 
     public static final long MINUTES_IN_DAY =  * ;

    
The number of seconds in a minute.
 
     public static final long SECONDS_IN_MINUTE = 60;

    
The number of seconds in an hour.
 
     public static final long SECONDS_IN_HOUR =  * ;

    
The number of seconds in a day.
 
     public static final long SECONDS_IN_DAY =  * ;

    
The maximum number of days expressible in number of seconds.
 
     protected static final long MAX_DAYS = . / ;

    
The maximum number of hours expressible in number of seconds.
 
     protected static final long MAX_HOURS = . / ;

    
The maximum number of minutes expressible in number of seconds.
 
     protected static final long MAX_MINUTES = . / ;

    
The time value.
 
     public final long time;

    
The time unit.
 
     public final TimeUnit unit;

    
Constructor.

Parameters:
time the time value.
unit the time unit.
 
     protected Time(final long time, @Nonnull final TimeUnit unit) {
 
         this. = time;
         this. = unit;
     }

    
Creates and returns an object representing the current system time in milliseconds.

Returns:
the time instance.
See also:
java.lang.System.currentTimeMillis()
 
    @Nonnull
    public static Time current() {
        return millis(System.currentTimeMillis());
    }

    
Creates and returns an object representing the current high precision system time in nanoseconds.

Returns:
the time instance.
See also:
java.lang.System.nanoTime()
    @Nonnull
    public static Time currentNano() {
        return nanos(System.nanoTime());
    }

    
Creates and returns an object representing the specified number of days.

Parameters:
days the number of days.
Returns:
the time instance.
    @Nonnull
    public static Time days(final long days) {
        if ((days > ) || (days < -)) {
            throw new IllegalArgumentException("time value overflow: " + days + " days");
        }
        return new Time(days * .);
    }

    
Creates and returns an object representing the specified time value in the specified time unit.

Parameters:
time the time value.
unit the time unit.
Returns:
the time instance.
Throws:
java.lang.NullPointerException if the specified time unit is null.
    @Nonnull
    @SuppressWarnings("ConstantConditions")
    public static Time fromUnit(final long time, @Nonnull TimeUnit unit) {
        if (unit == null) {
            throw new NullPointerException("the time unit cannot be null");
        }
        return new Time(timeunit);
    }

    
Creates and returns an object representing the specified number of hours.

Parameters:
hours the number of hours
Returns:
the time instance.
    @Nonnull
    public static Time hours(final long hours) {
        if ((hours > ) || (hours < -)) {
            throw new IllegalArgumentException("time value overflow: " + hours + " hours");
        }
        return new Time(hours * .);
    }

    
Creates and returns an object representing the specified number of microseconds.

Parameters:
micros the number of microseconds.
Returns:
the time instance.
    @Nonnull
    public static Time micros(final long micros) {
        return new Time(micros.);
    }

    
Creates and returns an object representing the specified number of milliseconds.

Parameters:
millis the number of milliseconds.
Returns:
the time instance.
    @Nonnull
    public static Time millis(final long millis) {
        return new Time(millis.);
    }

    
Creates and returns an object representing the specified number of minutes.

Parameters:
minutes the number of minutes.
Returns:
the time instance.
    @Nonnull
    public static Time minutes(final long minutes) {
        if ((minutes > ) || (minutes < -)) {
            throw new IllegalArgumentException("time value overflow: " + minutes + " minutes");
        }
        return new Time(minutes * .);
    }

    
Creates and returns an object representing the specified number of nanoseconds.

Parameters:
nanos the number of nanoseconds.
Returns:
the time instance.
    @Nonnull
    public static Time nanos(final long nanos) {
        return new Time(nanos.);
    }

    
Creates and returns an object representing the specified number of seconds.

Parameters:
seconds the number of seconds.
Returns:
the time instance.
    @Nonnull
    public static Time seconds(final long seconds) {
        return new Time(seconds.);
    }

    
Converts this time in days.

Returns:
the time instance.
    @Nonnull
    public Time daysTime() {
        return days(toDays());
    }
    @Override
    public int hashCode() {
        int result = (int) ( ^ ( >>> 32));
        result = 31 * result + .hashCode();
        return result;
    }
    @Override
    public boolean equals(final Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof Time)) {
            return false;
        }
        final Time that = (Timeo;
        if ( != that.unit) {
            return (to(that.unit) == that.time) && (that.to() == );
        }
        return ( == that.time);
    }
    @Override
    public String toString() {
        return  + " " + .toString();
    }

    
Converts this time in hours.

Returns:
the time instance.
    @Nonnull
    public Time hoursTime() {
        return hours(toHours());
    }

    
Checks if this time is zero.

Returns:
whether this time value is zero.
    public boolean isZero() {
        return ( == 0);
    }

    
Converts this time in microseconds.

Returns:
the time instance.
    @Nonnull
    public Time microsTime() {
        return micros(toMicros());
    }

    
Converts this time in milliseconds.

Returns:
the time instance.
    @Nonnull
    public Time millisTime() {
        return millis(toMillis());
    }

    
Converts this time in minutes.

Returns:
the time instance.
    @Nonnull
    public Time minutesTime() {
        return minutes(toMinutes());
    }

    
Converts this time in nanoseconds.

Returns:
the time instance.
    @Nonnull
    public Time nanosTime() {
        return nanos(toNanos());
    }

    
Converts this time in seconds.

Returns:
the time instance.
    @Nonnull
    public Time secondsTime() {
        return seconds(toSeconds());
    }

    
Converts this time in the specified unit.

Returns:
the time value in the specified unit.
Throws:
java.lang.NullPointerException if the specified time unit is null.
    public long to(@Nonnull final TimeUnit timeUnit) {
        return timeUnit.convert();
    }

    
Converts this time in number of days.

Returns:
the number of days.
    public long toDays() {
        return .toSeconds() / ;
    }

    
Converts this time in number of hours.

Returns:
the number of hours.
    public long toHours() {
        return .toSeconds() / ;
    }

    
Converts this time in number of microseconds.

Returns:
the number of microseconds.
    public long toMicros() {
        return .toMicros();
    }

    
Converts this time in number of milliseconds.

Returns:
the number of milliseconds.
    public long toMillis() {
        return .toMillis();
    }

    
Converts this time in number of minutes.

Returns:
the number of minutes.
    public long toMinutes() {
        return .toSeconds() / ;
    }

    
Converts this time in number of nanoseconds.

Returns:
the number of nanoseconds.
    public long toNanos() {
        return .toNanos();
    }

    
Converts this time in number of seconds.

Returns:
the number of seconds.
    public long toSeconds() {
        return .toSeconds();
    }
New to GrepCode? Check out our FAQ X