Check if the date is set after the specified date?

I'm trying to check for a date that should only accept current and future dates, if the date is older, then it should show

Date older than current day

I also want to resolve the current date. Now, indicating the current day as gievnDate, it always shows

Date older than current day

But I expect the result as

Date is the future day for givenDate as it is today.

Below is the code I tried:

    Date current = new Date();
    String myFormatString = "dd/MM/yy";
    SimpleDateFormat df = new SimpleDateFormat(myFormatString);
    Date givenDate = df.parse("15/02/13");
    Long l = givenDate.getTime();
    //create date object
    Date next = new Date(l);
    //compare both dates
    if(next.after(current) || (next.equals(current))){
        System.out.println("The date is future day");
    } else {

        System.out.println("The date is older than current day");
    }

Here, when I check from 15/02/13, it shows up as older than the current day. Is my method wrong? Or are there any more efficient approaches?

+5
source share
5 answers

In addition to the logical error that the Assyrians pointed out:

, givenDate - "15/02/13 00: 00: 00.000" . , ( "" ), givenDate "", "".

givenDate , "".

+3

if(next.after(current) && (next.equals(current))) false, next current current .

, :

if(next.after(current) || (next.equals(current))) OR.

+8

, , . :

if(next.after(current) && (next.equals(current))){

true, , next.after(current) next.equals(current) .

, :

if(next.after(current) || (next.equals(current))){

, , , , : ( , ), ( ), , ?

+2

TL;DR

LocalDate.parse( "15/02/13" , DateTimeFormatter.ofPattern( "dd/MM/uu" ) )
    .isBefore( LocalDate.now( ZoneId.of( "America/Montreal" ) ) )

.

, , , java.time.

isBefore isAfter java.time.LocalDate.

LocalDate

LocalDate .

. . , - , "" .

ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );

DateTimeFormatter

, DateTimeFormatter.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uu" );
String input = "15/02/13";
LocalDate ld = LocalDate.parse( input , f );

LocalDate, , compareTo, equals, isAfter, isBefore, isEqual.

String message = "ERROR - Message not set. Error # f633d13d-fbbc-49a7-9ee8-bcd1cfa99183." ;
if( ld.isBefore( today ) ) {  // Before today.
   message = "The date: " + ld + " is in the past, before today: " + today );
} else if( ld.isEqual( today ) ) {  // On today.
   message = "The date: " + ld + " is today: " + today );
} else if( ld.isAfter( today ) ) {  // After today.
   message = "The date: " + ld + " is in the future, later than today: " + today );
} else {  // Double-check.
   message = "ERROR – Unexpectedly reached Case Else. Error # c4d56437-ddc3-4ac8-aaf0-e0b35fb52bed." ) ;
}

, , , . , , -, . .

ISO 8601 . , YYYY-MM-DD, 2016-10-22.


java.time

java.time Java 8 . legacy , java.util.Date, Calendar SimpleDateFormat.

Joda-Time, , java.time.

, . Oracle. Qaru . JSR 310.

java.time?

ThreeTen-Extra java.time . java.time. , Interval, YearWeek, YearQuarter .

+2

Although this is an old question.
I would like to share a simple solution.
You can check if the current date is NOT until the next date. Because if it is not earlier, it is either equal to the current date, or after the current date. And that is what you are looking for. Therefore, you do not need the "OR equals" check.

if(!current.isBefore(next)){
     System.out.println("The date is future day");
}else{
     System.out.println("The date is older than current day");
}

Alternatively you can use:

if(next.getTime() >= current.getTime())
0
source

All Articles