-1

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.

user2567005
  • 271
  • 1
  • 13
  • 27
  • 2
    Let you know what? How to do it or just the fact that they know? Be clear in what you are asking about. Also, show us where you're blocked. SO is not a code factory. – Sotirios Delimanolis Aug 12 '14 at 03:45
  • Start by doing a search for `[java] joda time dates between` and `[java] calendar dates between` and see what you come up with... – MadProgrammer Aug 12 '14 at 03:46
  • Sounds like a homework problem. What you've shown isn't even close to valid Java. At least make an effort to come up with a solution on your own before asking for help. – Jim Garrison Aug 12 '14 at 03:55

1 Answers1

2

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);
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thnks "MadProgrammer" u save my day. i was struggling more than 3 hours to find a answer to this problem u saved my day. thnks agin "MadProgrammer" – user2567005 Aug 12 '14 at 04:25