?
Solved

how to compare formatted String with int

Posted on 2006-04-11
11
Medium Priority
?
177 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:royalcyber
  • 4
  • 4
  • 2
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16429398
You need to turn them into Date and then do the comparision
0
 

Author Comment

by:royalcyber
ID: 16429413
How can I turn them into dates
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16429481
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:royalcyber
ID: 16429599
is the date java.util.date or java.sql.date
0
 

Author Comment

by:royalcyber
ID: 16430433
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

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16430552
if (end.compareTo(date) >= 0 && start.compareTo(date) <= 0)
0
 
LVL 92

Expert Comment

by:objects
ID: 16431525

> 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)))
0
 

Author Comment

by:royalcyber
ID: 16431646
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

0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 16431663
Set a Calendar to the Date and clear all but the month and year
0
 
LVL 92

Expert Comment

by:objects
ID: 16431690
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);
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question