Results 1 to 4 of 4

Thread: Earlier date

  1. #1
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default Earlier date

    Hi

    I have written the following programe to compare two dates and give the earlier date.

    Java Code:
    import java.util.Scanner;
    
    public class Dates {
    	public static void main(String[] args){
    		
    		//Declare object of Scanner class
    		Scanner scan = new Scanner(System.in);
    		
    		//Declare variables
    		int d1,m1,y1;
    		int d2,m2,y2;
    		
    		//Prompt user to enter the first date
    		System.out.print("Enter the first date : " ); 
    		d1 = scan.nextInt(); 
    		m1 = scan.nextInt(); 
    		y1 = scan.nextInt();
    		
    		//Prompt user to enter the second date
    		System.out.print("Enter the second date : " ); 
    		d2 = scan.nextInt(); 
    		m2 = scan.nextInt(); 
    		y2 = scan.nextInt();
    		
    		if ( (d1==d2) && (m1==m2) && (y1==y2)){
    			System.out.println("Dates are the same !");
    		}else if (y1<y2){
    			System.out.println(d1 + "/" + m1 + "/" + y1 + " is earlier than " + d2 + "/" + m2 + "/" + y2);
    		}else if ((m1<m2)&&(y1==y2)){
    			System.out.println(d1 + "/" + m1 + "/" + y1 + " is earlier than " + d2 + "/" + m2 + "/" + y2);			
    		}else if ((d1<d2)&&(m1==m2)&&(y1==y2)){
    			System.out.println(d1 + "/" + m1 + "/" + y1 + " is earlier than " + d2 + "/" + m2 + "/" + y2);
    		}else{
    			System.out.println(d2 + "/" + m2 + "/" + y2 + " is earlier than " + d1 + "/" + m1 + "/" + y1);
    		}
    	}
    }
    First, How can I make the user input the date as (dd mm yyyy) instead of write the day in one line and the month in another line and the year in anther line.

    Second, Do you think my code is right and gives the earlier date or not?

    Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ŖΫ ỏ Ңόρę View Post
    First, How can I make the user input the date as (dd mm yyyy) instead of write the day in one line and the month in another line and the year in anther line.
    Look at using String's split method, in particular split(" ") if you want to split on spaces. For example "31 12 1979".split(" ") will return a three item String array consisting of the three Strings.

    Second, Do you think my code is right and gives the earlier date or not?
    The best way to answer this question is to test your code and find out. :)

    Much luck!

  3. #3
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Look at using String's split method, in particular split(" ") if you want to split on spaces. For example "31 12 1979".split(" ") will return a three item String array consisting of the three Strings.
    So, I have to use an array !


    Thank u

    best regards

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ŖΫ ỏ Ңόρę View Post
    So, I have to use an array !
    Also have a look at the SimpleDateFormat class.

    kind regards,

    Jos

Similar Threads

  1. java.util.Date vs java.sql.Date
    By Jack in forum New To Java
    Replies: 5
    Last Post: 10-28-2010, 02:59 PM
  2. julian date to full date format
    By judy318 in forum New To Java
    Replies: 7
    Last Post: 11-02-2009, 12:17 PM
  3. Compare date input to database with current date
    By hungleon88 in forum Advanced Java
    Replies: 2
    Last Post: 11-25-2008, 08:10 AM
  4. Creating a Gregorian Calendar using a Date object gives date - 1
    By prachi_goliwadekar in forum New To Java
    Replies: 1
    Last Post: 05-08-2008, 08:32 PM
  5. Difference between current date and anothe date
    By vijay balusamy in forum New To Java
    Replies: 1
    Last Post: 04-16-2008, 04:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •