Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.pojava.datetime;


Shift describes an offset in time in terms of CalendarUnit increments of various proportions. Compare this to the "Duration" class, which describes fixed units of time in a self-contained manner. A Shift, by contrast, may use a 23, 24 or 25 hour day depending on Daylight Saving Time, or a 28, 29, 30, or 31 day month. The amount of time described by a Shift is dependent on a start time and a time zone, both external to the Shift object itself.

Author(s):
John Pile
 
 public class Shift {
    
Nanoseconds per second
 
     private static final long NPS = 1000000000;
    
Seconds per Minute
 
     private static final long SPM = 60;
    
Minutes per Hour
 
     private static final long MPH = 60;
     private int year = 0;
     private int month = 0;
     private int week = 0;
     private int day = 0;
     private int hour = 0;
     private int minute = 0;
     private int second = 0;
     private long nanosec = 0;
 
     public Shift() {
         // default constructor
     }

    
Constructor parsing from a string.

Parameters:
iso8601 String representation of a duration.
 
     public Shift(String iso8601) {
         char[] chars = iso8601.toCharArray();
         double accum = 0.0;
         double dec = 1.0;
         int sign = 1;
         int time = 0;
         for (char c : chars) {
             if (c == '.') {
                 dec /= 10;
             } else if (c == '-') {
                 sign = -1;
             } else if (c >= '0' && c <= '9') {
                 if (dec >= 1.0) {
                     accum = accum * 10 + sign * dec * (c - '0');
                 } else {
                     accum += sign * dec * (c - '0');
                     dec /= 10;
                 }
             } else if (c == 'T') {
                 time = 1;
             } else if (c == 'Y') {
                 if (accum != 0) {
                     shiftYears(accum);
                     accum = 0;
                     dec = 1.0;
                     sign = 1;
                 }
             } else if (c == 'W') {
                 if (accum != 0) {
                     shiftWeeks(accum);
                     accum = 0;
                     dec = 1.0;
                     sign = 1;
                 }
             } else if (c == 'D') {
                 if (accum != 0) {
                     shiftDays(accum);
                     accum = 0;
                     dec = 1.0;
                     sign = 1;
                 }
             } else if (c == 'H') {
                 if (accum != 0) {
                     shiftHours(accum);
                     accum = 0;
                     dec = 1.0;
                     sign = 1;
                 }
             } else if (c == 'M') {
                 if (accum != 0) {
                     if (time == 0) {
                         shiftMonths(accum);
                     } else {
                         shiftMinutes(accum);
                     }
                     accum = 0;
                    dec = 1.0;
                    sign = 1;
                }
            } else if (c == 'S') {
                if (accum != 0) {
                    shiftSeconds(accum);
                    accum = 0;
                    dec = 1.0;
                    sign = 1;
                }
            }
        }
    }

    
Shift by number of years

Parameters:
accum
    public void shiftYears(double accum) {
        if (accum >= 1.0 || accum <= -1.0) {
             += (longaccum;
            accum -= (longaccum;
        }
        if (accum != 0) {
            shiftMonths(accum * 12);
        }
    }

    
Shift by whole number of months

Parameters:
accum
    public void shiftMonths(double accum) {
        if (accum >= 1.0 || accum <= -1.0) {
             += (longaccum;
            accum -= (longaccum;
        }
        if (accum != 0) {
            shiftDays(accum * 30);
        }
    }

    
Shift by whole number of weeks

Parameters:
accum
    public void shiftWeeks(double accum) {
        if (accum >= 1.0 || accum <= -1.0) {
             += (longaccum;
            accum -= (longaccum;
        }
        if (accum != 0) {
            shiftDays(accum * 7);
        } else {
            settleContents();
        }
    }

    

Parameters:
accum
    public void shiftDays(double accum) {
        if (accum >= 1.0 || accum <= -1.0) {
             += (longaccum;
            accum -= (longaccum;
        }
        if (accum != 0) {
            shiftHours(accum * 24);
        } else {
            settleContents();
        }
    }
    public void shiftHours(double accum) {
        if (accum >= 1.0 || accum <= -1.0) {
             += (longaccum;
            accum -= (longaccum;
        }
        if (accum != 0) {
            shiftMinutes(accum * );
        } else {
            settleContents();
        }
    }
    public void shiftMinutes(double accum) {
        if (accum >= 1.0 || accum <= -1.0) {
             += (longaccum;
            accum -= (longaccum;
        }
        if (accum != 0) {
            shiftSeconds(accum * );
        } else {
            settleContents();
        }
    }
    public void shiftSeconds(double accum) {
        if (accum >= 1.0 || accum <= -1.0) {
             += (longaccum;
            accum -= (longaccum;
        }
        if (accum != 0) {
            accum += (accum < 0 ? -0.0000000005 : 0.0000000005);
             += (long) (accum * 1000000000);
        }
        settleContents();
    }
    private void settleContents() {
        if ( >=  ||  <= -) {
            long calcsec =  / ;
             += calcsec;
             -= calcsec * ;
        }
        if ( >=  ||  <= -) {
            long calcmin =  / ;
             += calcmin;
             -= calcmin * ;
        }
        if ( >=  ||  <= -) {
            long calchour =  / ;
             += calchour;
             -= calchour * ;
        }
        while ( < 0 &&  > 0) {
            --;
             += 7;
        }
        while ( < 0 &&  > 0) {
            --;
             += 60;
        }
        while ( < 0 &&  > 0) {
            --;
             += 60;
        }
        while ( < 0 &&  > 0) {
            --;
             += 1000000000;
        }
    }
    public String toString() {
        // See ISO 8601
        settleContents();
        StringBuilder sb = new StringBuilder();
        sb.append('P');
        if ( != 0) {
            sb.append();
            sb.append('Y');
        }
        if ( != 0) {
            sb.append();
            sb.append('M');
        }
        if ( != 0) {
            sb.append();
            sb.append('W');
        }
        if ( != 0) {
            sb.append();
            sb.append('D');
        }
        if ( != 0 ||  != 0 ||  != 0 ||  != 0) {
            sb.append('T');
            if ( != 0) {
                sb.append();
                sb.append('H');
            }
            if ( != 0) {
                sb.append();
                sb.append('M');
            }
            if ( != 0 ||  != 0) {
                if ( == 0) {
                    sb.append();
                } else {
                    sb.append((double) ( +  / 1000000000.0));
                }
                sb.append('S');
            }
        }
        return sb.toString();
    }
    public int getYear() {
        return ;
    }
    public void setYear(int year) {
        this. = year;
    }
    public int getMonth() {
        return ;
    }
    public void setMonth(int month) {
        this. = month;
    }
    public int getWeek() {
        return ;
    }
    public void setWeek(int week) {
        this. = week;
    }
    public int getDay() {
        return ;
    }
    public void setDay(int day) {
        this. = day;
    }
    public int getHour() {
        return ;
    }
    public void setHour(int hour) {
        this. = hour;
    }
    public int getMinute() {
        return ;
    }
    public void setMinute(int minute) {
        this. = minute;
        settleContents();
    }
    public int getSecond() {
        return ;
    }
    public void setSecond(int second) {
        this. = second;
        settleContents();
    }
    public int getNanosec() {
        return (int;
    }
    public void setNanosec(int nanosec) {
        this. = nanosec;
        settleContents();
    }
    public void setNanosec(long nanosec) {
        this. = nanosec;
        settleContents();
    }
New to GrepCode? Check out our FAQ X