001    /*
002     * Zmanim Java API
003     * Copyright (C) 2004-2005 Eliyahu Hershfeld
004     * 
005     * This program is free software; you can redistribute it and/or modify it under the terms of the
006     * GNU General Public License as published by the Free Software Foundation; either version 2 of the
007     * License, or (at your option) any later version.
008     * 
009     * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
010     * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011     * General Public License for more details.
012     * 
013     * You should have received a copy of the GNU General Public License along with this program; if
014     * not, write to the Free Software Foundation, Inc. 59 Temple Place - Suite 330, Boston, MA
015     * 02111-1307, USA or connect to: http://www.fsf.org/copyleft/gpl.html
016     */
017    package net.sourceforge.zmanim.util;
018    
019    /**
020     * A class that represents a numeric time. Times that represent a time of day
021     * are stored as {@link java.util.Date}s in this API. The time class is used to
022     * represent numeric time such as the time in hours, minutes, seconds and
023     * milliseconds of a
024     * {@link net.sourceforge.zmanim.AstronomicalCalendar#getTemporalHour() temporal hour}.
025     * 
026     * @author © Eliyahu Hershfeld 2004 - 2007
027     * @version 0.9.0
028     */
029    public class Time {
030            private static final int SECOND_MILLIS = 1000;
031    
032            private static final int MINUTE_MILLIS = SECOND_MILLIS * 60;
033    
034            private static final int HOUR_MILLIS = MINUTE_MILLIS * 60;
035    
036            private int hours = 0;
037    
038            private int minutes = 0;
039    
040            private int seconds = 0;
041    
042            private int milliseconds = 0;
043            
044            private boolean isNegative = false;
045    
046            public Time(int hours, int minutes, int seconds, int milliseconds) {
047                    this.hours = hours;
048                    this.minutes = minutes;
049                    this.seconds = seconds;
050                    this.milliseconds = milliseconds;
051            }
052    
053            public Time(double millis) {
054                    this((int) millis);
055            }
056    
057            public Time(int millis) {
058                    if(millis < 0){
059                            isNegative = true;
060                            millis = Math.abs(millis);
061                    }
062                    hours = millis / HOUR_MILLIS;
063                    millis = millis - hours * HOUR_MILLIS;
064                    
065                    minutes = millis / MINUTE_MILLIS;
066                    millis = millis - minutes * MINUTE_MILLIS;
067    
068                    seconds = millis / SECOND_MILLIS;
069                    millis = millis - seconds * SECOND_MILLIS;
070    
071                    milliseconds = millis;
072            }
073            
074            public boolean isNegative(){
075                    return isNegative;
076            }
077            public void setIsNegative(boolean isNegative){
078                    this.isNegative = isNegative;
079            }
080    
081            /**
082             * @return Returns the hour.
083             */
084            public int getHours() {
085                    return hours;
086            }
087    
088            /**
089             * @param hours
090             *            The hours to set.
091             */
092            public void setHours(int hours) {
093                    this.hours = hours;
094            }
095    
096            /**
097             * @return Returns the minutes.
098             */
099            public int getMinutes() {
100                    return minutes;
101            }
102    
103            /**
104             * @param minutes
105             *            The minutes to set.
106             */
107            public void setMinutes(int minutes) {
108                    this.minutes = minutes;
109            }
110    
111            /**
112             * @return Returns the seconds.
113             */
114            public int getSeconds() {
115                    return seconds;
116            }
117    
118            /**
119             * @param seconds
120             *            The seconds to set.
121             */
122            public void setSeconds(int seconds) {
123                    this.seconds = seconds;
124            }
125    
126            /**
127             * @return Returns the milliseconds.
128             */
129            public int getMilliseconds() {
130                    return milliseconds;
131            }
132    
133            /**
134             * @param milliseconds
135             *            The milliseconds to set.
136             */
137            public void setMilliseconds(int milliseconds) {
138                    this.milliseconds = milliseconds;
139            }
140    
141            public double getTime() {
142                    return hours * HOUR_MILLIS + minutes * MINUTE_MILLIS + seconds
143                                    * SECOND_MILLIS + milliseconds;
144            }
145    
146            public String toString() {
147                    return new ZmanimFormatter().format(this);
148            }
149    }