How to find out if there is a date before a certain time in Java?

For example, for java.util.Date, how can I check if it was before 12:30 pm on that day?

+5
source share
7 answers

See the Calendar.before () Method.

Calendar now = Calendar.getInstance();
now.set(Calendar.HOUR_OF_DAY, 12);
now.set(Calendar.MINUTE, 30);
Calendar givenDate = Calendar.getInstance();
givenDate.setTime(yourDate);

boolean isBefore = now.before(givenDate);
+12
source

It works. It only checks with the hourly and minute parts of your date:

Date yourDate;
Calendar calendar = Calendar.getInstance();
calendar.setTime(yourDate);
boolean before = calendar.get(Calendar.HOUR) * 60 + calendar.get(Calendar.MINUTE) < 12 * 60 + 30;
+1
source

12:30 .

0

GregorianCalendar.

0

before() java.util.Date , Calendar.settime()

0

startDate endDate , - :

if (startDate.before(endDate))
{
  System.out.println("Date is before your given date");
}
0
source

I think @Bohemian has a better answer if you do not want to add dependencies, but IMO you need to save some time and abandon java classes and add joda time to your project :)

    static boolean isBefore1230(Date d) {
        return new DateTime(d).getMinuteOfDay() < 12 * 60 + 30;
    }
0
source

All Articles