Comparing dates in Java

User avatar
Sabina
Уже с Приветом
Posts: 5669
Joined: 13 Oct 2000 09:01
Location: East Bay, CA

Comparing dates in Java

Post by Sabina »

I need to check if given date is within startDate-endDate.

All the methods I found in java.util.Date and java.util.Calendar are "before", "after" and "equals".

Is there nicer solution then combining all 4 conditions?

I mean I would like something like:

date>=startdate && date>=endDate; OR
date.isWithin(startEnd,endDate);

rather then:

date.after(startDate) &&
date.before(endDate) ||
date.equals(startEnd) ||
date.equals(endDate);

Thanks,
Sabina
User avatar
IsterGray
Новичок
Posts: 38
Joined: 07 Oct 2003 18:41

Re: Comparing dates in Java

Post by IsterGray »

Sabina wrote:I need to check if given date is within startDate-endDate.

All the methods I found in java.util.Date and java.util.Calendar are "before", "after" and "equals".

Is there nicer solution then combining all 4 conditions?

I mean I would like something like:

date>=startdate && date>=endDate; OR
date.isWithin(startEnd,endDate);

rather then:

date.after(startDate) &&
date.before(endDate) ||
date.equals(startEnd) ||
date.equals(endDate);

Thanks,
Sabina


Преобразуй свою дату в long - это число в секундах от 1 января 1970 (помоему).
Для этого используй Date.getTime() и далее работай как с обычными числами:
date.getTime()>=startdate &&date.getTime()>=endDate;

Return to “Вопросы и новости IT”