How to add days to java.sql.date?
import java.sql.Date;
import java.time.LocalTime;
import java.util.Calendar;
import java.util.List;
import java.util.stream.Collectors;
public class Test {
public static void main(String a[]) {
java.sql.Date todaysDate = new java.sql.Date(new java.util.Date().getTime());
int futureDay =1;
int pastDay=2;
java.sql.Date futureDate = this.addDays(todaysDate, futureDay);
java.sql.Date pastDate = this.subtractDays(todaysDate, pastDay);
System.out.println("futureDate =>>> " + futureDate);
System.out.println("pastDate =>>> " + pastDate);
}
public static Date addDays(Date date, int days) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.DATE, days);
return new Date(c.getTimeInMillis());
}
public static Date subtractDays(Date date, int days) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.DATE, -days);
return new Date(c.getTimeInMillis());
}
}
Calendar#getTime
returns a java.util.Date
representation of the Calendar
. You really need to use Calendar#getTimeInMillis
instead
java.sql.Date startDate= new java.sql.Date(c.getTimeInMillis())