M9A
M9A

Reputation: 3276

Java - Check if array contains 3 consecutive dates

Hi I have a String[] array that contains dates in the format YYYY/MM/DD. I want to iterate through this array and see if the next 2 elements in the array contain consecutive dates. If they do then just increase the count variable. Here is what I have so far. I just basically need help regarding the if statement that checks if there are 3 consecutive dates.

int count = 0;

String[] dates = { 
        "2004/1/23", "2004/1/24", "2004/1/25",
        "2004/1/26", "2004/1/29", "2004/2/11", 
        "2004/2/17", "2004/2/18", "2004/2/18", "2004/3/7"};

for(int i = 0; i < dates.length-2; i++){

    //Help needed here! If i, i+1 and i+2 are consecutive...
    if(...){
        count++;
    }
}

I realise that I might need to convert the String dates into an actual Date object before I can compare them. Further guidance would be appreciated. Thanks

Upvotes: 0

Views: 6843

Answers (4)

Basil Bourque
Basil Bourque

Reputation: 338496

tl;dr

Use java.time.LocalDate class.

if ( z.minusDays ( 1 ).isEqual ( y ) && z.minusDays ( 2 ).isEqual ( x ) )  …

Using java.time

The modern approach uses the java.time classes rather than the troublesome old legacy classes Date & Calendar.

First convert your array to a list of LocalDate objects. I changed your example data as I assume you meant the 18th and 19th of February rather than repeating the 18th twice.

String[] dates = {
        "2004/1/23" , "2004/1/24" , "2004/1/25" ,
        "2004/1/26" , "2004/1/29" , "2004/2/11" ,
        "2004/2/17" , "2004/2/18" , "2004/2/19" , "2004/3/7" };

DateTimeFormatter f = DateTimeFormatter.ofPattern ( "uuuu/M/d" );
List < LocalDate > localdates = new ArrayList <> ( dates.length );
for ( String input : dates ) {
    LocalDate ld = LocalDate.parse ( input, f );
    localdates.add ( ld );
}

Loop the LocalDate list, remembering the previous two items. If the current and the previous two are all sequential dates, then increment your counter.

Integer tripletCount = 0;
List< LocalDate > tripletDates = new ArrayList<>();
LocalDate x = null;
LocalDate y = null;
for ( LocalDate z : localdates ) {
    if ( null == x ) { x = z; continue; }
    if ( null == y ) { y = z; continue; }
    if ( z.minusDays ( 1 ).isEqual ( y ) && z.minusDays ( 2 ).isEqual ( x ) ) {
        tripletCount= ( tripletCount + 1 );
        tripletDates.add( z );
    }
    // Prepare for next loop.
    x = y ;
    y = z ;
}

Dump to console.

System.out.println ( "localdates: " + localdates );
System.out.println ( "tripletCount: " + tripletCount );
System.out.println ( "tripletDates: " + tripletDates );

localdates: [2004-01-23, 2004-01-24, 2004-01-25, 2004-01-26, 2004-01-29, 2004-02-11, 2004-02-17, 2004-02-18, 2004-02-19, 2004-03-07]

tripletCount: 3

tripletDates: [2004-01-25, 2004-01-26, 2004-02-19]


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?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Upvotes: 2

user2030471
user2030471

Reputation:

Convert String[] to Date[] (i.e., prepare a Date array). I presume you already know how to do this.

Now you can check for consecutive dates with something like this:

Calendar c = Calendar.getInstance();
int numConsecutive = 0;
Date last = null;

for (int i = 0; i < dates.length; i++) {
    c.setTime(dates[i]);
    c.add(Calendar.DATE, -1);
    if (c.getTime().equals(last)) {
        numConsecutive++;
    } else {
        numConsecutive = 0;
    }
    if (numConsecutive == 2) {
        numConsecutive = 0;
        count++;
    }
    last = dates[i];
}

Upvotes: 6

Mike Hogan
Mike Hogan

Reputation: 10603

@Test
public void threeConsecutiveDates() throws ParseException {
    List<Date> consecutive = new ArrayList<>();
    consecutive.add(new Date(0));
    final SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");

    String[] dates = {
            "2004/1/23", "2004/1/24", "2004/1/25",
            "2004/1/26", "2004/1/29", "2004/2/11",
            "2004/2/17", "2004/2/18", "2004/2/18", "2004/3/7"};
    for (String s : dates) {
        Date previous = consecutive.get(consecutive.size()-1);
        Date current = format.parse(s);
        if(previous.before(current) && (current.getTime()-previous.getTime() == 1000 * 60 * 60 * 24)) {
            consecutive.add(current);
        } else {
            consecutive.clear();
            consecutive.add(current);
        }
        if(consecutive.size() == 3) {
            break;
        }
    }
    System.out.println("consecutive = " + consecutive);
}

Upvotes: 0

Guido
Guido

Reputation: 958

Hi you will need to calculate the amount of seconds between two dates and then tranform into number of days:

import java.util.*;  
class DateDiff  
{  
    public static void main(String [] args)  
    {  
        Calendar c1=Calendar.getInstance();  
        c1.set(2011,5, 29 );  
        Calendar c2=Calendar.getInstance();  
        c2.set(2012,5,30);  

        Date d1=c1.getTime();  
        Date d2=c2.getTime();  

        long diff=d2.getTime()-d1.getTime();  
        int noofdays=(int)(diff/(1000*24*60*60));  
        System.out.println(noofdays);  
    }  
}  

Upvotes: 0

Related Questions