Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need example for date comparison with a certain format 1

Status
Not open for further replies.

thelordoftherings

Programmer
May 16, 2004
616
IL
Hello,

I have a list of dates, each one contained in a String at this format: "dd/mm/yy". Today's date for example is represented like this: 29/05/05.
I would like to go over the list and remove all the dates that are older than a week coparing to today's date.
Can someone please show me an example of how to do it?

10X, Roy
 
Use the SimpleDateFormat class to convert your date string to a Date object.

The just test if the Date object represents a point in time older than a week.

See this thread : for how to use SimpleDateFormat class, or look at the API docs.

--------------------------------------------------
Free Database Connection Pooling Software
 
Hello sedj,

I was able to convert my date.
I tried to check if the Date object represents a point in time older than a week using the GregorianCalendar object but I can't find any method to do that. Any ideas...?
 
Code:
long oneWeekMillis = 7 * 86400000; // 7 days * number of millis in a day

Date checkDate = ... // your parsed string date thing

long now = System.currentTimeMillis();
long oneWeekAgo = now - oneWeekMillis;

if (checkDate.getTime() < oneWeekAgo) {

  // date is older than a week ago
}

--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top