【问题标题】:Method to calculate the upcoming birthdays in next N days计算未来 N 天即将到来的生日的方法
【发布时间】:2014-11-04 05:12:39
【问题描述】:

我需要一个方法,它接受一个整数输入 (N) 并返回接下来 N 天的生日。我发现让任何代码运行都非常困难。下面只是我希望它如何工作的代码 - 它绝不是工作代码。非常感谢任何帮助。

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

}

【问题讨论】:

  • 第一个停靠港 - 如果可以,请放弃 java.util.Datejava.util.Calendar。您可以使用 Java 8 的 java.time 包或 Joda Time 吗?
  • birthdayList - 它包含字符串还是日期?
  • 使用 Java Instant 它有 isBefore 和 isAfter 方法来封装你的日期。
  • 列表包含 Date 对象。
  • @Nikhil 如果列表包含 java.util.Date 对象,请更正您的问题。您的问题说列表对象的格式为MM/DD/YYYY,但 j.u.Date 对象没有格式。表示 j.u.Date 值的字符串可以有这样的格式。

标签: java java.util.date java.util.calendar


【解决方案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);

在哪里

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

现在将此日期列表与您的生日日期列表进行比较并相应地工作

【讨论】:

    【解决方案2】:

    搜索 StackOverflow

    简短的回答,因为这种工作已经在 StackOverflow 上解决了数百甚至数千次。请搜索 StackOverflow 以获取更多信息。搜索“joda”和“half-open”,也许还有“immutable”。显然搜索下面示例代码中看到的类和方法名称。

    避免使用 java.util.Date 和 .Calendar

    避免与 Java 捆绑在一起的 java.util.Date 和 .Calendar 类。他们是出了名的麻烦。使用 Joda-Time 或 Java 8 中的新 java.time 包。

    乔达时间

    假设您的列表包含 java.util.Date 对象,请将它们转换为 Joda-Time DateTime 对象。

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

    【讨论】:

      猜你喜欢
      • 2011-04-14
      • 1970-01-01
      • 1970-01-01
      • 2011-02-11
      • 2015-05-21
      • 1970-01-01
      • 1970-01-01
      • 2010-09-10
      • 2014-05-03
      相关资源
      最近更新 更多