001/* 002 * Zmanim Java API 003 * Copyright (C) 2004-2020 Eliyahu Hershfeld 004 * 005 * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General 006 * Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) 007 * any later version. 008 * 009 * This library is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied 010 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 011 * details. 012 * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to 013 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA, 014 * or connect to: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html 015 */ 016package com.kosherjava.zmanim.util; 017 018import java.util.TimeZone; 019 020/** 021 * A class that represents a numeric time. Times that represent a time of day are stored as {@link java.util.Date}s in 022 * this API. The time class is used to represent numeric time such as the time in hours, minutes, seconds and 023 * milliseconds of a {@link com.kosherjava.zmanim.AstronomicalCalendar#getTemporalHour() temporal hour}. 024 * 025 * @author © Eliyahu Hershfeld 2004 - 2020 026 */ 027public class Time { 028 /** milliseconds in a second. */ 029 private static final int SECOND_MILLIS = 1000; 030 031 /** milliseconds in a minute. */ 032 private static final int MINUTE_MILLIS = SECOND_MILLIS * 60; 033 034 /** milliseconds in an hour. */ 035 private static final int HOUR_MILLIS = MINUTE_MILLIS * 60; 036 037 /** 038 * @see #getHours() 039 */ 040 private int hours = 0; 041 042 /** 043 * @see #getMinutes() 044 */ 045 private int minutes = 0; 046 047 /** 048 * @see #getSeconds() 049 */ 050 private int seconds = 0; 051 052 /** 053 * @see #getMilliseconds() 054 */ 055 private int milliseconds = 0; 056 057 /** 058 * @see #isNegative() 059 * @see #setIsNegative(boolean) 060 */ 061 private boolean isNegative = false; 062 063 /** 064 * Constructor with parameters for the hours, minutes, seconds and millisecods. 065 * 066 * @param hours the hours to set 067 * @param minutes the minutes to set 068 * @param seconds the seconds to set 069 * @param milliseconds the milliseconds to set 070 */ 071 public Time(int hours, int minutes, int seconds, int milliseconds) { 072 this.hours = hours; 073 this.minutes = minutes; 074 this.seconds = seconds; 075 this.milliseconds = milliseconds; 076 } 077 078 /** 079 * Constructor with a parameter for milliseconds. This constructor casts the milliseconds to an int and 080 * calls {@link #Time(int)} 081 * @param millis the milliseconds to set the object with. 082 */ 083 public Time(double millis) { 084 this((int) millis); 085 } 086 087 /** 088 * A constructor that sets the time by milliseconds. The milliseconds are converted to hours, minutes, seconds 089 * and milliseconds. If the milliseconds are negative it will call {@link #setIsNegative(boolean)}. 090 * @param millis the milliseconds to set. 091 */ 092 public Time(int millis) { 093 int adjustedMillis = millis; 094 if (adjustedMillis < 0) { 095 this.isNegative = true; 096 adjustedMillis = Math.abs(adjustedMillis); 097 } 098 this.hours = adjustedMillis / HOUR_MILLIS; 099 adjustedMillis = adjustedMillis - this.hours * HOUR_MILLIS; 100 101 this.minutes = adjustedMillis / MINUTE_MILLIS; 102 adjustedMillis = adjustedMillis - this.minutes * MINUTE_MILLIS; 103 104 this.seconds = adjustedMillis / SECOND_MILLIS; 105 adjustedMillis = adjustedMillis - this.seconds * SECOND_MILLIS; 106 107 this.milliseconds = adjustedMillis; 108 } 109 110 /** 111 * Does the time represent a negative time 9such as using this to subtract time from another Time. 112 * @return if the time is negative. 113 */ 114 public boolean isNegative() { 115 return this.isNegative; 116 } 117 118 /** 119 * Set this to represent a negative time. 120 * @param isNegative that the Time represents negative time 121 */ 122 public void setIsNegative(boolean isNegative) { 123 this.isNegative = isNegative; 124 } 125 126 /** 127 * @return Returns the hour. 128 */ 129 public int getHours() { 130 return this.hours; 131 } 132 133 /** 134 * @param hours 135 * The hours to set. 136 */ 137 public void setHours(int hours) { 138 this.hours = hours; 139 } 140 141 /** 142 * @return Returns the minutes. 143 */ 144 public int getMinutes() { 145 return this.minutes; 146 } 147 148 /** 149 * @param minutes 150 * The minutes to set. 151 */ 152 public void setMinutes(int minutes) { 153 this.minutes = minutes; 154 } 155 156 /** 157 * @return Returns the seconds. 158 */ 159 public int getSeconds() { 160 return this.seconds; 161 } 162 163 /** 164 * @param seconds 165 * The seconds to set. 166 */ 167 public void setSeconds(int seconds) { 168 this.seconds = seconds; 169 } 170 171 /** 172 * @return Returns the milliseconds. 173 */ 174 public int getMilliseconds() { 175 return this.milliseconds; 176 } 177 178 /** 179 * @param milliseconds 180 * The milliseconds to set. 181 */ 182 public void setMilliseconds(int milliseconds) { 183 this.milliseconds = milliseconds; 184 } 185 186 /** 187 * Returns the time in milliseconds by converting hours, minutes and seconds into milliseconds. 188 * @return the time in milliseconds 189 */ 190 public double getTime() { 191 return this.hours * HOUR_MILLIS + this.minutes * MINUTE_MILLIS + this.seconds * SECOND_MILLIS 192 + this.milliseconds; 193 } 194 195 /** 196 * @see java.lang.Object#toString() 197 */ 198 public String toString() { 199 return new ZmanimFormatter(TimeZone.getTimeZone("UTC")).format(this); 200 } 201}