1

I'm trying to iterate through dates without success as an inifite loop happens and it won't stop creating records in the database. Why?

I have the following method which increments by one the date passed as a parameter and then return it updated.

public static Date addDays(Date date, int days) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.add(Calendar.DATE, days);
    return cal.getTime();
}

And then I use it in a for-loop this way...

for (Date initDate = fromDate; initDate.before(toDate); addDays(initDate, 1)) {
    // Do something...
}

fromDate and toDate are values from a JTextField parsed into sql.Date this way.

private java.sql.Date fechaParser(String f) {
    SimpleDateFormat formatDate = new SimpleDateFormat("dd/MM/yyyy");
    Date date;

    try {
        date = formatDate.parse(f);
        java.sql.Date sqlDate = new java.sql.Date(date.getTime());

        return sqlDate;
    } catch (ParseException e) {
        JOptionPane.showMessageDialog(null, "Wrong date format.");
        e.printStackTrace();
    }

    return null;
}
1
  • 1
    initDate = addDays(initDate, 1)?? Commented Nov 30, 2015 at 23:02

2 Answers 2

7

Your addDays() function returns a new date instead of changing the argument one.

Thus you need to change the increment part of your for-loop from this:

for ( ...; addDays(initDate, 1)) { ...

to this:

for ( ...; initDate = addDays(initDate, 1)) { ...
Sign up to request clarification or add additional context in comments.

Comments

2

The accepted Answer is correct.

As of Java 8 and later, the java.util.Date/.Calendar classes are now outmoded.

java.time

The java.time framework built into Java 8 and later supplants the troublesome old java.util.Date/.Calendar classes. The new classes are inspired by the highly successful Joda-Time framework, intended as its successor, similar in concept but re-architected. Defined by JSR 310. Extended by the ThreeTen-Extra project. See the Tutorial.

First we define a formatter using a pattern to match our expected input String.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");

The java.time framework includes a class to represent a date-only value, without time-of-day: LocalDate. A static method on that class can parse.

String input = "05/07/2013";
LocalDate localDate_Start = LocalDate.parse( input , formatter );

If the user gives us bad text input, the parse method throws a DateTimeParseException. So we should catch and handle that exception.

try{ 
    LocalDate localDate_Start = LocalDate.parse( input , formatter );
} catch ( DateTimeParseException e ) {
    // … handle the exception …
}

The LocalDate class includes a plusDays method to add days.

LocalDate localDate_Stop = localDate_Start.plusDays( someNumberOfDays );

By the way, in date-time work we commonly use the Half-Open approach where the beginning is inclusive while the ending is exclusive. For example a week would run from a Monday up to, but not including, the following Monday.

A while loop makes more sense to me than your use of a for loop

To do some database work we must convert from java.time.LocalDate to java.sql.Date. Someday JDBC drivers will be updated to directly handle java.time types but until then we must covert. Look for newly added conversion methods such as java.sql.Date.valueOf( LocalDate localDate ).

LocalDate localDate = localDate_Start;
while( localDate.isBefore( localDate_Stop ) ) {
    // Do some database work.
    // Until JDBC drivers updated to handle java.time types, convert to java.sql type.
    java.sql.Date sqlDate = java.sql.Date.valueOf( localDate );
    // … do you database work.

    // Prepare for next iteration of this 'while' loop.
    localDate = localDate.plusDays( 1 );
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.