Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.pojava.datetime;
  
  
  /*
   Copyright 2008-10 John Pile
  
   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.
  */

Duration is a fixed measurement of time.

Author(s):
John Pile
 
 public class Duration implements Comparable<Duration>, Serializable {

    
Compulsory serial ID.
 
     private static final long serialVersionUID = 1L;

    
A MILLISECOND = one thousandth of a second
 
     public static final long MILLISECOND = 1L;

    
A SECOND = One second is the time that elapses during 9,192,631,770 cycles of the radiation produced by the transition between two levels of the cesium 133 atom... rounded to some margin of error by your less accurate system clock.
 
     public static final long SECOND = 1000L * ;

    
A MINUTE = 60 seconds
 
     public static final long MINUTE = 60L * ;

    
An HOUR = 60 minutes
 
     public static final long HOUR = 60L * ;

    
A DAY = 24 hours (for a variable day, see CalendarUnit)
 
     public static final long DAY = 24L * ;

    
A WEEK = a fixed set of seven 24-hour days
 
     public static final long WEEK = 7L * ;

    
1 ms = 1 million nanoseconds
 
     private static final int NANOS_PER_MILLI = 1000000;

    
Non-leap Milliseconds since an epoch
 
     protected long millis = 0;

    
Nanoseconds used by high-resolution time stamps
 
     protected int nanos = 0;

    
Constructor for a duration of zero.
 
     public Duration() {
         // Default is zero duration
     }

    
Constructor parsing from a string.

Parameters:
duration String representation of a duration.
 
     public Duration(String duration) {
         char[] chars = duration.toCharArray();
         double accum = 0.0;
         double tot = 0.0;
         double dec = 1.0;
         int sign = 1;
         // Weeks, days, hours, minutes, seconds, nanoseconds [wdhmsn]
         for (char c : chars) {
            if (c == '.') {
                dec /= 10;
            } else if (c == '-') {
                sign = -1;
            } else if (c >= '0' && c <= '9') {
                if (Math.abs(dec) > 0.5) {
                    accum = accum * 10 + sign * dec * (c - '0');
                } else {
                    accum += sign * dec * (c - '0');
                    dec /= 10;
                }
            } else if (c == 'w' || c == 'W') {
                if (accum != 0) {
                    tot += . * accum;
                    accum = 0;
                    dec = 1;
                }
            } else if (c == 'd' || c == 'D') {
                if (accum != 0) {
                    tot += . * accum;
                    accum = 0;
                    dec = 1;
                }
            } else if (c == 'h' || c == 'H') {
                if (accum != 0) {
                    tot += . * accum;
                    accum = 0;
                    dec = 1;
                }
            } else if (c == 'm' || c == 'M' || c == '\'') {
                if (accum != 0) {
                    tot += . * accum;
                    accum = 0;
                    dec = 1;
                }
            } else if (c == 's' || c == 'S' || c == '\"') {
                if (accum != 0) {
                    tot += . * accum;
                    accum = 0;
                    dec = 1;
                }
            } else if (c == 'n' || c == 'N') {
                if (accum != 0) {
                    tot += accum / .;
                    accum = 0;
                    dec = 1;
                }
            }
        }
        // tot is in whole and fractional milliseconds
        if (tot > 0) {
            tot += 0.0000001;
        } else {
            tot -= 0.0000001;
        }
        this. = (longtot;
        tot /= 1000;
        tot -= (longtot;
        tot *= 1000;
        this. = (int) (tot * .);
    }

    
Duration specified in milliseconds.

Parameters:
millis
    public Duration(long millis) {
        this. = millis;
        int calcNanos = (int) (millis % ) * ;
        if (calcNanos < 0)
            this. = 1000000000 + calcNanos;
        else
            this. = calcNanos;
    }

    
Seconds + nanos pair will always be adjusted so that nanos is positive. It's a strange arrangement, but useful when representing time as an offset of Epoch, where a negative value usually represents a positive year.

Parameters:
seconds
nanos
    public Duration(long secondsint nanos) {
        while (nanos > 999999999) {
            seconds++;
            nanos -= 1000000000;
        }
        while (nanos < 0) {
            seconds--;
            nanos += 1000000000;
        }
        this. = seconds *  + nanos / ;
        this. = nanos;
        if (this. < 0 && this. > 0)
            this. -= 1000;
    }

    
Add a duration, producing a new duration.

Parameters:
dur
Returns:
A newly calculated Duration.
    public Duration add(Duration dur) {
        return new Duration(this.getSeconds() + dur.getSeconds(), this. + dur.nanos);
    }

    
Add fixed number of (+/-) milliseconds to a Duration, producing a new Duration.

Parameters:
milliseconds
Returns:
A newly calculated Duration.
    public Duration add(long milliseconds) {
        Duration d = new Duration(this.toMillis() + milliseconds);
        d.nanos += this. % 1000000;
        return d;
    }

    
Add seconds and nanoseconds to a Duration, producing a new Duration.

Parameters:
seconds
nanos
Returns:
A newly calculated Duration.
    public Duration add(long secondsint nanos) {
        // Adjust to safe range to prevent overflow.
        if (nanos > 999999999) {
            seconds++;
            nanos -= 1000000000;
        }
        return new Duration(this.getSeconds() + secondsthis. + nanos);
    }

    
Return relative comparison between two Durations.

Parameters:
other Duration to compare to
Returns:
-1, 0, or 1 of left compared to right.
    public int compareTo(Duration other) {
        if (other == null) {
            throw new NullPointerException("Cannot compare Duration to null.");
        }
        if (this. == other.millis) {
            return  < other.nanos ? -1 :  == other.nanos ? 0 : 1;
        }
        return this. < other.millis ? -1 : 1;
    }

    
Two durations are equal if internal values are identical.

Parameters:
other is a Duration or derived object
Returns:
True if durations match.
    public boolean equals(Object other) {
        return other instanceof Duration && compareTo((Durationother) == 0;
    }
    @Override
    public int hashCode() {
        return (int) ((this. & 0xFFFF) ^ this.);
    }

    
Return fractional seconds in nanoseconds
Sign of value will match whole time value.

Returns:
Sub-second portion of Duration specified in nanoseconds.
    public int getNanos() {
        return ;
    }

    
Return time truncated to milliseconds

Returns:
Number of whole milliseconds in Duration.
    public long toMillis() {
        return ;
    }

    
Return duration truncated seconds.

Returns:
Number of whole seconds in Duration.
    public long getSeconds() {
        return  / 1000 - ( < 0 ? 1 : 0);
    }

    
Return a duration parsed from a string. Expected string is of regex format "(-?[0-9]*\.?[0-9]+ *['"wdhmsnWDHMSN][^-0-9.]*)+"

Parameters:
str of format similar to "1h15m12s" or "8 weeks, 3.5 days" or [5'13"]
Returns:
a new Duration object
    public static Duration parse(String str) {
        return new Duration(str);
    }

    
Helper to build out a duration string

Parameters:
ms milliseconds remaining
interval number of milliseconds per discrete chunk
label character representing chunk size (w,d,h,m,s)
sb current duration on which to append
Returns:
ms remaining
    private long extract(long mslong intervalString labelStringBuilder sb) {
        long unit = 0;
        unit = ms / interval;
        sb.append(unit);
        sb.append(label);
        ms -= unit * interval;
        return ms;
    }

    
Output duration as a string.
    public String toString() {
        StringBuilder sb = new StringBuilder();
        long ms = this.;
        int ns = this.;
        if (ms < 0 || (ms == 0 && ns < 0)) {
            sb.append("-");
            ms *= -1;
            ns *= -1;
        }
        if (ms > .) {
            ms = extract(ms."d"sb);
        }
        if (ms > .) {
            ms = extract(ms."h"sb);
        }
        if (ms > .) {
            ms = extract(ms."m"sb);
        }
        if (ms > .) {
            // Findbugs complains of "dead store" if assigned to ms here
            extract(ms."s"sb);
        }
        if (ns > 0) {
            sb.append(ns);
            sb.append("n");
        }
        return sb.toString();
    }
New to GrepCode? Check out our FAQ X