Question:

Help java code main() method of java > enter values :| ?

by  |  earlier

0 LIKES UnLike



how to enter parameter (date1 or date2) in mani() method of java.

my code compilies but i didnt allow me enter value of dates to compare

the

main class Date work according my expectation and test Date is to compare dates

cade is:

public class TestDate

{

// instance variables - replace the example below with your own

private Date date1;

private Date date2;

/**

* Constructor for objects of class TestDate

*/

public TestDate(Date date1, Date date2)

{

// initialise instance variables

this. date1 = date1;

this.date2 = date2;

}

public static void main(String[] args)

{

Date date1 = new Date();

date1.setDate(date1.getMonth(), date1.getDay(), date1.getYear());

Date date2 = new Date();

date2.setDate(date2.getMonth(), date2.getDay(), date2.getYear());

date1.isEqual(date2);

if (date1 == date2)

{

System.out.println(date1 + "is equals to" + date2);

}

else

{

//do nothing

}

boolean early = date1.isEarlier(date2);

if(early)

{

System.out.println(date1 + "greater than" + date2);

}

else

{

// do nothing

}

boolean later = date1.isLater(date2);

{

if(later)

{

System.out.println(date1 + "later than" + date2);

}

else

{

// do nothing

}

}

}

}

 Tags:

   Report

2 ANSWERS


  1. You have several problems:

    1. Did not allow user input

    2. Did not implement Date.isEarlier() and Date.isLater()

    The way I approach the solution:

    1. Capture user input "MM/DD/YYYY" using SimpleDateFormat

    2. Use default methods Date.equals(), Date.before() and Date.after() to compare dates

    import java.text.*;

    import java.util.*;

    public class TestDate {

    // instance variables - replace the example below with your own

    private Date date1;

    private Date date2;

    /**

    * Constructor for objects of class TestDate

    */

    public TestDate(Date date1, Date date2) {

    // initialise instance variables

    this.date1 = date1;

    this.date2 = date2;

    }

    public static void main(String[] args) {

    SimpleDateFormat sd = new SimpleDateFormat("MM/dd/yyyy");

    String dateStr1, dateStr2;

    System.out.print("Enter Date #1: ");

    dateStr1 = new Scanner(System.in).next();

    System.out.print("Enter Date #2: ");

    dateStr2 = new Scanner(System.in).next();

    System.out.println(dateStr1 + " " + dateStr2);

    try {

    Date date1 = sd.parse(dateStr1);

    Date date2 = sd.parse(dateStr2);

    if (date1.equals(date2)) {

    System.out.println(date1 + "is equals to" + date2);

    } else {

    // do nothing

    }

    boolean early = date1.after(date2);

    if (early) {

    System.out.println(date1 + " greater than " + date2);

    } else {

    // do nothing

    }

    boolean later = date1.before(date2);

    {

    if (later) {

    System.out.println(date1 + " earlier than " + date2);

    } else {

    // do nothing

    }

    }

    } catch (java.text.ParseException e) {

    e.printStackTrace();

    }

    }

    }


  2. it doesn't work cause you never prompt the user for input.  In your  main method you need to prompt the user for the input you want.    You will want to use the Scanner class.  

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.