Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Question is:

Define two date variables and assign the dates as 10/19/18 and 11/20/18 (format mm/dd/yy), and print the number of days between the dates.


class Example{
      static void main(String[] args){
       String date = "10/19/18"
        Date d1=Date.parse('MM/dd/yy',date)
       //complete the code

      }
}

please, anyone, help me to solve this, and i bit confused with the Date.parse() method. because it takes an only a single parameter, but in the question, it has given two parameters. so kindly explain how to do it.

What I have tried:

I have tried, but I was not getting the logic of differentiating between of given two dates of the days to display
Posted
Updated 27-Jan-22 1:54am

Reference: Calculate days between two dates | Level Up Lunch[^]
Java
//Difference in days
@Test
public void difference_between_date_in_days() {

    def today = new Date()
    def yesterday = today - 1

    assert 1 == today.minus(yesterday)
    assert 1 == today - yesterday
}


Another way[^], using TimeCategory[^] for getting a duration:
JavaScript
use(groovy.time.TimeCategory) {
   def duration = date1 - date2
   println "days: ${duration.days}, Hours: ${duration.hours}"
}
 
Share this answer
 
Comments
Maciej Los 2-Sep-20 6:06am    
5ed!
Sandeep Mewara 2-Sep-20 6:16am    
Thanks Maciej
I am using TimeCategory class to get the time difference in my groovy script but it throws error,

groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.use() is applicable for argument types: (java.lang.Class, Script17$_run_closure2) values: [class groovy.time.TimeCategory, Script17$_run_closure2@362b384c]\nPossible solutions: use(java.lang.Class, groovy.lang.Closure), use(java.util.List, groovy.lang.Closure), use([Ljava.lang.Object;), dump(), grep(), any()"}

 

This is my snapshot of script below,

use(TimeCategory) {
def duration = StartDate - CurrentDate
// logger.info("RECEIVED VALUES : days = {}, .hours = {}, minutes = {}, seconds = {}", ${duration.days}, ${duration.hours}, ${duration.minutes},${duration.seconds})
def DaystoHours = (duration.days*24)
def DaystoHourstoMins = (DaystoHours*60)

def HourstoMins = (duration.hours*60)

def SecstoMins = (duration.seconds/60)

def TotalMins = DaystoHourstoMins+HourstoMins+duration.minutes+SecstoMins

WAIT_PERIOD_2 = "PT"+TotalMins+"M"
}
 
Share this answer
 
Comments
CHill60 27-Jan-22 8:20am    
If you have a question then use the red "Ask a Question" link at the top of this page. Don't post questions or comments as a solution to another member's post

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900