0

I am trying to calculate the time difference between the user-set-date and the current-system-date.

My code is as follows,

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 
    }

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


}

I have been searching for lots and lots of examples, but still no progress on my code.

user1803551
  • 12,965
  • 5
  • 47
  • 74
edwardjwj
  • 3
  • 2
  • 2
    Where is your code for calculating the difference? Or -- where should it be and which dates should be diffed? – Mick Mnemonic May 05 '15 at 21:43
  • 1
    Depending on the result you want, there are a number of ways to achieve this, but, you should either be using the Java 8 Time API or JodaTime to do the core work, for [example](http://stackoverflow.com/questions/12851934/how-to-find-difference-between-two-joda-time-datetimes-in-minutes/12852021#12852021), [example](http://stackoverflow.com/questions/13328912/java-getting-time-interval/13329218#13329218), [example](http://stackoverflow.com/questions/28979880/about-days-between-two-dates/28980026#28980026) – MadProgrammer May 05 '15 at 21:57
  • 1
    [example](http://stackoverflow.com/questions/29737159/how-can-i-create-a-calculation-to-get-the-age-of-a-person-from-two-dates/29738430#29738430) – MadProgrammer May 05 '15 at 21:58

1 Answers1

0
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.");
}

Possible output is: Slept for 590ms.

You can try it yourself here.

Zereges
  • 5,139
  • 1
  • 25
  • 49