tom
tom

Reputation: 5504

Comparing dates of type String in Java minus few days

I was trying to compare two dates which are of type String(After applying SimpleDateFormat) now i have a to apply logic like to check the date which i am receiving is 7 days less the today's date.

The format of both the dates are yyyy-mm-dd.

Upvotes: 0

Views: 969

Answers (4)

Basil Bourque
Basil Bourque

Reputation: 339043

Joda-Time

The Joda-Time 2.3 library makes this kind of date-time calculation much easier. Avoid using the bundled java.util.Date/Calendar classes.

In this case, Joda-Time offers a handy minusDays() method along with comparison methods isBefore and isAfter.

Parsing

That string format happens to be standard ISO 8601 format. Joda-Time conveniently takes such standard strings as arguments to the constructors of its date-time instances. So no need to define a formatter.

If using other non-standard formats, Joda-Time has a variety of ways to specify a format to use in parsing the string. Search on "joda format" for many examples here in StackOverflow.com.

Time Of Day

If you truly care about date only without any time-of-day, use the LocalDate class. If you are concerned with time, such as caring about when day starts in certain time zones, then use DateTime class and call withTimeAtStartOfDay() method.

Example Code

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;

String string = "2011-02-03";
LocalDate localDate = new LocalDate( string );
LocalDate sevenDaysAgo = LocalDate.now().minusDays( 7 );
boolean isThatDateMoreThanSevenDaysAgo = localDate.isBefore( sevenDaysAgo );

Dump to console…

System.out.println( "localDate: " + localDate );
System.out.println( "sevenDaysAgo: " + sevenDaysAgo );
System.out.println( "isThatDateMoreThanSevenDaysAgo: " + isThatDateMoreThanSevenDaysAgo );

When run…

localDate: 2011-02-03
sevenDaysAgo: 2013-12-26
isThatDateMoreThanSevenDaysAgo: true

Java 8

In Java 8 you may either continue to use Joda-Time or switch to the new bundled java.time.* classes defined by JSR 310. Those new classes were inspired by Joda-Time but are entirely re-architected.

Upvotes: 3

Kevin Bowersox
Kevin Bowersox

Reputation: 94489

public class DateExample {

    public static void main(String[] args) {
        System.out.println(isWithin("2014-01-01", -7));
        System.out.println(isWithin("2014-01-03", -7));
        System.out.println(isWithin("2013-12-27", -7));
    }

    public static boolean isWithin(String date, int days){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date dateProvided = null;
        try {
            dateProvided = format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        } 

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, days);
        calendar.set(Calendar.HOUR, 12);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date comparisonDate = calendar.getTime();

        return comparisonDate.equals(dateProvided);
    }
} 

Upvotes: 1

Meno Hochschild
Meno Hochschild

Reputation: 44061

Working with j.u.Date is ugly because you are interested in just the plain date, but this class is a kind of global timestamp. At the moment I would recommend JodaTime-class org.joda.time.LocalDate for this task.

Upvotes: 1

claj
claj

Reputation: 5402

You should do the comparisation on the dates before doing any conversion to strings.

Checkout the many questions already posted here.

Upvotes: 1

Related Questions