Java – how do I know if the date is on the same day as another date

If I have two dates in Java (Android) (date1 and date2), how can I know if date2 is on the same day as date1? (Note: not if date2-date1 < 24 hours). Some examples (same month and year)

@H_502_5@ Date1: day=14 hour=00:00           Date2: day=14 hour=00:00    --> Result TRUE
 Date1: day=13 hour=23:59           Date2: day=14 hour=00:00    --> Result FALSE
 Date1: day=14 hour=05:00           Date2: day=14 hour=00:00    --> Result TRUE
 Date1: day=14 hour=00:00           Date2: day=15 hour=00:00    --> Result FALSE

thank you

resolvent:

Why don't you use dateutils?

You can directly call methods such as issameday

@H_502_5@if (DateUtils.isSameDay(date1, date2)) {
    System.out.println("Same Date");
} else if (date1.before(date2)) {
    System.out.println("date1 before date2");
} else {
    System.out.println("date1 after date2");
}

Check Apache dateutils

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>