事件之间的时间差

Time Difference between events

我正在尝试计算用户设置日期和当前系统日期之间的时差。

我的代码如下,

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JFrame;


public class Main {

    public static void main(String[] args){
        // Make the frame
        JFrame appFrame = new JFrame("Get Things Done");

        // Make a panel
        WelcomePanel buttonPanel = new WelcomePanel();

        // Set the class button to work
        appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Add the panel to the frame
        appFrame.add(buttonPanel);

        // Set the size of the frame
        appFrame.setSize(400, 400);
        // Make the frame visible
        appFrame.setVisible(true);

        appFrame.pack();

        WelcomePanel welcome = new WelcomePanel();

// --------------------------------------------------------------------------

       // Print currentTime
        Time time = new Time();
        time.printCurrentTime();
        UserEvent input = new UserEvent();

        // Test inputTime
        input.setInputTime("15/05/28 18:00:00");
        System.out.println("***Test Getters and Setters***");
        System.out.println(input.getInputTime());

    } // end main

} // end class

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Time {

    Date currentDate = new Date();
    UserEvent user = new UserEvent(); 

    // Print current time
    public void printCurrentTime(){
        DateFormat dateFormat = new SimpleDateFormat ("yy/MM/dd HH:mm:ss");
//      System.out.println("date without format: "+ currentDate.toString());
        System.out.println(dateFormat.format(currentDate));

    } // end printCurrentTime

    public long getCurrentTime(){
        return currentDate.getTime();
    }
// ----------------------------------------------------------------------------



} // end class

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class UserEvent {
    private String inputTime;
    Date user = new Date();

    // set inputTime
    public void setInputTime(String newInputTime){
    inputTime = newInputTime;

    } // end setInputTime

    // get inputTime
    public String getInputTime(){
        return inputTime;
    } // end getInputTime

    public void parseInput(){
        DateFormat dateFormat = new SimpleDateFormat ("yy/MM/dd HH:mm:ss");
        try{
            user = dateFormat.parse(inputTime);
            System.out.println(user);
        } catch (ParseException error){
            System.out.println("Are you sure you entered a date?");
        } // end try/catch 
    }

// ---------------------------------------------------------------------------  


}

我一直在搜索很多很多示例,但我的代码仍然没有任何进展。

public static void main(String[] argv) throws InterruptedException // Thread.sleep throws that
{
    Random rng = new Random();
    Date start = new Date();
    Thread.sleep(rng.nextInt(1000)); // sleep for 0-999 ms
    Date end = new Date();
    System.out.println("Slept for " + (end.getTime() - start.getTime()) + "ms.");
}

可能的输出是:Slept for 590ms.

你可以自己试试here