logo

Hvordan sammenligne datoer i Java

I Java , mens vi forholder oss til Dato og tid , noen ganger trenger vi det sammenligne datoer . De sammenligning av datoer i Java er ikke det samme som sammenligning av to tall. Så det er en litt vanskelig oppgave sammenligne to datoer i Java . Vi trenger ikke å implementere noen logikk for å sammenligne datoer . For å gjøre denne oppgaven enkel Java gir sammenlignTil(), før(), etter(), og er lik() metode. I denne delen skal vi lære hvordan sammenligne to datoer i Java .

Det er fire klasser i Java som gir metoder for å sammenligne to datoer.

  • Ved hjelp av sammenligne med() Metode
  • Ved hjelp av Dato Klasse
  • Ved hjelp av Kalender Klasse
  • Ved hjelp av LocalDate Klasse

Bruke Date.compareTo()-metoden

Java Dato klasse gir ulike metoder knyttet til tid og datoer. Det er en klasse avjava.utilpakke. Klassen implementerer grensesnittene Serialiserbare, Klonbare og Sammenlignbare.

For sammenligning av to datoer gir klassen sammenligne med() metode. Den sammenligner datoer for bestilling. Den analyserer en dato (som skal sammenlignes) som en parameter. Det kaster NullPointerException hvis argumentdatoen er null.

Syntaks:

 public int compareTo(Date anotherDate) 

Den returnerer heltallsverdier:

    0:hvis begge datoene er like.En verdi mindre enn 0:hvis datoen er før argumentasjonsdatoen.En verdi større enn 0:hvis datoen er etter argumentdatoen.

Huske: Hvis du har å gjøre med dato i Java, ikke glem å importere java.text.SimpleDateFormat, java.text.ParseException,java.util.Dato.

kassen med git

La oss implementere compareTo()-metoden og sammenligne to datoer.

I det følgende eksempelet har vi laget en forekomst av SimpleDateFormat klasse som lar oss ta forskjellige datoformater. Etter det har vi tatt to variabler dato 1 og dato2 av typen Dato. Ved å bruke analysere() metoden til SimpleDateFormat-klassen, har vi analysert datoene som skal sammenlignes. Metoden returnerer en Dato analysert fra strengen. Vi har passert variabelen date1 og date2 av typen Date i format() metode. Metoden gir den formaterte dato-/tidsstrengen.

For å sammenligne de to datoene har vi brukt sammenligne med() metode. Hvis begge datoene er like, skrives den ut Begge datoene er like. Hvis dato1 er større enn dato2 , den skrives ut Dato 1 kommer etter dato 2 . Hvis dato1 er mindre enn dato2 , den skrives ut Dato 1 kommer etter dato 2 .

CompareDatesExample1.java

 import java.util.Date; import java.text.SimpleDateFormat; import java.text.ParseException; public class CompareDatesExample1 { public static void main(String[] args) throws ParseException { //object of SimpleDateFormat class SimpleDateFormat sdf = new SimpleDateFormat(&apos;yyyy-MM-dd&apos;); //dates to be compare Date date1 = sdf.parse(&apos;2020-07-20&apos;); Date date2 = sdf.parse(&apos;2020-06-18&apos;); //prints dates System.out.println(&apos;Date 1: &apos; + sdf.format(date1)); System.out.println(&apos;Date 2: &apos; + sdf.format(date2)); //comparing dates if(date1.compareTo(date2) &gt; 0) { System.out.println(&apos;Date 1 comes after Date 2&apos;); } else if(date1.compareTo(date2) <0) 1 { system.out.println('date comes before date 2'); } else if(date1.compareto(date2)="=" 0) system.out.println('both dates are equal'); < pre> <p> <strong>Output:</strong> </p> <pre> Date 1: 2020-07-20 Date 2: 2020-06-18 Date 1 comes after Date 2 </pre> <h2>Using Date Class</h2> <p>Java date class provides before() , after() , and equals() method to compare two dates.</p> <p> <strong>before():</strong> The method check that the date comes before the specified date or not. It parses a parameter of type Date. It returns <strong>true</strong> if and only if the instant of time represented by this Date object is strictly earlier than the instant represented by when, <strong>false</strong> otherwise.</p> <p> <strong>Syntax:</strong> </p> <pre> public boolean before(Date when) </pre> <p>It throws <strong>NullPointerException</strong> if when is null.</p> <p> <strong>after():</strong> The method check that the date comes after the specified date or not. It parses a parameter of type Date. It returns <strong>true</strong> if and only if the instant of time represented by this Date object is strictly later than the instant represented by when, <strong>false</strong> otherwise.</p> <p> <strong>Syntax:</strong> </p> <pre> public boolean after(Date when) </pre> <p>It throws <strong>NullPointerException</strong> if when is null.</p> <p> <strong>equals():</strong> The method checks (compare) the equality of two dates. It overrides the equals() method of the Object class. It returns true if the objects are same, else returns false. Therefore, the Date objects will be equal if and only if the getTime() method returns the same long value for both dates.</p> <p> <strong>Syntax:</strong> </p> <pre> public boolean equals (Object obj) </pre> <p>Let&apos;s use the above-explained method in an example and compare two dates with the help of these methods.</p> <p> <strong>CompareDatesExample2.java</strong> </p> <pre> import java.util.Date; import java.text.ParseException; import java.text.SimpleDateFormat; public class CompareDatesExample2 { public static void main(String args[]) throws ParseException { //Creating an object of the SimpleDateFormat class SimpleDateFormat sdfo = new SimpleDateFormat(&apos;yyyy-MM-dd&apos;); //dates to be compared Date date1 = sdfo.parse(&apos;2019-01-01&apos;); Date date2 = sdfo.parse(&apos;2020-01-01&apos;); // Print the dates System.out.println(&apos;Date1: &apos; + sdfo.format(date1)); System.out.println(&apos;Date2: &apos; + sdfo.format(date2)); //Compare the two dates if (date1.after(date2)) { //if date1&gt;date2, prints the following statement System.out.println(&apos;Date1 comes after Date2&apos;); } else if (date1.before(date2)) { //if date1<date2, prints the following statement system.out.println('date1 comes before date2'); } else if (date1.equals(date2)) { date1="date2" system.out.println('both dates are equal'); < pre> <p> <strong>Output:</strong> </p> <pre> Date1: 2019-01-01 Date2: 2020-01-01 Date1 comes before Date2 </pre> <h2>Using Calendar Class</h2> <p>Like the Java Date class, the <a href="/java-calendar-class"> <strong>Calendar</strong> class</a> also provides before() , after() , and equals() methods . All three methods have the same signature, as we have explained above.</p> <p>Let&apos;s use the Calendar class and compare two dates with the help of after(), before(), and equals() method.</p> <p>In the following example, we have used the same method used in the previous example, except the <strong>getInstance()</strong> and <strong>setTime()</strong> methods.</p> <p> <strong>getInstance():</strong> It is a static method of the Calendar. It returns a Calendar using the default time zone and locale.</p> <p> <strong>Syntax:</strong> </p> <pre> public static Calendar getInstance() </pre> <p> <strong>setTime():</strong> The method sets the calendar time according to the specified date. It parses a parameter of type Date.</p> <p> <strong>Syntax:</strong> </p> <pre> public final void setTime(Date date) </pre> <p> <strong>CompareDatesExample3.java</strong> </p> <pre> import java.util.Date; import java.util.Calendar; import java.text.ParseException; import java.text.SimpleDateFormat; public class CompareDatesExample3 { public static void main(String args[]) throws ParseException { // Create SimpleDateFormat object SimpleDateFormat sdf = new SimpleDateFormat(&apos;yyyy-MM-dd&apos;); //dates to be compare Date date1 = sdf.parse(&apos;2020-12-01&apos;); Date date2 = sdf.parse(&apos;2020-12-01&apos;); // Prints the dates System.out.println(&apos;Date1: &apos; + sdf.format(date1)); System.out.println(&apos;Date2: &apos; + sdf.format(date2)); //invoking getInstance() method Calendar cal1 = Calendar.getInstance(); Calendar cal2 = Calendar.getInstance(); cal1.setTime(date1); cal2.setTime(date2); //compare two dates if (cal1.after(cal2)) { //if date1&gt;date2 System.out.println(&apos;Date1 comes after Date2&apos;); } else if (cal1.before(cal2)) { //if date1<date2 system.out.println('date1 comes before date2'); } else if (cal1.equals(cal2)) { date1="date2" system.out.println('both dates are equal'); < pre> <p> <strong>Output:</strong> </p> <pre> Date1: 2020-12-01 Date2: 2020-12-01 Both dates are equal </pre> <h2>Using LocalDate Class</h2> <p>Java provides another <strong>LocalDate</strong> class to compare two LocalDate, LocalTime, and LocalDateTime. It is the member of <span>java.time</span> package. The class provides isBefore(), isAfter(), isEquals(), and compareTo() method to compare dates. These methods works same as the method before(), after(), and equals() of the Date and Calendar class.</p> <p>Let&apos;s use the <a href="/java-localdate-class">LocalDate class</a> in an example to compare two dates.</p> <p>In the following example, we have used the following method to compare two dates. All the methods check the dates according to the local-time line.</p> <p> <strong>of():</strong> It is a static method of LocalDate class. It obtains an instance of LocalDate form year, month, and day. It accepts three parameters year, month, and date of type int. It returns a LocalDate with the specified date.</p> <p> <strong>Syntax:</strong> </p> <pre> public static LocalDate of(int year, int month, int dayOfMonth) </pre> <p>where:</p> <p> <strong>year:</strong> must be between MIN_YEAR to MAX_YEAR.</p> <p> <strong>month:</strong> must be between 1 (January) to 12 (December).</p> <p> <strong>datOfMonth:</strong> must be between 1 to 31.</p> <p>It throws DateTimeException if the value of any parameter is out of range.</p> <p> <strong>isBefore():</strong> The method checks the date is before the specified date. It parses a date (to compare) as a parameter. It returns true if and only if the date is before the specified date. Its comparison approach is different from <strong>compareTo(ChronoLocalDate).</strong> </p> <p> <strong>Syntax:</strong> </p> <pre> public boolean isBefore(ChronoLocalDate other) </pre> <p> <strong>isAfter():</strong> The method checks the date is before the specified date. It parses a date (to compare) as a parameter. It returns true if and only if the date is before the specified date. Its comparison approach is different from <strong>compareTo(ChronoLocalDate)</strong> .</p> <p> <strong>Syntax:</strong> </p> <pre> public boolean isAfter(ChronoLocalDate other) </pre> <p> <strong>isEqual():</strong> The method compares the dates are equal or not. If both dates are equal it returns true, false otherwise. It parses a date (to compare) as a parameter.</p> <p>It returns true if and only if the date is before the specified date. Its comparison approach is different from <strong>compareTo(ChronoLocalDate).</strong> </p> <p> <strong>Syntax:</strong> </p> <pre> public boolean isEqual(ChronoLocalDate other) </pre> <p> <strong>CompareDatesExample4.java</strong> </p> <pre> import java.time.LocalDate; public class CompareDatesExample4 { public static void main(String[] args) { // Create LocalDate objects with dates LocalDate date1 = LocalDate.of(2020,9,29); LocalDate date2 = LocalDate.of(2020,12,07); // Print the Dates System.out.println(&apos;Date1: &apos; + date1); System.out.println(&apos;Date2: &apos; + date2); //comparing two dates if (date1.isAfter(date2)) { System.out.println(&apos;Date1 comes after Date2&apos;); } else if (date1.isBefore(date2)) { System.out.println(&apos;Date1 comes before Date2&apos;); } else if (date1.isEqual(date2)) { System.out.println(&apos;Both dates are equal&apos;); } } } </pre> <p> <strong>Output:</strong> </p> <pre> Date1: 2020-09-29 Date2: 2020-12-07 Date1 comes before Date2 </pre> <hr></date2></pre></date2,></pre></0)>

Bruke datoklasse

Java-datoklassen gir metoden before() , after() , og equals() for å sammenligne to datoer.

før(): Metoden kontrollerer at datoen kommer før den angitte datoen eller ikke. Den analyserer en parameter av typen Dato. Den kommer tilbake ekte hvis og bare hvis tidspunktet representert av dette Dato-objektet er strengt tatt tidligere enn øyeblikket representert av når, falsk ellers.

Syntaks:

 public boolean before(Date when) 

Det kaster NullPointerException hvis når er null.

etter(): Metoden kontrollerer at datoen kommer etter den angitte datoen eller ikke. Den analyserer en parameter av typen Dato. Den kommer tilbake ekte hvis og bare hvis tidspunktet representert av dette Dato-objektet er strengt tatt senere enn øyeblikket representert av når, falsk ellers.

Syntaks:

type variabler java
 public boolean after(Date when) 

Det kaster NullPointerException hvis når er null.

er lik(): Metoden kontrollerer (sammenlign) likheten mellom to datoer. Den overstyrer equals()-metoden til Object-klassen. Den returnerer sann hvis objektene er like, ellers returnerer den usann. Derfor vil Dato-objektene være like hvis og bare hvis getTime()-metoden returnerer den samme lange verdien for begge datoene.

Syntaks:

 public boolean equals (Object obj) 

La oss bruke den ovenfor forklarte metoden i et eksempel og sammenligne to datoer ved hjelp av disse metodene.

lineært søk i java

CompareDatesExample2.java

 import java.util.Date; import java.text.ParseException; import java.text.SimpleDateFormat; public class CompareDatesExample2 { public static void main(String args[]) throws ParseException { //Creating an object of the SimpleDateFormat class SimpleDateFormat sdfo = new SimpleDateFormat(&apos;yyyy-MM-dd&apos;); //dates to be compared Date date1 = sdfo.parse(&apos;2019-01-01&apos;); Date date2 = sdfo.parse(&apos;2020-01-01&apos;); // Print the dates System.out.println(&apos;Date1: &apos; + sdfo.format(date1)); System.out.println(&apos;Date2: &apos; + sdfo.format(date2)); //Compare the two dates if (date1.after(date2)) { //if date1&gt;date2, prints the following statement System.out.println(&apos;Date1 comes after Date2&apos;); } else if (date1.before(date2)) { //if date1<date2, prints the following statement system.out.println(\'date1 comes before date2\'); } else if (date1.equals(date2)) { date1="date2" system.out.println(\'both dates are equal\'); < pre> <p> <strong>Output:</strong> </p> <pre> Date1: 2019-01-01 Date2: 2020-01-01 Date1 comes before Date2 </pre> <h2>Using Calendar Class</h2> <p>Like the Java Date class, the <a href="/java-calendar-class"> <strong>Calendar</strong> class</a> also provides before() , after() , and equals() methods . All three methods have the same signature, as we have explained above.</p> <p>Let&apos;s use the Calendar class and compare two dates with the help of after(), before(), and equals() method.</p> <p>In the following example, we have used the same method used in the previous example, except the <strong>getInstance()</strong> and <strong>setTime()</strong> methods.</p> <p> <strong>getInstance():</strong> It is a static method of the Calendar. It returns a Calendar using the default time zone and locale.</p> <p> <strong>Syntax:</strong> </p> <pre> public static Calendar getInstance() </pre> <p> <strong>setTime():</strong> The method sets the calendar time according to the specified date. It parses a parameter of type Date.</p> <p> <strong>Syntax:</strong> </p> <pre> public final void setTime(Date date) </pre> <p> <strong>CompareDatesExample3.java</strong> </p> <pre> import java.util.Date; import java.util.Calendar; import java.text.ParseException; import java.text.SimpleDateFormat; public class CompareDatesExample3 { public static void main(String args[]) throws ParseException { // Create SimpleDateFormat object SimpleDateFormat sdf = new SimpleDateFormat(&apos;yyyy-MM-dd&apos;); //dates to be compare Date date1 = sdf.parse(&apos;2020-12-01&apos;); Date date2 = sdf.parse(&apos;2020-12-01&apos;); // Prints the dates System.out.println(&apos;Date1: &apos; + sdf.format(date1)); System.out.println(&apos;Date2: &apos; + sdf.format(date2)); //invoking getInstance() method Calendar cal1 = Calendar.getInstance(); Calendar cal2 = Calendar.getInstance(); cal1.setTime(date1); cal2.setTime(date2); //compare two dates if (cal1.after(cal2)) { //if date1&gt;date2 System.out.println(&apos;Date1 comes after Date2&apos;); } else if (cal1.before(cal2)) { //if date1<date2 system.out.println(\'date1 comes before date2\'); } else if (cal1.equals(cal2)) { date1="date2" system.out.println(\'both dates are equal\'); < pre> <p> <strong>Output:</strong> </p> <pre> Date1: 2020-12-01 Date2: 2020-12-01 Both dates are equal </pre> <h2>Using LocalDate Class</h2> <p>Java provides another <strong>LocalDate</strong> class to compare two LocalDate, LocalTime, and LocalDateTime. It is the member of <span>java.time</span> package. The class provides isBefore(), isAfter(), isEquals(), and compareTo() method to compare dates. These methods works same as the method before(), after(), and equals() of the Date and Calendar class.</p> <p>Let&apos;s use the <a href="/java-localdate-class">LocalDate class</a> in an example to compare two dates.</p> <p>In the following example, we have used the following method to compare two dates. All the methods check the dates according to the local-time line.</p> <p> <strong>of():</strong> It is a static method of LocalDate class. It obtains an instance of LocalDate form year, month, and day. It accepts three parameters year, month, and date of type int. It returns a LocalDate with the specified date.</p> <p> <strong>Syntax:</strong> </p> <pre> public static LocalDate of(int year, int month, int dayOfMonth) </pre> <p>where:</p> <p> <strong>year:</strong> must be between MIN_YEAR to MAX_YEAR.</p> <p> <strong>month:</strong> must be between 1 (January) to 12 (December).</p> <p> <strong>datOfMonth:</strong> must be between 1 to 31.</p> <p>It throws DateTimeException if the value of any parameter is out of range.</p> <p> <strong>isBefore():</strong> The method checks the date is before the specified date. It parses a date (to compare) as a parameter. It returns true if and only if the date is before the specified date. Its comparison approach is different from <strong>compareTo(ChronoLocalDate).</strong> </p> <p> <strong>Syntax:</strong> </p> <pre> public boolean isBefore(ChronoLocalDate other) </pre> <p> <strong>isAfter():</strong> The method checks the date is before the specified date. It parses a date (to compare) as a parameter. It returns true if and only if the date is before the specified date. Its comparison approach is different from <strong>compareTo(ChronoLocalDate)</strong> .</p> <p> <strong>Syntax:</strong> </p> <pre> public boolean isAfter(ChronoLocalDate other) </pre> <p> <strong>isEqual():</strong> The method compares the dates are equal or not. If both dates are equal it returns true, false otherwise. It parses a date (to compare) as a parameter.</p> <p>It returns true if and only if the date is before the specified date. Its comparison approach is different from <strong>compareTo(ChronoLocalDate).</strong> </p> <p> <strong>Syntax:</strong> </p> <pre> public boolean isEqual(ChronoLocalDate other) </pre> <p> <strong>CompareDatesExample4.java</strong> </p> <pre> import java.time.LocalDate; public class CompareDatesExample4 { public static void main(String[] args) { // Create LocalDate objects with dates LocalDate date1 = LocalDate.of(2020,9,29); LocalDate date2 = LocalDate.of(2020,12,07); // Print the Dates System.out.println(&apos;Date1: &apos; + date1); System.out.println(&apos;Date2: &apos; + date2); //comparing two dates if (date1.isAfter(date2)) { System.out.println(&apos;Date1 comes after Date2&apos;); } else if (date1.isBefore(date2)) { System.out.println(&apos;Date1 comes before Date2&apos;); } else if (date1.isEqual(date2)) { System.out.println(&apos;Both dates are equal&apos;); } } } </pre> <p> <strong>Output:</strong> </p> <pre> Date1: 2020-09-29 Date2: 2020-12-07 Date1 comes before Date2 </pre> <hr></date2></pre></date2,>

Bruke kalenderklasse

I likhet med Java Date-klassen Kalender klasse gir også metodene before() , after() og equals() . Alle tre metodene har samme signatur, som vi har forklart ovenfor.

La oss bruke klassen Kalender og sammenligne to datoer ved hjelp av metoden after(), before() og equals().

I det følgende eksempelet har vi brukt den samme metoden som ble brukt i det forrige eksempelet, bortsett fra getInstance() og setTime() metoder.

getInstance(): Det er en statisk metode for kalenderen. Den returnerer en kalender med standard tidssone og lokalitet.

Syntaks:

 public static Calendar getInstance() 

setTime(): Metoden setter kalendertiden i henhold til den angitte datoen. Den analyserer en parameter av typen Dato.

Syntaks:

enkel datoformater i java
 public final void setTime(Date date) 

CompareDatesExample3.java

 import java.util.Date; import java.util.Calendar; import java.text.ParseException; import java.text.SimpleDateFormat; public class CompareDatesExample3 { public static void main(String args[]) throws ParseException { // Create SimpleDateFormat object SimpleDateFormat sdf = new SimpleDateFormat(&apos;yyyy-MM-dd&apos;); //dates to be compare Date date1 = sdf.parse(&apos;2020-12-01&apos;); Date date2 = sdf.parse(&apos;2020-12-01&apos;); // Prints the dates System.out.println(&apos;Date1: &apos; + sdf.format(date1)); System.out.println(&apos;Date2: &apos; + sdf.format(date2)); //invoking getInstance() method Calendar cal1 = Calendar.getInstance(); Calendar cal2 = Calendar.getInstance(); cal1.setTime(date1); cal2.setTime(date2); //compare two dates if (cal1.after(cal2)) { //if date1&gt;date2 System.out.println(&apos;Date1 comes after Date2&apos;); } else if (cal1.before(cal2)) { //if date1<date2 system.out.println(\'date1 comes before date2\'); } else if (cal1.equals(cal2)) { date1="date2" system.out.println(\'both dates are equal\'); < pre> <p> <strong>Output:</strong> </p> <pre> Date1: 2020-12-01 Date2: 2020-12-01 Both dates are equal </pre> <h2>Using LocalDate Class</h2> <p>Java provides another <strong>LocalDate</strong> class to compare two LocalDate, LocalTime, and LocalDateTime. It is the member of <span>java.time</span> package. The class provides isBefore(), isAfter(), isEquals(), and compareTo() method to compare dates. These methods works same as the method before(), after(), and equals() of the Date and Calendar class.</p> <p>Let&apos;s use the <a href="/java-localdate-class">LocalDate class</a> in an example to compare two dates.</p> <p>In the following example, we have used the following method to compare two dates. All the methods check the dates according to the local-time line.</p> <p> <strong>of():</strong> It is a static method of LocalDate class. It obtains an instance of LocalDate form year, month, and day. It accepts three parameters year, month, and date of type int. It returns a LocalDate with the specified date.</p> <p> <strong>Syntax:</strong> </p> <pre> public static LocalDate of(int year, int month, int dayOfMonth) </pre> <p>where:</p> <p> <strong>year:</strong> must be between MIN_YEAR to MAX_YEAR.</p> <p> <strong>month:</strong> must be between 1 (January) to 12 (December).</p> <p> <strong>datOfMonth:</strong> must be between 1 to 31.</p> <p>It throws DateTimeException if the value of any parameter is out of range.</p> <p> <strong>isBefore():</strong> The method checks the date is before the specified date. It parses a date (to compare) as a parameter. It returns true if and only if the date is before the specified date. Its comparison approach is different from <strong>compareTo(ChronoLocalDate).</strong> </p> <p> <strong>Syntax:</strong> </p> <pre> public boolean isBefore(ChronoLocalDate other) </pre> <p> <strong>isAfter():</strong> The method checks the date is before the specified date. It parses a date (to compare) as a parameter. It returns true if and only if the date is before the specified date. Its comparison approach is different from <strong>compareTo(ChronoLocalDate)</strong> .</p> <p> <strong>Syntax:</strong> </p> <pre> public boolean isAfter(ChronoLocalDate other) </pre> <p> <strong>isEqual():</strong> The method compares the dates are equal or not. If both dates are equal it returns true, false otherwise. It parses a date (to compare) as a parameter.</p> <p>It returns true if and only if the date is before the specified date. Its comparison approach is different from <strong>compareTo(ChronoLocalDate).</strong> </p> <p> <strong>Syntax:</strong> </p> <pre> public boolean isEqual(ChronoLocalDate other) </pre> <p> <strong>CompareDatesExample4.java</strong> </p> <pre> import java.time.LocalDate; public class CompareDatesExample4 { public static void main(String[] args) { // Create LocalDate objects with dates LocalDate date1 = LocalDate.of(2020,9,29); LocalDate date2 = LocalDate.of(2020,12,07); // Print the Dates System.out.println(&apos;Date1: &apos; + date1); System.out.println(&apos;Date2: &apos; + date2); //comparing two dates if (date1.isAfter(date2)) { System.out.println(&apos;Date1 comes after Date2&apos;); } else if (date1.isBefore(date2)) { System.out.println(&apos;Date1 comes before Date2&apos;); } else if (date1.isEqual(date2)) { System.out.println(&apos;Both dates are equal&apos;); } } } </pre> <p> <strong>Output:</strong> </p> <pre> Date1: 2020-09-29 Date2: 2020-12-07 Date1 comes before Date2 </pre> <hr></date2>

Bruker LocalDate Class

Java gir en annen LocalDate klasse for å sammenligne to LocalDate, LocalTime og LocalDateTime. Det er medlem avjava.tidpakke. Klassen gir isBefore(), isAfter(), isEquals() og compareTo() metode for å sammenligne datoer. Disse metodene fungerer på samme måte som metoden før(), etter() og lik() i klassen Dato og kalender.

La oss bruke LocalDate-klassen i et eksempel for å sammenligne to datoer.

I det følgende eksempelet har vi brukt følgende metode for å sammenligne to datoer. Alle metodene sjekker datoene i henhold til lokaltidslinjen.

av(): Det er en statisk metode for LocalDate-klassen. Den henter en forekomst av LocalDate fra år, måned og dag. Den godtar tre parametere år, måned og dato for type int. Den returnerer en LocalDate med den angitte datoen.

Syntaks:

 public static LocalDate of(int year, int month, int dayOfMonth) 

hvor:

år: må være mellom MIN_YEAR og MAX_YEAR.

måned: må være mellom 1 (januar) til 12 (desember).

datOfMonth: må være mellom 1 og 31.

Det kaster DateTimeException hvis verdien til en parameter er utenfor området.

erFør(): Metoden kontrollerer at datoen er før den angitte datoen. Den analyserer en dato (for å sammenligne) som en parameter. Den returnerer sann hvis og bare hvis datoen er før den angitte datoen. Dens sammenligningstilnærming er forskjellig fra compareTo(ChronoLocalDate).

Syntaks:

 public boolean isBefore(ChronoLocalDate other) 

erEtter(): Metoden kontrollerer at datoen er før den angitte datoen. Den analyserer en dato (for å sammenligne) som en parameter. Den returnerer sann hvis og bare hvis datoen er før den angitte datoen. Dens sammenligningstilnærming er forskjellig fra compareTo(ChronoLocalDate) .

streng til int

Syntaks:

 public boolean isAfter(ChronoLocalDate other) 

er lik(): Metoden sammenligner datoene er like eller ikke. Hvis begge datoene er like, returnerer det sant, ellers usant. Den analyserer en dato (for å sammenligne) som en parameter.

Den returnerer sann hvis og bare hvis datoen er før den angitte datoen. Dens sammenligningstilnærming er forskjellig fra compareTo(ChronoLocalDate).

Syntaks:

 public boolean isEqual(ChronoLocalDate other) 

CompareDatesExample4.java

 import java.time.LocalDate; public class CompareDatesExample4 { public static void main(String[] args) { // Create LocalDate objects with dates LocalDate date1 = LocalDate.of(2020,9,29); LocalDate date2 = LocalDate.of(2020,12,07); // Print the Dates System.out.println(&apos;Date1: &apos; + date1); System.out.println(&apos;Date2: &apos; + date2); //comparing two dates if (date1.isAfter(date2)) { System.out.println(&apos;Date1 comes after Date2&apos;); } else if (date1.isBefore(date2)) { System.out.println(&apos;Date1 comes before Date2&apos;); } else if (date1.isEqual(date2)) { System.out.println(&apos;Both dates are equal&apos;); } } } 

Produksjon:

 Date1: 2020-09-29 Date2: 2020-12-07 Date1 comes before Date2