电子时钟的敏捷实现

今天尝试虚拟机调优,想打印些时间信息,随便找了下别人实现的电子时钟,然而有的并不是我们想要的电子时钟,因此自己稍微写了下。

import java.util.Date;

public class Timer {

	static class Time {

		private int hour;

		private int minute;

		private int second;

		private int millisecond;

		public Time(int hour, int min, int sec, int milli) {
			this.hour = hour;
			this.minute = min;
			this.second = sec;
			this.millisecond = milli;

		}

		public void setHour(int hour) {
			this.hour = hour;
		}

		public void setMinute(int minute) {
			this.minute = minute;
		}

		public void setSecond(int second) {
			this.second = second;
		}

		public void setMillisecond(int millisecond) {
			this.millisecond = millisecond;
		}

		public int getHour() {
			return hour;
		}

		public int getMinute() {
			return minute;
		}

		public int getSecond() {
			return second;
		}

		public int getMillisecond() {
			return millisecond;
		}
	}

	public static void main(String atgs[]) {

		Date d = new Date();
		
		Time t = new Time(d.getHours(), d.getMinutes(), d.getSeconds(), 0);
		
		while (true) {
			try {
				Thread.sleep(1);
			} catch (Exception e) {
			}
			UpdateTime(t);
			String hStr = Integer.toString(t.getHour());
			String mStr = Integer.toString(t.getMinute());
			String sStr = Integer.toString(t.getSecond());
			String mlStr = Integer.toString(t.getMillisecond());
			if (hStr.length() < 2)
				hStr = "0" + hStr;

			if (mStr.length() < 2)
				mStr = "0" + mStr;

			if (sStr.length() < 2)
				sStr = "0" + sStr;

			String mflStr = Integer.toString(t.getMillisecond());
			for (int i = 0; i < 3 - mlStr.length(); i++)
				mflStr = "0" + mflStr;
			System.out.print("\b\b\b\b\b\b\b\b\b\b\b\b");
			System.out.print(hStr + ":" + mStr + ":" + sStr + ":" + mflStr);
		}

	}

	public static void UpdateTime(Time t) {

		int millisecond = t.getMillisecond();

		if (millisecond == 999) {
			t.setMillisecond(0);
			if (t.getSecond() == 59) {
				t.setSecond(0);
				if (t.getMinute() == 59) {
					t.setMinute(0);
					if (t.getHour() == 23) {
						t.setHour(0);
					} else {
						t.setHour(t.getHour() + 1);
					}

				} else {
					t.setMinute(t.getMinute() + 1);
				}

			} else {
				t.setSecond(t.getSecond() + 1);
			}

		} else {
			t.setMillisecond(t.getMillisecond() + 1);
		}

	}

	// int check(int number,int max) {

	// }

}



结果截图:

你可能感兴趣的:(敏捷)