【问题标题】:How can I find the most recent month end date with Joda time?如何使用 Joda 时间找到最近的月末日期?
【发布时间】:2010-06-14 11:34:51
【问题描述】:

假设月末日期是一个月中的最后一个非周末和非节假日的日期。如何使用 Joda 时间找到最近的上个月结束日期?例如,今天的答案是 5 月 28 日星期五,因为那是 5 月的月底,而 5 月是最近的月底。

【问题讨论】:

标签: java datetime jodatime


【解决方案1】:

DateTime.dayOfMonth.getMaximumValue() 给出该月的最后一天。获取当天的工作日,查看是否是周末。

【讨论】:

    【解决方案2】:

    一般来说:

    • 查找当前月份
    • 回去一个月
    • 从该月的最后一天开始:
      • 如果既不是周末也不是节假日,那就是结束日期
      • 否则,请返回一天并重复

    也许是:

    • 从今天开始:
      • 如果两者都不是……

    这不是最有效的,但在计算后缓存该值很便宜,因为它会在整个月内有效。您可以轻松计算“所有”月末日期并将其存储在查找表中。你肯定想计算下一个是什么时候,因为那是当前缓存值过期的时候。


    示例

    这是我快速编造的一个 sn-p:

    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 只是实际实现的一个存根。我想真正的测试会相当复杂(假期部分),并且可能值得单独提出一个问题。

    【讨论】:

      【解决方案3】:

      根据您的问题和 cmets,您正在尝试解决这样的问题:

      1. 如果今天是工作日,则返回上个月的最后一个工作日
      2. 如果今天不是工作日,那么:
        1. 如果最近的工作日是当月的最后一天,则返回最近的工作日
        2. 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);
      }
      

      【讨论】:

        猜你喜欢
        • 2018-06-15
        • 2020-10-23
        • 2016-09-17
        • 1970-01-01
        • 1970-01-01
        • 2016-09-02
        • 1970-01-01
        • 2022-10-13
        • 1970-01-01
        相关资源
        最近更新 更多