File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes range Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "range" Watch "range" New topic


Jackie Wang
Ranch Hand

Joined: Apr 18, 2002
Posts: 315
I got 2 strings.
They will be in form for yyyymmdd
i.e. 20030802, 20030701
I want to do 2 things
1) make sure 1st one is earlier than 2nd one
2) they are not more than 30days apart.
Both will throw error.
Anyone can help?
thanks so much and really appreciated that.
Eric Fletcher
Ranch Hand

Joined: Oct 26, 2000
Posts: 188
Since you know the format(and assuming it won't vary with input)I think I'd parse the date info out and create GregorianCalendar instances.
You can then use the boolean after() method(inherited from Calendar) to see if the one date is after another.
Then use the DAY_OF_YEAR fields in the different GregorianCalendar instances to determine if the dates are more than 30 days apart, i.e. do a subtraction and see if the difference > 30.
A lot of hoops to jump through, I realize, but I don't think there is any simple way to do this sort of thing with just String comparisons. Anybody?

My theory of evolution is that Darwin was adopted. - Steven Wright
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
I like Eric's advice.
Jackie, what do you mean by "Both will throw error."? What did you try to do that caused an error?

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Jackie Wang
Ranch Hand

Joined: Apr 18, 2002
Posts: 315
thanks eric
what's GregorianCalendar?
To Dirk,
I think I will just return false or break out the method if any one of the rule violate. thansk!
Susilo Saja
Ranch Hand

Joined: May 27, 2003
Posts: 91
GregorianCalendar is a class in java.util
You can parse the String to Date using java.text.SimpleDateFormat
I agree. Here's the link:
subject: range
It's not a secret anymore!