假设月底日期是一个月中的日期,即该月最后一个非周末和非节假日。如何使用 Joda 时间找到最近的上个月结束日期?例如,今天的答案是 5 月 28 日星期五,因为那是 5 月的月底,而 5 月是最近的月底。
问问题
3971 次
3 回答
5
DateTime.dayOfMonth.getMaximumValue() 给出该月的最后一天。获取当天的工作日以检查是否在周末。
于 2010-06-14T11:39:55.947 回答
3
通常来说,一般来说:
- 查找当前月份
- 回去一个月
- 从该月的最后一天开始:
- 如果既不是周末也不是节假日,那就是结束日期
- 否则,返回一天并重复
或者也许是:
- 从今天开始:
- 如果两者都不是...
这不是最有效的,但在计算后缓存值很便宜,因为它会在整个月内有效。您可以轻松计算“所有”月末日期并将其存储在查找表中。你肯定想计算下一个是什么时候,因为那是当前缓存值过期的时候。
例子
这是我快速编造的一个片段:
import org.joda.time.*;
public class LastMonthEnd {
static int count = 0;
static boolean isWeekendOrHoliday(DateTime d) {
return (count++ < 5);
}
public static void main(String[] args) {
DateTime d = new DateTime().minusMonths(1).dayOfMonth().withMaximumValue();
while (isWeekendOrHoliday(d)) {
d = d.minusDays(1);
}
System.out.println(d);
}
}
今天(2010-06-14 12:04:57Z
),这打印"2010-05-26T12:00:42.702Z"
。请注意,这isWeekendOrHoliday
只是实际实现的一个存根。我想真正的测试会相当复杂(假期部分),并且可能值得单独提出一个问题。
于 2010-06-14T11:43:04.333 回答
0
根据您的问题和评论,您正在尝试解决这样的问题:
- 如果今天是工作日,则返回上个月的最后一个工作日
- 如果今天不是工作日,那么:
- 如果最近的工作日是当月的最后一天,则返回最近的工作日
- else 返回上个月的最后一个工作日
这是财务调度库 Lamma ( http://lamma.io ) 的实现。该方法Date.backward(Calendar)
用于查找最近的工作日。
如果你真的想在 Joda 中实现,那么你基本上需要自己实现 Date.pastMonthEnd() 和 Date.backward(Calendar) 。
public static void main(String [] args) {
// print 2014-05-30, because 2014-05-31 is holiday
System.out.println(findDate(new Date(2014, 5, 31)));
// print 2014-04-30
System.out.println(findDate(new Date(2014, 5, 30)));
// print 2014-04-30, because the most recent working day of 2014-05-25 is not the last working day of May
System.out.println(findDate(new Date(2014, 5, 25)));
}
private static HolidayRule cal = weekends(); // let's use weekend calendar for now
public static Date findDate(Date current) {
if (cal.isHoliday(current)) {
Date lastWorkingDayOfThisMonth = current.lastDayOfMonth().backward(cal);
Date mostRecentWorkingDay = current.backward(cal);
if (lastWorkingDayOfThisMonth.equals(mostRecentWorkingDay)) {
return mostRecentWorkingDay;
} else {
return lastWorkingDayOfLastMonth(current);
}
} else {
return lastWorkingDayOfLastMonth(current);
}
}
public static Date lastWorkingDayOfLastMonth(Date d) {
return d.lastDayOfPreviousMonth().backward(cal);
}
于 2014-06-09T23:33:06.477 回答