Groovy:获得多年的持续时间 [英] Groovy: Get duration in years

查看:190
本文介绍了Groovy:获得多年的持续时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在groovy中运行以下代码 -

  import groovy.time。* 
import org.codehaus。 groovy.runtime.TimeCategory
def today = new Date()
use(TimeCategory)
{
def modifiedToday = today.plus(10.minutes)
modifiedToday = modifiedToday.plus(10.months)
modifiedToday = modifiedToday.plus(10.years)
def duration = modifiedToday - today
println duration.years
println duration.months
println duration.days
println duration.minutes
}

我是获得以下输出 -

  0 
0
3956
10

请建议,为什么我会将年和月数作为0和所有的天数值。

解决方案

如何在几个月内得到这个价值?



每个月有不同的天数,那么你会做什么?



你可以从现在开始,做:

  println duration.from.now 

或者,您可以通过以下方式获取过去代表的日期:

  println duration.ago 

我想你可以从那里工作,基于给定日期对TimeDuration进行归一化的内置功能






编辑



这种事情从过去的一个日期滚动到指定的日期。我还没有做任何真正的测试,所以你应该在使用它的任何重要的...之前应该关心和测试生活。

  import static java.util.Calendar。* 
import groovy.time.DatumDependentDuration
import groovy.time.TimeCategory

DatumDependentDuration getAge(Date dob, Date now = new Date()){
dob.clearTime()
now.clearTime()
assert dob<现在
Calendar.instance.with {c - >
c.time = dob
def(years,months,days)= [0,0,0]

while((c [YEAR]< now [YEAR] - 1)||
(c [YEAR]< now [YEAR]&& c [MONTH]< = now [MONTH])){
c.add(YEAR,1)
年++
}

while((c [YEAR]< now [YEAR])||
(c [MONTH]< now [MONTH] ;& c [DAY_OF_MONTH]< = now [DAY_OF_MONTH])){
//当我们正在包装DEC / JAN边框时收到,并且会超过现在
if(c [YEAR] = =现在[YEAR] - 1&
现在[MONTH] == JANUARY&& c [MONTH] == DECEMBER&&
c [DAY_OF_MONTH]> now [DAY_OF_MONTH] ){
break
}
c.add(MONTH,1)
months ++
}

while(c [DAY_OF_YEAR]!=现在[DAY_OF_YEAR]){
c.add(DAY_OF_YEAR,1)
days ++
}

new DatumDependentDuration(y耳朵,月,日,0,0,0,0)
}
}

println getAge(Date.parse('dd / MM / yyyy',' 10/2000'))

//打印:'12年,2个月,30天'


On running the following code in groovy -

import groovy.time.*
import org.codehaus.groovy.runtime.TimeCategory
def today = new Date()
use(TimeCategory)
{
  def modifiedToday = today.plus(10.minutes)
  modifiedToday = modifiedToday.plus(10.months)
  modifiedToday = modifiedToday.plus(10.years)
  def duration = modifiedToday - today
  println duration.years
  println duration.months
  println duration.days
  println duration.minutes
}

I am getting the following output -

0
0
3956
10

Please suggest, why am I getting years and months as 0 and all the value in days. How do I get the value in years and months?

解决方案

How would you get it in months?

Each month has a different number of days, so what would you do?

You can get back the date this represents from now by doing:

println duration.from.now

Or, you can get the date that represents in the past by doing:

println duration.ago

And I guess you could work it out from there, but there is no in-built functionality for normalising a TimeDuration based on a given date


Edit

This sort of thing rolls from one date in the past to the specified date. I haven't done any real testing on it though, so you should take care and test the life out of it before using it in anything important...

import static java.util.Calendar.*
import groovy.time.DatumDependentDuration
import groovy.time.TimeCategory

DatumDependentDuration getAge( Date dob, Date now = new Date() ) {
  dob.clearTime()
  now.clearTime()
  assert dob < now
  Calendar.instance.with { c ->
    c.time = dob
    def (years, months, days) = [ 0, 0, 0 ]

    while( ( c[ YEAR ] < now[ YEAR ] - 1 ) || 
           ( c[ YEAR ] < now[ YEAR ] && c[ MONTH ] <= now[ MONTH ] ) ) {
      c.add( YEAR, 1 )
      years++
    }

    while( ( c[ YEAR ] < now[ YEAR ] ) ||
           ( c[ MONTH ] < now[ MONTH ] && c[ DAY_OF_MONTH ] <= now[ DAY_OF_MONTH ] ) ) {
      // Catch when we are wrapping the DEC/JAN border and would end up beyond now
      if( c[ YEAR ] == now[ YEAR ] - 1 &&
          now[ MONTH ] == JANUARY && c[ MONTH ] == DECEMBER &&
          c[ DAY_OF_MONTH ] > now[ DAY_OF_MONTH ] ) {
        break
      }
      c.add( MONTH, 1 )
      months++
    }

    while( c[ DAY_OF_YEAR ] != now[ DAY_OF_YEAR ] ) {
      c.add( DAY_OF_YEAR, 1 )
      days++
    }

    new DatumDependentDuration( years, months, days, 0, 0, 0, 0 )
  }
}

println getAge( Date.parse( 'dd/MM/yyyy', '11/10/2000' ) )

// Prints: '12 years, 2 months, 30 days'

这篇关于Groovy:获得多年的持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆