开发者

java - how to compare same days in date

开发者 https://www.devze.com 2023-03-19 03:58 出处:网络
I have a long data member that represents a date. I cast it to a Date d = new Date(long); I want to now if a nother date has the same day.

I have a long data member that represents a date.

I cast it to a

 Date d = new Date(long);  

I want to now if a nother date has the same day. How do I do it? Thanks.

(For andrew)

Edit :

Found this solution

 SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
 return fmt.format(date1).equals(fmt.format(date2));

in here Comparing two java.util.Dates to see if they are in the same day lo开发者_运维百科oks nice


use the joda api.
http://joda-time.sourceforge.net/
Its a lot easier and better than the Calendar object route in java jdk


Well you can convert them both to calendar Objects and get the calendar objects day and compare that way.

Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(LONG VALUE HERE);
int day = cal.get(Calendar.DAY_OF_MONTH);

Do the same thing with the other date, and compare the values.

edit: By the way, you are not casting the long to a date, you are just creating a Date object using a long.


Use apache commons.

 DateUtils.isSameDay(date1, date2);


To see if the dates are equal:

date_one.equals(date_two);

To see if just the day is equal, I usually chop the time off the date (setHours(0), setMinutes(0), etc.) and then use the .equals() method.


Use java.util.Calendar for all comparison operations.

0

精彩评论

暂无评论...
验证码 换一张
取 消