How to add days to java.sql.date?

前端 未结 5 2640
猫巷女王i
猫巷女王i 2021-02-20 07:16

Here is my program, I tried

java.sql.Date logicalDate;
Calendar c = Calendar.getInstance(); 
c.setTime(logicalDate); 
c.add(Calendar.DATE, 1);

5条回答
  •  一生所求
    2021-02-20 07:34

    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());
        }
    
    }
    

提交回复
热议问题