计算时针分针秒针夹角的方法

将当前时间转换为秒(hour*60*60+minute*60+second)再除以表盘总秒数(12*60*60)再乘以360度就等于当前时针的度数。

分针,秒针同理。

再将时针度数分针度数做减法取绝对值,即可得到夹角,注:以12点方向为0度,取逆时针方向。

首先准备分数工具类

public class Fenshi {
	int fz;
	int fm;

	public Fenshi(int fz, int fm) {
		int g = gcd(fz, fm);
		if (fz % fm == 0) {
			this.fz = fz / fm;
			this.fm = 1;
		} else {
			this.fz = fz / g;
			this.fm = fm / g;
		}
	}

	public Fenshi add(Fenshi fs) {
		int newfz = fz * fs.fm + fm * fs.fz;
		int newfm = fm * fs.fm;
		return new Fenshi(newfz, newfm);
	}

	public Fenshi sub(Fenshi fs) {
		int newfz = fz * fs.fm - fm * fs.fz;
		int newfm = fm * fs.fm;
		return new Fenshi(newfz, newfm);
	}

	public Fenshi subAbs(Fenshi fs) {
		int newFz = Math.abs(fz * fs.fm - fm * fs.fz);
		int newFm = Math.abs(fm * fs.fm);
		return new Fenshi(newFz, newFm);
	}

	public int gcd(int a, int b) { // 求最大公约数
		// 辗转相除法
		int m = Math.max(Math.abs(a), Math.abs(b));
		int n = Math.min(Math.abs(a), Math.abs(b));
		int r;
		while (n != 0) {
			r = m % n;
			m = n;
			n = r;
		}
		return m;
	}

	public String toString() { // 定义输出格式
		if (fz % fm == 0) {
			return fz / fm + "";
		} else {
			return fz + "/" + fm;
		}
	}

	public static void main(String[] args) {
		System.out.println(new Fenshi(3, 6).add(new Fenshi(4, 8)));
	}
}

在提供夹角工具类

public class HourMinuteAngle {
	public static void main(String[] args) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);

		for (int i = 0; i < 12 * 60 * 60; i++) {
			calendar.add(Calendar.SECOND, 1);
			int hour = calendar.get(Calendar.HOUR);
			int minute = calendar.get(Calendar.MINUTE);
			int second = calendar.get(Calendar.SECOND);

			Fenshi angleHour = angleHour(hour, minute, second);
			Fenshi angleMinute = angleMinute(minute, second);
			// double angleSecond = angleSecond(second);

			Fenshi angleHourMinute = angleHand(angleHour, angleMinute);
			if (angleHourMinute.toString().equals(Integer.toString(180))) {
				System.out.println("angleHourMinute->" + angleHourMinute + "->" + hour + ":" + minute + ":" + second);
			} else if (angleHourMinute.toString().equals(Integer.toString(90))) {
				System.out.println("angleHourMinute->" + angleHourMinute + "->" + hour + ":" + minute + ":" + second);
			} else if (angleHourMinute.toString().equals(Integer.toString(270))) {
				System.out.println("angleHourMinute->" + angleHourMinute + "->" + hour + ":" + minute + ":" + second);
			}
		}
	}

	public static Fenshi angleSecond(int second) {
		int fenzi = second % 60 * 360;
		int fenmu = 60;
		return new Fenshi(fenzi, fenmu);
	}

	public static Fenshi angleMinute(int minute, int second) {
		int fenzi = (minute * 60 + second) % (60 * 60) * 360;
		int fenmu = 60 * 60;

		return new Fenshi(fenzi, fenmu);
	}

	public static Fenshi angleHour(int hour, int minute, int second) {
		int fenzi = (hour * 60 * 60 + minute * 60 + second) % (12 * 60 * 60) * 360;
		int fenmu = 12 * 60 * 60;

		return new Fenshi(fenzi, fenmu);
	}

	public static Fenshi angleHand(Fenshi angle1, Fenshi angle2) {
		return angle1.subAbs(angle2);
	}
}

 

你可能感兴趣的:(java学习)