Add 1 Week to a Date, which way is preferred?

前端 未结 10 1778
迷失自我
迷失自我 2021-01-07 17:30

I am reviewing some code at work and came across an inconsistency in how the code handles adding 1 week to the current time and was wondering if there was any reason why one

10条回答
  •  醉话见心
    2021-01-07 18:01

    The below can be done in java 8, Java 8 rocks !!

    public static void main(String[] args) {
            LocalDate today = LocalDate.now();
            System.out.println("Current date: " + today);
    
            //add 1 week to the current date
            LocalDate nextWeek = today.plus(1, ChronoUnit.WEEKS);
            System.out.println("Next week: " + nextWeek);
        }
    

提交回复
热议问题