start date and end date between two dates

前端 未结 1 1543
长发绾君心
长发绾君心 2020-12-12 08:22

hi i need to get all the start dates and end date of each and every month in between given two years

pulbic void printStartDateAndEndDate(Date start, Date e         


        
1条回答
  •  -上瘾入骨i
    2020-12-12 08:48

    Using JodaTime...

    LocalDate startDate = new LocalDate(2011, 11, 8);
    LocalDate endDate = new LocalDate(2012, 5, 1);
    
    startDate = startDate.withDayOfMonth(1);
    
    while (!startDate.isAfter(endDate)) {
        System.out.println("> " + startDate);
        startDate = startDate.plusMonths(1);
        LocalDate endOfMonth = startDate.minusDays(1);
        System.out.println("< " + endOfMonth);
    }
    

    Using Java 8's time API

    LocalDate startDate = LocalDate.of(2011, 11, 8);
    LocalDate endDate = LocalDate.of(2012, 5, 1);
    
    startDate = startDate.withDayOfMonth(1);
    
    while (!startDate.isAfter(endDate)) {
        System.out.println("> " + startDate);
        startDate = startDate.plusMonths(1);
        LocalDate endOfMonth = startDate.minusDays(1);
        System.out.println("< " + endOfMonth);
    }
    

    0 讨论(0)
提交回复
热议问题