系统时间 - 用户输入时间帮助(Java)

时间:2013-09-08 04:24:34

标签: java date datetime simpledateformat

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

提前致谢!

肖恩

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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

好吧,这一行

d1 = format.parse(dNow);

没有任何意义(实际上,不会编译),因为dNowDate对象,您只需要解析String

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

更改

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

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

你的解析应该有用。