我正在製作一個小型鬧鐘應用程序來刷新Java。該應用程序的目的是讓用戶設置時鐘時間和鬧鐘時間,然後當系統時間等於鬧鐘時間時鬧鐘應該「關閉」。如何保持更新日期/日曆對象?
時鐘類包含日曆和日期對象。
時鐘是從課堂外的主要方法引用的。在這個主要方法中,我建立了一個簡單的命令行用戶界面,用戶可以設置時鐘和鬧鐘的時間。默認情況下,時鐘被初始化爲當前系統時間。
但是,這裏是自動初始化和用戶定義的時鐘對象的問題 - 它們不是「滴答」或更新。
Clock regularClock = new Clock(); //Defaults clock (using a Date) to the system time
while (userInput != "Quit")
{
switch(userInput)
{
...Other choices here...
case "Set Time": System.out.print("Enter hour: ");
hours = kb.nextInt();
System.out.print("\nEnter minutes: ");
minutes = kb.nextInt();
ACR.regularClock.setTime(hours, minutes);
System.out.println("Clock has been set");
case "Set Alarm": System.out.print("Enter hour: ");
hours = kb.nextInt();
System.out.print("\nEnter minutes: ");
minutes = kb.nextInt();
ACR.alarmClock.setTime(hours, minutes);
ACR.alarmClock.setAlarmOn(true);
System.out.println("Alarm has been set.");
break;
...Other choices here...
userInput = keyboard.next();
}
正如你所看到的,沒有循環或任何東西來刷新或保持regularClock滴答作響。出於某種原因,當我開始時,我認爲Date和Calendar對象一旦創建就保持運行 - 有點像秒錶。
所以現在我想知道在這個while循環中更新它們的最好方法是什麼。如果只允許默認的系統時間時鐘,那很容易 - 我可以在每次while循環的開始處創建一個新的Date對象。但是,如果他們選擇了這一點,則會覆蓋用戶創建的時鐘時間。另外,如果用戶不輸入任何輸入 - 而是讓應用程序坐在那裏 - 他/她將輸入輸入 - 它不應該仍然是刷新時間,並檢查是否regularClock = alarmClock時間?我怎樣才能做到這一點?
我意識到我現在有些亂七八糟,所以我會放棄它。我一直在這方面工作,但無法弄清楚最佳解決方案。如果您有任何問題,請告訴我!
簡短總結問題:
如何保持一個日期或日曆對象一個滴答作響的時間,即使它已被修改?
如何在等待用戶輸入的同時不斷更新這些對象?
順便說一句,我建議查看[Joda-Time](http://www.joda.org/joda-time/)來處理日期 - 時間對象rath呃比java.util.Date/Calendar。尋找['plusHours()'](http://www.joda.org/joda-time/apidocs/org/joda/time/DateTime.html#plusHours(int))和['plusMinutes()']( http://www.joda.org/joda-time/apidocs/org/joda/time/DateTime.html#plusMinutes(int))[DateTime]上的方法(http://www.joda.org/joda-time /apidocs/org/joda/time/DateTime.html)類。 –