1Date has before and after methods and can be compared to each other as follows:
2
3if(todayDate.after(historyDate) && todayDate.before(futureDate)) {
4 // In between
5}
6For an inclusive comparison:
7
8if(!historyDate.after(todayDate) && !futureDate.before(todayDate)) {
9 /* historyDate <= todayDate <= futureDate */
10}
11You could also give Joda-Time a go, but note that:
12
13Joda-Time is the de facto standard date and time library for Java prior to Java SE 8. Users are now asked to migrate to java.time (JSR-310).
14
15Back-ports are available for Java 6 and 7 as well as Android.
1for ( Map.Entry<DayOfWeek , LocalTime> entry : dayToTimeMap.entrySet () ) {
2 DayOfWeek key = entry.getKey ();
3 LocalTime value = entry.getValue ();
4 int comparison = key.compareTo ( today );
5 if ( comparison < 0 ) { // if earlier day…
6 earlier.add ( key );
7 } else if ( comparison == 0 ) { //If same day…
8 if ( value.isBefore ( now ) ) {
9 earlier.add ( key );
10 } else { // Else same time as now or later than now…
11 later.add ( key );
12 }
13 } else if ( comparison > 0 ) {
14 later.add ( key );
15 } else {
16 throw new RuntimeException ( "Unexpectedly reached IF-ELSE for comparison: " + comparison );
17 }
18}
19
1package javaapplication;
2
3import java.text.*;
4import java.util.Date;
5import java.text.ParseException;
6
7public class date {
8
9 public static void main(String[] args) {
10
11 String startDate = "21-08-2027";
12 String endDate = "21/08/2021";
13
14 try {
15
16 Date dateStart = new SimpleDateFormat("dd-MM-yyyy").parse(startDate);
17
18 Date dateEnd = new SimpleDateFormat("dd/MM/yyyy").parse(endDate);
19
20 if (dateStart.after(dateEnd)) {
21 System.out.println("Start Date is greater than End Date");
22
23 } else if (dateStart.before(dateEnd)) {
24
25 System.out.println("Start Date is less than End Date");
26
27 } else {
28
29 System.out.println("Start Date is Equal to End Date");
30 }
31
32 } catch (ParseException e) {
33 e.printStackTrace();
34 }
35
36 }
37}
38