0

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.

Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
tom
  • 5,114
  • 6
  • 24
  • 36
  • 2
    If you are creating the string form of the dates anyway, why not just compare the original date objects? – forgivenson Jan 02 '14 at 21:14
  • You don't have to beg for help here. Can you provide something you tried or some details like code. – Furquan Khan Jan 02 '14 at 21:15
  • 1
    Take a look at [this](http://stackoverflow.com/questions/12851934/how-to-find-difference-between-two-joda-datetime-in-minutes/12852021#12852021) for an example of calculating the difference between dates and [this](http://stackoverflow.com/questions/17559772/string-to-joda-localdate-in-format-of-dd-mmm-yy/17560962#17560962) for converting a String to a DateTime object all using JodaTime ;) – MadProgrammer Jan 02 '14 at 21:20
  • Do you want it exactly 7 days less or within 7 days? – Kevin Bowersox Jan 02 '14 at 21:27
  • @KevinBowersox - exactly less then seven days – tom Jan 02 '14 at 21:31
  • This question needs editing for clarity. – Basil Bourque Jan 02 '14 at 22:45

4 Answers4

3

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

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

Checkout the many questions already posted here.

Community
  • 1
  • 1
claj
  • 5,172
  • 2
  • 27
  • 30
  • the date which iam receving is not having anytime stamp, i need to convert system date to the format which i receing in that process it is converting into String – tom Jan 02 '14 at 21:18
  • Date date = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); – tom Jan 02 '14 at 21:19
  • But while comparing sys date, it will be having some time stamp, Like some value, so while comapring it will be false? – tom Jan 02 '14 at 21:19
1

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.

Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
1
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);
    }
} 
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189