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

}
4

2 回答 2

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

于 2014-09-10T14:10:50.440 回答
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 );
    }
}
于 2014-09-10T16:15:16.693 回答