如果每个月有30天计算年,月,日的差异

时间:2015-04-23 10:37:58

标签: java groovy

无法计算年,月,日两个日期之间的差异。问题是每个月有30天,每年有365天,没有例外。这里有很多主题,但我没有找到任何可以解决我的问题或帮助它。我找到的每个主题都只是解决了正常方式计算差异的问题,这很简单。 我该如何计算这种差异?

我正在使用Java 7(没有机会安装joda时间)和groovy 2.4.0。

例如:

date from    date to       y  m  d
2014-02-01 - 2015-02-01 => 1  0  1
2000-02-10 - 2003-01-18 => 2  11 9
2004-02-02 - 2004-06-24 => 0  4  23

感谢。

2 个答案:

答案 0 :(得分:3)

您的示例似乎不一致。在第一种情况下,结束日期是独占的,另外两种是包含它的。例如2015-01-01到2015-01-01应该给0d还是1d? 假设独家,看起来你需要的就是这样: 鉴于fromDate为yearFrom,monthFrom和daysFrom,toDate为yearTo,monthTo和daysTo:

days = -yearFrom*365-monthFrom*30-daysFrom+yearTo*365+monthTo*30+daysTo
假设365d年和30d月,

会给你几天的差异。 然后,差异将是:

yearDifference = days/365 (as integer)
monthDifference = (days%365)/30
daysDifference = (days%365)%30

评论后修改: 如果你需要它是包容性的,按照最后两个例子,只需在“天”中添加一个:

days = -yearFrom*365-monthFrom*30-daysFrom+yearTo*365+monthTo*30+daysTo+1

答案 1 :(得分:1)

在Groovy中提出这个问题,不确定它是否符合您的要求,但它会在结果中提出问题:

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div id="demoContainer" style="outline: solid 3px red; padding: 10px">
    <div id="htmlDiv">
      I want this outer div to shrink-wrap the 4 inner divs regardless of width... <br>
      <div class="container">
        <div class="select-bubbles">
          <select class="selector">
            <option value="select">select</option>
            <option value="selectAll">selectAll</option>
          </select>
          <select id="multi-single">
            <option value="4">multi</option>
            <option value="1">single</option>
          </select>
          <div class="select-subBubbles">
            <select class="selector">
              <option value="select">select</option>
              <option value="selectAll" selected="selected">selectAll</option>
            </select>
            <span class="select-spans">
              <select class="selector">
                <option value="select">select</option>
                <option value="selectAll">selectAll</option>
              </select>
            </span>
          </div>
        </div>
      </div> <br>
      <div class="bubbles bubble1">
        bubble1
        <div class="subBubbles bubble1">
          <span>sub1</span>
          <span class="spans bubble1">
            <span style="transform: rotateX(0deg); color: rgb(0, 0, 0);" class="temp">
              sub1
            </span> subsub1
          </span>
          <span class="spans bubble2">subsub2</span>
        </div>
        <div class="subBubbles bubble2">
          <span>
            sub2
          </span>
          <span class="spans bubble1">
            <span class="temp">
              sub2
            </span>
            subsub1
          </span>
        </div>
      </div>
      <div class="bubbles bubble2">
        bubble2
        <div class="subBubbles bubble1">
          sub1
          <span class="spans bubble1">subsub1</span>
          <span class="spans bubble2">subsub2</span>
        </div>
        <div class="subBubbles bubble2">
          sub2
          <span class="spans bubble1">subsub1</span>
          <span class="spans bubble2">subsub2</span>
        </div><div class="subBubbles bubble3">
          sub3
          <span class="spans bubble1">
            subsub1
          </span>
          <span class="spans bubble2">
            subsub2
          </span>
        </div>
      </div>
      <div class="bubbles bubble2">
        bubble2
        <div class="subBubbles bubble1">
          sub1
          <span class="spans bubble1">subsub1</span>
          <span class="spans bubble2">subsub2</span>
        </div>
        <div class="subBubbles bubble2">
          sub2
          <span class="spans bubble1">subsub1</span>
          <span class="spans bubble2">subsub2</span>
        </div><div class="subBubbles bubble3">
          sub3
          <span class="spans bubble1">
            subsub1
          </span>
          <span class="spans bubble2">
            subsub2
          </span>
        </div>
      </div>
    </div>
  </div>