Skip to content Skip to sidebar Skip to footer

How To Compare Current Day & Month With Given Day & Month In Java

I want to compare current date with the given date. Current date format will be (dd-MM-YYYY). I don't want to get the current year. Suppose today's date is 30-08-2016. I want to ge

Solution 1:

java.time.MonthDay

Use modern date-time classes in the ThreeTenABP project, an Android adaptation of the back-port of the java-time classes built into Java 8 and later. Avoid the troublesome old legacy date-time classes.

These java.time classes include the MonthDay class to represent a month and a day-of-month without a year.

MonthDaymd= MonthDay.of( 8 , 15 );

This class already implements the Comparable interface and its compareTo method. Also implements methods equals, isBefore, and isAfter. So, job done.

Determining the current MonthDay requires a time zone. For any given moment the date varies by time zone.

ZoneIdz= ZoneId.of( "America/Montreal" );
MonthDaytoday= MonthDay.now( z );

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

Solution 2:

Have you tried to use the same year in the dates you want to compare?

Just using Calendar, set the year to be the same in the two Date objects and after that compare them...

(Take care with the leap-years)

Solution 3:

function to check dob( with current date)
/** dob should be in the format yyyy-MM-dd hh:mm:ss */publicstaticbooleanisTodayBdy(String dob) {
    Date today = newDate();
    try {
        if(dob.length() > 1){
            Date dobDate = newSimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(dob);
            if ((today.getMonth() == dobDate.getMonth()) && (today.getDate() == dobDate.getDate())) {
                returntrue;
            }
        }
    } catch (ParseException e) {
        returnfalse;
    }
    returnfalse;
}

Solution 4:

If you want to ignore the year, forget about using the date concept itself.

String given_date = "15-08";

String[] tokens = given_date.split("-");

int given_day = Integer.parseInt(tokens[0]);
int given_month = Integer.parseInt(tokens[1]);

Calendar c = Calendar.getInstance();

int now_month = c.get(Calendar.MONTH) + 1;
int now_day = c.get(Calendar.DATE);

System.out.println(given_day + " " + given_month + " " + now_day + " " + now_month);

if (now_month > given_month) {
    System.out.println("Before Today!");
} elseif (now_month < given_month) {
    System.out.println("After Today!");
} else {
    if (now_day > given_day) {
        System.out.println("Before Today!");
    } elseif (now_day < given_day) {
        System.out.println("After Today!");
    } else {
        System.out.println("Today!");
    }
}

Post a Comment for "How To Compare Current Day & Month With Given Day & Month In Java"