Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 end){

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

}

if somebody knows how to do this please let me know.i need the "startDateOfMonth" and "endDateOfMonth" to be in Date object.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
175 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...