0

我有一个需要帮助的闹钟程序。我需要帮助的是从他们在程序中输入的用户时间中减去计算机系统时间。我不知道我做错了什么或修复它需要什么。

提前致谢!

肖恩

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);
}
}
4

2 回答 2

0

dNow 已经是一个 Date 对象,您不需要使用 format.parse 将其从 String 转换为 Date。

如果这没有帮助,请发布您的错误消息或不正确的输出。

于 2013-09-08T04:33:06.573 回答
0

嗯,这条线

d1 = format.parse(dNow);

没有任何意义(实际上也没有编译),因为dNow它是一个Date对象,你只需要解析一个String.

此外,您的日期格式行会创建一个不必要的空间,这会破坏您的解析器。

改变

SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a ");

SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a");

你的解析应该可以工作。

于 2013-09-08T04:37:26.167 回答