0

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

M9A
  • 3,168
  • 14
  • 51
  • 79
  • 1
    Yes converting them to `Date` can actually save you lots of time and work. –  Mar 04 '13 at 21:09
  • Why don't you keep the dates in a `Dates[]`?What's the point to convert back and forth? – Cratylus Mar 04 '13 at 21:19
  • @Cratylus - Because I need to use their String representation for another method – M9A Mar 04 '13 at 21:21
  • @Matt9Atkins then why not just convert the date to a string representation when you need it? – I82Much Mar 04 '13 at 21:22
  • @I82Much - Its not just that, they are also in string format because they were read directly from a text file – M9A Mar 04 '13 at 21:24
  • 1
    @Matt9Atkins:So what?If your core algorithm manipulates `Date` then convert them to `Date` as you read-in the text file and just use `toString()` or any string conversion only when you need a string.Don't complicate things when you don't have to – Cratylus Mar 04 '13 at 21:36
  • Did you mean to repeat Feb 18 twice? Is should that the 18th and the 19th? – Basil Bourque Jun 06 '17 at 01:45
  • There are more interesting answers with [this duplicate question](https://stackoverflow.com/questions/44372209/how-to-check-array-of-dates-are-consecutive-from-todays-date). – Ole V.V. Jun 06 '17 at 11:09

4 Answers4

6

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];
}
2

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.

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

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);  
    }  
}  
Guido
  • 926
  • 2
  • 10
  • 19
  • 4
    Or you could just see if after [adding](http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#add(int,%20int)) one day to `c1` you get `c2`. – millimoose Mar 04 '13 at 21:21
0
@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);
}
Mike Hogan
  • 9,933
  • 9
  • 41
  • 71