簡體   English   中英

兩個日期之間的開始日期和結束日期

[英]start date and end date between two dates

您好,我需要獲取給定兩年之間每個月的所有開始日期和結束日期

pulbic void printStartDateAndEndDate(Date start, Date end){

    for(// start - end){
      Sysout("1 st month starting date: "+ startDateOfMonth+ " End Date"+endDateOfMonth);    
    }

}

如果有人知道該怎么做,請告訴我。我需要“ startDateOfMonth”和“ endDateOfMonth”位於Date對象中。

使用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);
}

使用Java 8的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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM