0

I need a method which takes an integer input (N) and returns the birthdays in the next N days. I am finding it very difficult to get any code running. Below is just a code of how I want it to work - it is in no means a working code. Any help is highly appreciated.

/* print out all the birthdays in the next N days */
public void show( int N){
    Calendar cal = Calendar.getInstance();
    Date today = cal.getTime();

    // birthdayList is the list containing a list 
    // of birthdays Format: 12/10/1964 (MM/DD/YYYY)

    for(int i = 0; i<birthdayList.getSize(); i++){
        if(birthdayList[i].getTime()- today.getTime()/(1000 * 60 * 60 * 24) == N)){
            System.out.println(birthdayList[i]);
        }
    }

}
NanoNi
  • 315
  • 4
  • 16
  • 1
    First port of call - abandon `java.util.Date` and `java.util.Calendar` if you can. Can you use either Java 8's `java.time` package or Joda Time? – Jon Skeet Sep 10 '14 at 14:00
  • birthdayList - does it contain Strings or Dates? – peter.petrov Sep 10 '14 at 14:01
  • Use Java Instant it has isBefore and isAfter methods to encapsulate your date. – StackFlowed Sep 10 '14 at 14:09
  • The list contains Date objects. – NanoNi Sep 10 '14 at 14:12
  • @Nikhil If the list contains java.util.Date objects, please correct your Question. Your Question says the list objects have a format of `MM/DD/YYYY`, but j.u.Date objects have no format. Strings that represent the j.u.Date value can have such a format. – Basil Bourque Sep 10 '14 at 15:52

2 Answers2

1
Calendar calendar = Calendar.getInstance(Locale.ENGLISH);
calendar.setTime(new Date());
calendar.add(Calendar.DATE, n); // n is the number of days upto which to be calculated
Date futureDate = calendar.getTime();
List<String> listOfDates = returnListOfDatesBetweenTwoDates(new Date()
                                                                , futureDate);

where

public static List<String> returnListOfDatesBetweenTwoDates(java.util.Date fromDate,
                                                             java.util.Date toDate) {
    List<String> listOfDates = Lists.newArrayList();
    Calendar startCal = Calendar.getInstance(Locale.ENGLISH);
    startCal.setTime(fromDate);
    Calendar endCal = Calendar.getInstance(Locale.ENGLISH);
    endCal.setTime(toDate);
    while (startCal.getTimeInMillis() <= endCal.getTimeInMillis()){
        java.util.Date date = startCal.getTime();
        listOfDates.add(new SimpleDateFormat("dd-MM-yyyy"
                                               , Locale.ENGLISH).format(date).trim());
        startCal.add(Calendar.DATE, 1);
    }
    return listOfDates;
}

Now compare this list of Dates with your Birthday list of dates and work accordingly

SparkOn
  • 8,806
  • 4
  • 29
  • 34
1

Search StackOverflow

Brief answer as this kind of work has been addressed hundreds, if not thousands, of times on StackOverflow. Please search StackOverflow for more info. Search for "joda" and for "half-open", and maybe "immutable". And obviously search for the class and method names seen in example code below.

Avoid java.util.Date & .Calendar

Avoid java.util.Date and .Calendar classes bundled with Java. They are notoriously troublesome. Use either Joda-Time or the new java.time package in Java 8.

Joda-Time

Assuming your list contains java.util.Date objects, convert them to Joda-Time DateTime objects.

// birthDates is a list of java.util.Date objects.
DateTimeZone timeZone = DateTimeZone.forID( "America/Montreal" );
DateTime now = DateTime.now( timeZone );
Interval future = new Interval( now, now.plusDays( 90 ).withTimeAtStartOfDay() ); // Or perhaps .plusMonths( 3 ) depending on your business rules.
List<DateTime> list = new ArrayList<>();
for( java.util.Date date : birthDates ) {
    DateTime dateTime = new DateTime( date, timeZone ); // Convert from java.util.Date to Joda-Time DateTime.
    If( future.contains( dateTime ) ) {
        list.add( dateTime );
    }
}
Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154