Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9108123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:44:16+00:00 2026-06-17T02:44:16+00:00

On running the following code in groovy – import groovy.time.* import org.codehaus.groovy.runtime.TimeCategory def today

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T02:44:18+00:00Added an answer on June 17, 2026 at 2:44 am

    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'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting unexpected output when running following code, DateFormat df = new SimpleDateFormat(YYYY-MM-dd); Date
I'm running the following code: import org.eclipse.core.resources.IWorkspace; import org.eclipse.core.resources.ResourcesPlugin; public class WorkspaceTest { public
Running the following code, I get an exception: using (var client = new Pop3Client())
I'm running the following code: new FileSystemXmlApplicationContext(/data/farm/Server/confData/1000004/contex.xml) and it throws java.io.FileNotFoundException: class path resource
Running the following code to add an association enters multiple entries each time the
I'm running following code: TagLib.File f = TagLib.File.Create(path); f.Tag.AlbumArtists = f.Tag.Artists = new string[]
Running the following code: import os import datetime import ftplib currdate = datetime.datetime.now() formatdate
while running following code : def v1=[]; def p=[]; v.as('x') .except(v1).sideEffect{v1.add(it)} // step 1
Running the following code in IRB: t = Thread.new { loop { puts 'Hi'
I am running following code: System.Management.ManagementClass wmiNetAdapterConfiguration = new System.Management.ManagementClass(Win32_NetworkAdapterConfiguration); System.Management.ManagementObjectCollection wmiNetAdapters = wmiNetAdapterConfiguration.GetInstances();

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.