読者です 読者をやめる 読者になる 読者になる

Javaで時間を操る

Java プログラミング

概要

時間の情報を扱う。フィールドに時・分・秒を保持する。二つの時間の和を求めるメソッドと差を求めるメソッド、一秒ずつ進めるメソッド、一秒ずつ減らすメソッドを作る。

class MyTime_1 {
	int day;
	int hour;
	int minute;
	int second;

	static int calc_day;
	static int calc_hour;
	static int calc_minute;
	static int calc_second;

	MyTime_1() {
		this.day = 0;
		this.hour = 0;
		this.minute = 0;
		this.second = 0;
	}

	MyTime_1(int day, int hour, int minute, int second) {
		this.day = day;
		this.hour = hour;
		this.minute = minute;
		this.second = second;
	}

	static void setTime(MyTime_1 mytime1) {
		MyTime_1.calc_day = mytime1.day;
		MyTime_1.calc_hour = mytime1.hour;
		MyTime_1.calc_minute = mytime1.minute;
		MyTime_1.calc_second = mytime1.second;
	}

	static void reset() {
		calc_day = 0;
		calc_hour = 0;
		calc_minute = 0;
		calc_second = 0;
	}

	public String toString() {
		return "[" + calc_day + "]&" + "[" + calc_hour + ":" + calc_minute
				+ ":" + calc_second + "]";
	}

	static void addTime(MyTime_1 mytime1, MyTime_1 mytime2) {
		MyTime_1.calc_second += mytime1.second + mytime2.second;
		if (MyTime_1.calc_second >= 60) {
			MyTime_1.calc_second -= 60;
			MyTime_1.calc_minute++;
		}
		MyTime_1.calc_minute += mytime1.minute + mytime2.minute;
		if (MyTime_1.calc_minute >= 60) {
			MyTime_1.calc_second -= 60;
			MyTime_1.calc_hour++;
		}
		MyTime_1.calc_hour += mytime1.hour + mytime2.hour;
		if (MyTime_1.calc_hour >= 24) {
			MyTime_1.calc_hour -= 24;
			MyTime_1.calc_day++;
		}
		MyTime_1.calc_day += mytime1.day + mytime2.day;
	}

	static void subTime(MyTime_1 mytime1, MyTime_1 mytime2) {
		MyTime_1.calc_second += mytime1.second - mytime2.second;
		if (MyTime_1.calc_second < 0) {
			MyTime_1.calc_second += 60;
			MyTime_1.calc_minute--;
		}
		MyTime_1.calc_minute += mytime1.minute - mytime2.minute;
		if (MyTime_1.calc_minute < 0) {
			MyTime_1.calc_minute += 60;
			MyTime_1.calc_hour--;
		}
		MyTime_1.calc_hour += mytime1.hour - mytime2.hour;
		if (MyTime_1.calc_hour < 0) {
			MyTime_1.calc_hour += 24;
			MyTime_1.calc_day--;
		}
		MyTime_1.calc_day += mytime1.day - mytime2.day;
	}

	static void incrementSecond() {
		MyTime_1.calc_second++;
		if (MyTime_1.calc_second >= 60) {
			MyTime_1.calc_second -= 60;
			MyTime_1.calc_minute++;
			if (MyTime_1.calc_minute >= 60) {
				MyTime_1.calc_minute -= 60;
				MyTime_1.calc_hour++;
				if (MyTime_1.calc_hour >= 24) {
					MyTime_1.calc_hour -= 24;
					MyTime_1.calc_day++;

				}
			}
		}
	}

	static void decrementSecond() {
		MyTime_1.calc_second--;
		if (MyTime_1.calc_second < 0) {
			MyTime_1.calc_second += 60;
			MyTime_1.calc_minute--;
			if (MyTime_1.calc_minute < 0) {
				MyTime_1.calc_minute += 60;
				MyTime_1.calc_hour--;
				if (MyTime_1.calc_hour < 0) {
					MyTime_1.calc_hour += 24;
					MyTime_1.calc_day--;
				}
			}
		}
	}

}

public class MyTimeTest_1 {
	public static void main(String[] args) {
		MyTime_1 mt1 = new MyTime_1(0, 17, 15, 00);
		MyTime_1 mt2 = new MyTime_1(0, 18, 20, 32);
		MyTime_1 mt6 = new MyTime_1(0, 23, 59, 55);
		MyTime_1 mt7 = new MyTime_1(1, 0, 0, 05);
		System.out.println("初期値");
		System.out.println("mt1=[" + mt1.day + "]&" + "[" + mt1.hour + ":"
				+ mt1.minute + ":" + mt1.second + "]");
		System.out.println("mt2=[" + mt2.day + "]&" + "[" + mt2.hour + ":"
				+ mt2.minute + ":" + mt2.second + "]");
		System.out.println("mt6=[" + mt6.day + "]&" + "[" + mt6.hour + ":"
				+ mt6.minute + ":" + mt6.second + "]");
		System.out.println("mt7=[" + mt7.day + "]&" + "[" + mt7.hour + ":"
				+ mt7.minute + ":" + mt7.second + "]");
		System.out.println();

		MyTime_1.addTime(mt1, mt2);
		System.out.println("和:mt1+mt2=" + mt1);
		MyTime_1.reset();

		MyTime_1.subTime(mt1, mt2);
		System.out.println("差その1:mt1-mt2=" + mt1);
		MyTime_1.reset();

		MyTime_1.subTime(mt2, mt1);
		System.out.println("差その2:mt1-mt2=" + mt1);
		MyTime_1.reset();
		System.out.println();

		MyTime_1.setTime(mt6);
		System.out.println("1秒ずつ進める");
		for (int i = 0; i < 10; i++) {
			MyTime_1.incrementSecond();
			System.out.println("mt6=" + mt6);

		}

		MyTime_1.reset();
		System.out.println();

		MyTime_1.setTime(mt7);
		System.out.println("1秒ずつ戻す");
		for (int i = 0; i < 10; i++) {
			MyTime_1.decrementSecond();
			System.out.println("mt7=" + mt7);

		}
	}
}

実行結果

初期値
mt1=[0]&[17:15:0]
mt2=[0]&[18:20:32]
mt6=[0]&[23:59:55]
mt7=[1]&[0:0:5]

和:mt1+mt2=[1]&[11:35:32]
差その1:mt1-mt2=[-1]&[22:54:28]
差その2:mt1-mt2=[0]&[1:5:32]

1秒ずつ進める
mt6=[0]&[23:59:56]
mt6=[0]&[23:59:57]
mt6=[0]&[23:59:58]
mt6=[0]&[23:59:59]
mt6=[1]&[0:0:0]
mt6=[1]&[0:0:1]
mt6=[1]&[0:0:2]
mt6=[1]&[0:0:3]
mt6=[1]&[0:0:4]
mt6=[1]&[0:0:5]

1秒ずつ戻す
mt7=[1]&[0:0:4]
mt7=[1]&[0:0:3]
mt7=[1]&[0:0:2]
mt7=[1]&[0:0:1]
mt7=[1]&[0:0:0]
mt7=[0]&[23:59:59]
mt7=[0]&[23:59:58]
mt7=[0]&[23:59:57]
mt7=[0]&[23:59:56]
mt7=[0]&[23:59:55]

世界トップリーダー1000人が実践する時間術 (中経出版)

世界トップリーダー1000人が実践する時間術 (中経出版)