java compare dates

Solutions on MaxInterview for java compare dates by the best coders in the world

showing results for - "java compare dates"
Martín
27 Mar 2020
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.
Jeffery
10 Mar 2020
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
Victoria
18 Jun 2016
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 
queries leading to this page
java date comparecompare to date javacompare two date time in javahow to compare dates for if statement javahow to compare 2 dates in javahow to compare datetime javacompare int 5b 5d date in javatime comparision in javacomparing dates in string javahow to compare time in between javacomparing dates with compareto javahow to compare between two dates in javajava compare datesjava compareto datecompare string date in javahow to compare two dates in jaavcompare times javawhat does date compareto return in javajava compare dates without timecompare date java 8compare times in javacompare dates java datejava date compare dayshow to compare dates with timestamp in javajava date compareto examplecompare two string date in javahow to compare time in betweeen javacompare difference between two dates in java java compare two times no datecompare dates and time in javacompare 28date 2c date 29 javacompare java util datejava compare two datetimejava function to compare two datescompare llst by date javadate compare javahow to compare datetime in java by minutescompare string time in javacompare dates in java 8compareto java date examplecomapre dates javacompare int date in javahow to compare two time after or before in javacompare only date day javatwo date comparison in javajava date as string comparejava compare time with current timecompare two dates in javadatetime comparison in javacompare to dates in javajava compare two string datescomparing current date lies b 2fw the two dates in javadate comapreto values in javajava compare 2 datescomparing time in javahow to compare two string dates format in javajava comparing dates without timecompare java util date with current datedateutils class in java compare datesjava comapre datescompare to date time javahow to write a compare date method in javahow to know the current date in java in date object and then compare it with other datecompare date in java8java compare datehow to compare two string dates in javacompare tow dates javahow to compare dates in in java util datejava comparer string et dateshow to compare 2 times in javacompare element by date javacomparing dates in javacomparing dates with time package in javacompare java simple date and datecompare two dates and time in javacompare time in java 8java compare 2 datetimeshow to compare datetime in javacompare java time objectcomparing dates without java date classcompare different times java compare two dates in java for equalhow to compares date in javahow to compare two datetime in javajava compare datetimecompare two dates and time combined in javajava date compare only timecompare date object in javadate comparison javajava instants compare dateshow to compare two time in javahow to compare to date time in javahow to compare date time in javajava date class compare dateshow to compare different dates in javajava program compare two datecompare two string dates in javahow to compare string date in javajava compare 2 datetimecompare time in javahow to compare dates javahow can validate date in java with date 27s comparetojava date comparetdates with diff formats javajava to compare string date timestring date compare with another date in javahow to compare calendar date in javajava date comparison examplecompare ms date javajava compare string and datecompare only time javacompaire date in javato compare two dates in javadate time comparison javajava compare 2 datehow to comapre two time in javajava compare time in javacompare date string in javajava compare two datejava compare two dates stringshow to compare two dates with current date in java 8compare to date in javajava compare dates with timejava compare two timesjava time comparejava date compare before or equalcompare year in date object javacompare two dates in java 8how to compare 2 instance of date in javahow to compare date format in javahow to compare two string dates in javacompare time in between in javajava compare calendar datesdatelocal compare in javahow to compare time in javajava compare with current timecompare strings of date javahow to compare dates in in java util date java documentationcomapring date in javacompare dates simple date javadate compareto method in javacomparing date string javajava compare to datedate string compare javajava compare date stringscompare date with current date in javahow to compare two string date in javajava date compare two datescompareto date javahow compare is before date in javaexample of comparing dates in javajava 8 compare datescompare date only in javacompare current date with another date in javajava compare string dates to nowcompare two date sin javacompare two time in javajava compare string date to current datejava compare datetimcompare date between two dates in java 11compare java dateshow to compare date objects in javajava calendar compare datesdate comparision in javacompare datetime javahow to compare date in java 5chow can validate date in java with comparetotwo date compare error javacompare years in date object javacompare dates equals javacomparing two dates manually in javacompareto in java datejava compare with current datejava compare date timejava compare dates equalhow to compare two date and time in javajava comparing datescompare date and time in javacompare two dates using java 8 date timecompare current time with another time in javacompareto method in java to compare datescompareto date method in javacompare util dates in javahow to compare date in javacompare only date portion of java datecompares two dates in javadiff dates javajava 8 compare 2 datesdate compare java equalsjava compare string and datesjava code to compare 2 t 3ddatescompare date and current date java java util date comparejava date comparetocompare 2 dates in javahow to compare dates in jaavhow to compare java util dates in javadate compareto in javajava compare dates with stringscompare dates without time javajava date comparison with current datecompare two calendar dates in javacompare dates javajava compare date with stringjava date compare yearsjava date comparisonbest code for compare two date in javajava compare dates afterhow to compare two dates in javacompare only date javahow to compare two date times in javacompare date time javahow to compare two dates of util type date in javacompare date in java with todaydate comparison in java with current datehow to compare specific date with current date in javacompare two times in javacompare two date objects javacompare date with to date javacomparing two strin dates in javacompare dates in javacompare 2 dates javacheck date equality javadatetime compare javadate compareto javabest way to compare dates in javacompareto java datehow can validate string date in java with comparetocompare dates if same in javacomparing eqality of dates in javacompare two date in javacompare calendar dates in javahow to compare dates in java using the java util datehow to compare the date in javacompare time javajava compare date with current datejava to compare datecompare two dates without time in java 8date compareto in javadate compare in javacomparing date objects javajava compare hour string formatjava compare two string dates java 8comparator for time in javacomparison date javacompare specif int 5b 5d date in javajava date comparedate compareto javajava 8 compare dates without timejava date comparetohow to compare different date string in javadate object compare javacompare date in javacompare new date javajava compare date by dayhow to compare dates in javahow to compare date with current date in javahow to compare to date in javacomparing date and time in javacomparing two dates in javajava to comapre string date timehow to compare between to time in javajava compare timecomparing date in javadate compareto java examplejava compare to datescompare date javahow to compare two date in javacomparison of date type in javajava date time comparejava date time comparison examplecompareto method in java for datejava compare time from datecomparing two dates by days in javacompare two dates javajava compare date with current timecompare two times javadate object comparetoi javahow to compare dates in if statement javahow to compare two same dates in javahow to compare the time in javadate compareto 28date 29 javadiff two date javajava date compare datedate comparison in javajava compare two dates operation to compare dates in javajavaa compareto datehow to compare date values in javajava compare dates