CscTime.java 5.11 KB
Newer Older
TongZuu committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
package com.csc.library.utilities;

import java.math.RoundingMode;
import java.text.DecimalFormat;

public class CscTime {
	
	private int HH;
	private int MM;
	private boolean isPositive = true;
	private int HHH, MMM;
	private boolean isPositivePositive;

	public CscTime() {
	}
	
	public CscTime(float HHMM) {
		this.separateHHMM(HHMM);
		this.HH = this.HHH;
		this.MM = this.MMM;
		this.isPositive = this.isPositivePositive;
	}

	private void separateHHMM(float HHMM) {
		if (HHMM < 0) {
			this.isPositivePositive = false;
			HHMM = -(HHMM);
		} else {
			this.isPositivePositive = true;
		}
		this.MMM = 0;
		this.HHH = (int) HHMM;
		if (this.HHH != HHMM) {
			float buff = ((HHMM * 100) - (this.HHH * 100));
			DecimalFormat f = new DecimalFormat("00");				
			this.MMM = Integer.parseInt((String) f.format(buff));
			//this.MMM = (int) buff;
			if (this.MMM > 59) {
				this.HHH += (int) (this.MMM / 60);
				this.MMM = this.MMM % 60;
			}
		}
	}

	public CscTime(int HH, int MM) {
		this.HH = HH;
		this.MM = MM;
	}

	public float DTOT(float tm) {
		// Convert Decimal unit to Time unit (12.50 -> 12.30)
		int buff;
		float buff1;
		boolean minus = false;
		if (tm != 0) {
			if (tm < 0) {
				tm = -(tm);
				minus = true;
			}
			buff = (int) tm;
			buff1 = (float) (tm - buff);
			if (buff1 != 0) {
				buff1 = (buff1 * 6) / 10;
				tm = buff + buff1;
			}
			DecimalFormat df = new DecimalFormat("00.00");
			tm = Float.valueOf(df.format((double) tm)).floatValue();
			if (minus) {
				tm = -(tm);
			}
		}
		return tm;
	}

	public float TTOD(float tm) {
		// Convert Time unit to Decimal unit (12.30 -> 12.50)
		int buff;
		float buff1;
		boolean minus = false;
		if (tm != 0) {
			if (tm < 0) {
				tm = -(tm);
				minus = true;
			}
			buff = (int) tm;
			buff1 = (float) (tm - buff);
			if (buff1 != 0) {
				buff1 = (buff1 * 100) / 60;
				tm = buff + buff1;
			}
			if (minus) {
				tm = -(tm);
			}
		}
		return tm;
	}

	public float TTOD() {
		float result = 0;
		result = (float) this.HH;
		if (this.MM > 0)
			result += ((float) this.MM) / 60;
		if (!this.isPositive) {
			result = -(result);
		}
		return result;
	}

	public float getFloat() {
		float result = 0;
		result = ((float) (this.HH));
		if (this.MM > 0)
			result += ((float) this.MM) / 100;

		if (!this.isPositive) {
			result = -(result);
		}
		return result;
	}

	public int getHour() {
		int result = 0;
		result = this.HH;
		if (!this.isPositive) {
			result = -(result);
		}
		return result;
	}

	public int getMinute() {
		int result = 0;
		result = this.MM;
		if (!this.isPositive) {
			result = -(result);
		}
		return result;
	}

	public void setHour(int HH) {
		if (HH < 0) {
			HH = -(HH);
		}
		this.HH = HH;
	}

	public void setMinute(int MM) {
		if (MM < 0) {
			MM = -(MM);
		}
		this.MM = MM;
	}
	
	public void add(double HHMM) {
		this.add(Float.parseFloat(String.valueOf(HHMM)));
	}

	public void add(float HHMM) {
		this.separateHHMM(HHMM);
		if (this.isPositive) {
			if (this.isPositivePositive) {
				this.addHour(this.HHH);
				this.addMinute(this.MMM);
			} else {
				this.subHour(this.HHH);
				this.subMinute(this.MMM);
			}
		} else {
			if (this.isPositivePositive) {
				this.subHour(this.HHH);
				this.subMinute(this.MMM);
			} else {
				this.addHour(this.HHH);
				this.addMinute(this.MMM);
			}
		}
	}
	
	public void sub(double HHMM) {
		this.sub(Float.parseFloat(String.valueOf(HHMM)));
	}

	public void sub(float HHMM) {
		this.separateHHMM(HHMM);
		if (this.isPositive) {
			if (this.isPositivePositive) {
				this.subHour(this.HHH);
				this.subMinute(this.MMM);
			} else {
				this.addHour(this.HHH);
				this.addMinute(this.MMM);
			}
		} else {
			if (this.isPositivePositive) {
				this.addHour(this.HHH);
				this.addMinute(this.MMM);
			} else {
				this.subHour(this.HHH);
				this.subMinute(this.MMM);
			}
		}
	}

	public void addMinute(int MM) {
		this.MM += MM;
		if (this.MM > 59) {
			this.HH += (int) (this.MM / 60);
			this.MM = this.MM % 60;
		}
	}

	public void addHour(int HH) {
		this.HH += HH;
	}

	public void subMinute(int MM) {
		if (this.MM < MM) {
			if (this.HH > 0) {
				this.HH -= 1;
				this.MM += 60;
			} else {
				this.reverse();
				this.MM = MM - this.MM;
				MM = 0;
			}
		}
		this.MM -= MM;
	}

	public void subHour(int HH) {
		if (this.HH < HH) {
			this.HH = HH - this.HH;
			int buff = this.MM;
			this.MM = this.MMM;
			this.MMM = buff;
			this.reverse();
		} else {
			this.HH -= HH;
		}
	}

	public void reverse() {
		if (this.isPositive) {
			this.isPositive = false;
		} else {
			this.isPositive = true;
		}
	}

	public void add(CscTime t) {
		this.add(t.getFloat());
	}

	public void sub(CscTime t) {
		this.sub(t.getFloat());
	}

	public void inc() {
		this.addMinute(1);
	}

	public void dec() {
		this.subMinute(1);
	}

	public void resetTime() {
		this.setHour(0);
		this.setMinute(0);
	}
	
	public double getDouble() {
		return Double.parseDouble(String.valueOf(this.getFloat()));
	}
}