Results 1 to 4 of 4
Thread: Earlier date
- 02-06-2010, 11:21 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 80
- Rep Power
- 0
Earlier date
Hi
I have written the following programe to compare two dates and give the earlier date.
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.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); } } }
Second, Do you think my code is right and gives the earlier date or not?
Thanks
-
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.
The best way to answer this question is to test your code and find out. :)Second, Do you think my code is right and gives the earlier date or not?
Much luck!
- 02-07-2010, 06:01 PM #3
Member
- Join Date
- Jan 2010
- Posts
- 80
- Rep Power
- 0
- 02-07-2010, 06:04 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
java.util.Date vs java.sql.Date
By Jack in forum New To JavaReplies: 5Last Post: 10-28-2010, 02:59 PM -
julian date to full date format
By judy318 in forum New To JavaReplies: 7Last Post: 11-02-2009, 12:17 PM -
Compare date input to database with current date
By hungleon88 in forum Advanced JavaReplies: 2Last Post: 11-25-2008, 08:10 AM -
Creating a Gregorian Calendar using a Date object gives date - 1
By prachi_goliwadekar in forum New To JavaReplies: 1Last Post: 05-08-2008, 08:32 PM -
Difference between current date and anothe date
By vijay balusamy in forum New To JavaReplies: 1Last Post: 04-16-2008, 04:15 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks