Program
import java.time.LocalDateTime; public class CompareDateWithTime { public static void main(String[] args) { // TODO Auto-generated method stub LocalDateTime dateTime1 = LocalDateTime.of(2007, 11, 25, 10, 15, 45); LocalDateTime dateTime2 = LocalDateTime.of(1999, 9, 12, 07, 25, 55); Boolean bool1 = dateTime1.isAfter(dateTime2); Boolean bool2 = dateTime1.isBefore(dateTime2); Boolean bool3 = dateTime1.isEqual(dateTime2); if(bool1){ System.out.println(dateTime1+" is after "+dateTime2); } else if(bool2) { System.out.println(dateTime1+" is before "+dateTime2); } else if(bool3) { System.out.println(dateTime1+" is equla to "+dateTime2); } } }
Output
2007-11-25T10:15:45 is after 1999-09-12T07:25:55
Description
public boolean isAfter(ChronoLocalDateTime other)
Checks if this date-time is after the specified date-time.
This checks to see if this date-time represents a point on the local time-line after the other date-time
LocalDate a = LocalDateTime.of(2012, 6, 30, 12, 00);
LocalDate b = LocalDateTime.of(2012, 7, 1, 12, 00);
a.isAfter(b) == false
a.isAfter(a) == false
b.isAfter(a) == true
This method only considers the position of the two date-times on the local time-line. It does not take into account the chronology or calendar system. This is different from the comparison in compareTo(ChronoLocalDateTime), but is the same approach as ChronoLocalDateTime.timeLineOrder().
Specified by:
isAfter in interface ChronoLocalDateTime<LocalDate>
Parameters:
other – the other date-time to compare to, not null
Returns:
true if this date-time is after the specified date-time
public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond)
Obtains an instance of LocalDateTime from year, month, day, hour, minute, second and nanosecond.
This returns a LocalDateTime with the specified year, month, day-of-month, hour, minute, second and nanosecond. The day must be valid for the year and month, otherwise an exception will be thrown.
Parameters:
year – the year to represent, from MIN_YEAR to MAX_YEAR
month – the month-of-year to represent, from 1 (January) to 12 (December)
dayOfMonth – the day-of-month to represent, from 1 to 31
hour – the hour-of-day to represent, from 0 to 23
minute – the minute-of-hour to represent, from 0 to 59
second – the second-of-minute to represent, from 0 to 59
nanoOfSecond – the nano-of-second to represent, from 0 to 999,999,999
Returns:
the local date-time, not null
Throws:
DateTimeException – if the value of any field is out of range,
or if the day-of-month is invalid for the month-year
public boolean isBefore(ChronoLocalDateTime<?> other)
Checks if this date-time is before the specified date-time.
This checks to see if this date-time represents a point on the local time-line before the other date-time.
LocalDate a = LocalDateTime.of(2012, 6, 30, 12, 00);
LocalDate b = LocalDateTime.of(2012, 7, 1, 12, 00);
a.isBefore(b) == true
a.isBefore(a) == false
b.isBefore(a) == false
This method only considers the position of the two date-times on the local time-line. It does not take into account the chronology, or calendar system. This is different from the comparison in compareTo(ChronoLocalDateTime), but is the same approach as ChronoLocalDateTime.timeLineOrder().
Specified by:
isAfter in interface ChronoLocalDateTime<LocalDate>
Parameters:
other – the other date-time to compare to, not null
Returns:
if this date-time is before the specified date-time
public boolean isEqual(ChronoLocalDateTime<?> other)
Checks if this date-time is equal to the specified date-time.
This checks to see if this date-time represents the same point on the local time-line as the other date-time.
LocalDate a = LocalDateTime.of(2012, 6, 30, 12, 00);
LocalDate b = LocalDateTime.of(2012, 7, 1, 12, 00);
a.isEqual(b) == false
a.isEqual(a) == true
b.isEqual(a) == false
This method only considers the position of the two date-times on the local time-line.
It does not take into account the chronology, or calendar system. This is different from the comparison in compareTo(ChronoLocalDateTime), but is the same approach as ChronoLocalDateTime.timeLineOrder().
Specified by:
isAfter in interface ChronoLocalDateTime<LocalDate>
Parameters:
other – the other date-time to compare to, not null
Returns:
if this date-time is equal the specified date-time