my dog learned polymorphism
The moose likes Java in General and the fly likes parsing string in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "parsing string in java" Watch "parsing string in java" New topic

parsing string in java

swapnil paranjape
Ranch Hand

Joined: May 15, 2005
Posts: 126
hi i am currently facing one problem

i have one string like this "23-aug-2004"

other string is like "25-aug-2005"

i need to parse these srtings and get the difference between them in no of days.

i have used parse function

DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
Date d22 = df.parse(d1);
Date d23 = df.parse(d2);

but using this i can only compre strings but cant find the difference..

for this i think i need to parse it and convert into interger and then find difference..
can you pls suggest how should i do it?
vishwanath nadimpally
Ranch Hand

Joined: Jan 25, 2005
Posts: 116
One way to do this would be to use substring() method and get the 'year', 'month' and 'day' parts of both the dates and then manually calculate the difference.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Continue on with
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780

There is no emoticon for what I am feeling!
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

What do you think is the reason for using two Ms in the format?
I agree. Here's the link:
subject: parsing string in java
It's not a secret anymore!