Overview
This article describes the various ways to compare two DateTime instances.
- Compare DateTime
- Compare Two DateTime instances considering only Date fields and ignoring TIme fields
- Compare Two DateTime instances considering only Time fields and ignoring Date fields
- Using Upper and Lower Limits for DateTime comparison
Compare DateTime
Use
DateTimeComparator.getInstance()
to get a comparator for both date and time.
Compare Two DateTime instances considering only Date fields and ignoring TIme fields
Use
DateTimeComparator.getDateOnlyInstance()
to get a comparator for date and ignore time.
Even though the time fields for the two DateTime instances is different, we get a result that the two DateTime instances are equal because only date fields are considered for comparison and time fields are ignored.
Compare Two DateTime instances considering only Time fields and ignoring Date fields
Use
DateTimeComparator.getTimeOnlyInstance()
to get a comparator for time and ignore date.
Even though the date fields for the two DateTime instances is different, we get a result that the two DateTime instances are equal because only time fields are considered for comparison and date fields are ignored.
Using Upper and Lower Limits for DateTime comparison
In certain situations, it may be required to compare only certain date and time fields but not others. This can be specified using upper and lower limits.
In the example below, the upper limit is defined as month and lower limit is defined as hour. Hence only those fields within the range of upper and lower limits i.e. month, day and hour shall be considered for comparison. All other fields outside this limit i.e. year, minute and second shall not be considered.
To start, click on the next arrow. If you find anything missing in the tutorials then please drop in your comments.