我有一个需要帮助的闹钟程序。我需要帮助的是从他们在程序中输入的用户时间中减去计算机系统时间。我不知道我做错了什么或修复它需要什么。
提前致谢!
肖恩
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import java.util.*;
import java.text.*;
public class AlarmClock
{
public static void main(String[] args) throws MalformedURLException
{
int retry = JOptionPane.YES_NO_OPTION;
JOptionPane.showMessageDialog(null, "Sean\nListing 4.14 P. 163\nAlarm Clock", "Information", JOptionPane.INFORMATION_MESSAGE);
while (retry == JOptionPane.YES_NO_OPTION)
{
// Current time
Date dNow = new Date( );
SimpleDateFormat ft = new SimpleDateFormat (" MM/dd/yyyy HH:mm:ss a");
String userTime;
// Asking user for the date and time
userTime = JOptionPane.showInputDialog(null, "Please enter a date, time and AM or PM:\n Example: 09/12/2013 11:00:00 PM");
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a ");
//String dateStart = "01/14/2012 09:29:58";
//String dateStop = "01/14/2012 10:31:48";
// Computing the computer time and user time to see when the alarm will end
Date d1 = null;
Date d2 = null;
try
{
d1 = format.parse(dNow);
d2 = format.parse(userTime);
// In milliseconds
long diff = d2.getTime() - d1.getTime();
long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;
long diffDays = diff / (24 * 60 * 60 * 1000);
// Reporting when the alarm will hit
JOptionPane.showMessageDialog(null,diffDays + " days, " + diffHours + " hours, " + diffMinutes + " minutes, " + diffSeconds + " seconds" );
}
catch (Exception e)
{
e.printStackTrace();
}
// Asking the user if they like to set up another alarm
retry = JOptionPane.showConfirmDialog(null, "Would you like to set another alarm?", "Question", JOptionPane.YES_NO_OPTION);
}
// Good Bye Message
final ImageIcon icon = new ImageIcon(new URL("http://3.bp.blogspot.com/-PsxZ4-C4Jss/UOSc0sDFpPI/AAAAAAAABLQ/nZSkY95cL48/s320/goodbye.jpg"));
JOptionPane.showMessageDialog(null, "Thanks for using the Alarm Clock Program!", "Good Bye", JOptionPane.INFORMATION_MESSAGE, icon);
}
}