Link to home
Start Free TrialLog in
Avatar of royalcyber
royalcyber

asked on

how to compare formatted String with int

Hi i have two formatted String in this format "mm/dd/yyyy" for which I used the formatter.format method and the name are
events_start_date and events_end_date. I need to compare these two String with another String in this format date="20060416".
I need to create an if statement like this

if (events_start_date <=date && events_end_date>=date)Then{ }

Now how can I compare these two differnt format strings. Please help
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You need to turn them into Date and then do the comparision
Avatar of royalcyber
royalcyber

ASKER

How can I turn them into dates
DateFormat df1 = new SimpleDateFormat("MM/dd/yyyy" ):
DateFormat df2 = new SimpleDateFormat("yyyyMMdd" ):
Date d1 = df1.parse(s1); // 's' is String
Date d2 = df2.parse(s2);


You can now compare them. See java.util.Date javadoc
is the date java.util.date or java.sql.date
I saw the javadoc for java.util.Date but there were methods like after, before and equals. Would you know how an I use these methods to acheive this result


 if((start <= date) && (end >= date){{}

Thanks

if (end.compareTo(date) >= 0 && start.compareTo(date) <= 0)

> if (events_start_date <=date && events_end_date>=date)Then{ }

Date d = df.parse(date);

if (!(d.before(formatter.parse(events_start_date)) ||  d.after(formatter.parse(events_end_date)))
Thanks CEHJ

I am stuck with another question also.

I have two string month and year and I need to find out if the java.util.Date.start_event (only month and year) <= date && java.util.Date.end_event (only month and year) >= date

I concatenated the month and year Strings to one String date.

I tried using the getMonth() method and then compare the month and year seperately but the method is crossed out in JUilder and I am not able to use it

Please help

ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
use the following to get the month and year from a Date:

Calendar cal = Calendar.getInstance();
cal.setTime(date);

int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);