001 /*
002 * Zmanim Java API
003 * Copyright (C) 2004-2011 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 - 2011
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 int adjustedMillis = millis;
059 if(adjustedMillis < 0){
060 this.isNegative = true;
061 adjustedMillis = Math.abs(adjustedMillis);
062 }
063 this.hours = adjustedMillis / HOUR_MILLIS;
064 adjustedMillis = adjustedMillis - this.hours * HOUR_MILLIS;
065
066 this.minutes = adjustedMillis / MINUTE_MILLIS;
067 adjustedMillis = adjustedMillis - this.minutes * MINUTE_MILLIS;
068
069 this.seconds = adjustedMillis / SECOND_MILLIS;
070 adjustedMillis = adjustedMillis - this.seconds * SECOND_MILLIS;
071
072 this.milliseconds = adjustedMillis;
073 }
074
075 public boolean isNegative(){
076 return this.isNegative;
077 }
078 public void setIsNegative(boolean isNegative){
079 this.isNegative = isNegative;
080 }
081
082 /**
083 * @return Returns the hour.
084 */
085 public int getHours() {
086 return this.hours;
087 }
088
089 /**
090 * @param hours
091 * The hours to set.
092 */
093 public void setHours(int hours) {
094 this.hours = hours;
095 }
096
097 /**
098 * @return Returns the minutes.
099 */
100 public int getMinutes() {
101 return this.minutes;
102 }
103
104 /**
105 * @param minutes
106 * The minutes to set.
107 */
108 public void setMinutes(int minutes) {
109 this.minutes = minutes;
110 }
111
112 /**
113 * @return Returns the seconds.
114 */
115 public int getSeconds() {
116 return this.seconds;
117 }
118
119 /**
120 * @param seconds
121 * The seconds to set.
122 */
123 public void setSeconds(int seconds) {
124 this.seconds = seconds;
125 }
126
127 /**
128 * @return Returns the milliseconds.
129 */
130 public int getMilliseconds() {
131 return this.milliseconds;
132 }
133
134 /**
135 * @param milliseconds
136 * The milliseconds to set.
137 */
138 public void setMilliseconds(int milliseconds) {
139 this.milliseconds = milliseconds;
140 }
141
142 public double getTime() {
143 return this.hours * HOUR_MILLIS + this.minutes * MINUTE_MILLIS + this.seconds
144 * SECOND_MILLIS + this.milliseconds;
145 }
146
147 public String toString() {
148 return new ZmanimFormatter().format(this);
149 }
150 }