GAUTAM
asked on
Comparing dates in java
Hi Experts...
I have a number of dates in format of MMMDDYYYY and i have to compare if the date falls under a particular range i.e if SEP202011 falls under SEP152011 and SEP242011.
Basically i read the dates to be compared from a file which will be retreived in string format which will be a list of them to compare.
I have to perfrom this comparison for a number of dates in this format if they fall under this limit.
How do i do this.
Please help...
I have a number of dates in format of MMMDDYYYY and i have to compare if the date falls under a particular range i.e if SEP202011 falls under SEP152011 and SEP242011.
Basically i read the dates to be compared from a file which will be retreived in string format which will be a list of them to compare.
I have to perfrom this comparison for a number of dates in this format if they fall under this limit.
How do i do this.
Please help...
This is the test:
Output:
SimpleDateFormat sdf1 = new SimpleDateFormat("MMMddyyyy");
java.util.Date dd = sdf1.parse("SEP202011", new ParsePosition(0));
java.util.Date dd1 = sdf1.parse("SEP152011", new ParsePosition(0));
java.util.Date dd2 = sdf1.parse("SEP242011", new ParsePosition(0));
if(dd1.before(dd) && dd2.after(dd)){
System.out.println(dd1 + " before " + dd +" and " + dd2 + " after " + dd);
}
Output:
Thu Sep 15 00:00:00 PDT 2011 before Tue Sep 20 00:00:00 PDT 2011 and Sat Sep 24 00:00:00 PDT 2011 after Tue Sep 20 00:00:00 PDT 2011
ASKER
@for_yan:Here i have to compare if a date falls between a range.
I think if(dd1.before(dd)){do something} is comparing if dd1 occurs before dd then i can take any action.
How do i modify this to compare within a date range.
Please help...
I think if(dd1.before(dd)){do something} is comparing if dd1 occurs before dd then i can take any action.
How do i modify this to compare within a date range.
Please help...
ASKER
@for_yan:And i have to check inclusive of the date range as well.
Please help...
Please help...
look above :
if(dd1.before(dd) && dd1.after(dd2)) - this would mean that dd1 is between dd and dd2
ASKER
@for_yan:Sorry didnt see that.
How do i check inclusive of the date range as well.
How do i check inclusive of the date range as well.
SimpleDateFormat sdf1 = new SimpleDateFormat("MMMddyyyy HH:mm:ss");
String s0 = "SEP152011";
String s2 = "SEP242011";
String s1 = "SEP202011";
java.util.Date dd0 = sdf1.parse(s0 + " 00:00:01", new ParsePosition(0));
java.util.Date dd1 = sdf1.parse(s1 + " 00:00:00", new ParsePosition(0));
java.util.Date dd2 = sdf1.parse(s2 + " 23:59:59", new ParsePosition(0));
if(dd1.getTime() > dd0.getTime() && dd1.getTime()<dd2.getTime()){
System.out.println("true");
}
SimpleDateFormat sdf1 = new SimpleDateFormat("MMMddyyyy HH:mm:ss");
SimpleDateFormat sdf2 = new SimpleDateFormat("MMMddyyyy");
String s0 = "SEP152011";
String s2 = "SEP242011";
String s1 = "SEP202011";
java.util.Date dd0 = sdf1.parse(s0 + " 00:00:01", new ParsePosition(0));
java.util.Date dd1 = sdf2.parse(s1, new ParsePosition(0));
java.util.Date dd2 = sdf1.parse(s2 + " 23:59:59", new ParsePosition(0));
if(dd1.getTime() > dd0.getTime() && dd1.getTime()<dd2.getTime()){
System.out.println("true");
}
Output:true
I think this would be better - this is truly inclusive:
SimpleDateFormat sdf1 = new SimpleDateFormat("MMMddyyyy HH:mm:ss");
String s0 = "SEP152011";
String s2 = "SEP242011";
String s1 = "SEP152011";
java.util.Date dd0 = sdf1.parse(s0 + " 00:00:00", new ParsePosition(0));
java.util.Date dd1 = sdf1.parse(s1 + " 12:00:00", new ParsePosition(0));
java.util.Date dd2 = sdf1.parse(s2 + " 23:59:59", new ParsePosition(0));
if(dd1.getTime() >= dd0.getTime() && dd1.getTime() <= dd2.getTime()){
System.out.println("true");
}
Output
true
ASKER
@for_yan:Thanks a lot.
Will try this and get back to you if i face any issues.
Will try this and get back to you if i face any issues.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SimpleDateFormat sdf = new SimpleDateFormat("MMMddyyy
java.util.Date dd = sdf.parse("SEP202011", new ParsePosition(0));
java.util.Date dd1 = sdf.parse("SEP152011", new ParsePosition(0));
if(dd1.before(dd)){do something}