查找最后的付款日期

时间:2012-01-04 19:06:38

标签: java date calendar

我已经写了这个简单的方法来给我每个支付期的日期,它已经从1月1日起停止工作。我真的不确定这里有什么问题。它没有返回任何东西。

   public static List<Date> getPayPeriodDatesSinceStartOfYear() {

        Calendar cal = new GregorianCalendar();
        cal.add(Calendar.WEEK_OF_YEAR, 1);

        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        cal.set(Calendar.WEEK_OF_YEAR, 2);

        Calendar currentDate = new GregorianCalendar();
        currentDate.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        int counter = 2;
        List<Date> dates = new ArrayList<Date>();
        while ((cal.getTime().compareTo((currentDate.getTime())) <= 0)) {
            dates.add(cal.getTime());
            counter += 2;
            cal.set(Calendar.WEEK_OF_YEAR, counter);
        }
        java.util.Collections.reverse(dates);
        if(dates.isEmpty()){
            JOptionPane.showMessageDialog(null, "NO DATES, SOMETHING IS WRONG!");
        }
        return dates;
    }

2 个答案:

答案 0 :(得分:3)

您的状态未得到满足,您设置cal的方式大于currentDate因此,while循环永远不会发生。

答案 1 :(得分:1)

Calendar cal = new GregorianCalendar();
cal.add(Calendar.WEEK_OF_YEAR, 1);

cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
cal.set(Calendar.WEEK_OF_YEAR, 2);

为什么你将周设置为1然后设置为2?

(cal.getTime().compareTo((currentDate.getTime())) <= 0)

请参阅Date的文档。它有before(Date when)after(Date when)等方法。