'Print the days of two given dates in groovy
class Example {
public static void main(String[] args) {
def date =new Date().format("MM/dd/yy");
Scanner scanner= new Scanner(System.in);
String date1= scanner.next();
String date2= scanner.next();
def duration =(date2-date1)
return (date2 -date1).days;
}
}
Solution 1:[1]
See the project at https://github.com/jeffbrown/rajesh-kushwaha-dates.
app/src/main/groovy/rajesh/kushwaha/dates/App.groovy
package rajesh.kushwaha.dates
class App {
static void main(String[] args) {
String format = 'MM/dd/yyyy'
Scanner scanner= new Scanner(System.in)
// Error handling ommited for brevity...
print "Enter First Date $format: "
Date date1= Date.parse(format, scanner.next())
print "Enter Second Date $format: "
Date date2= Date.parse(format, scanner.next())
def duration =(date2-date1)
println "Duration: $duration"
}
}
That appears to work:
~ $ git clone [email protected]:jeffbrown/rajesh-kushwaha-dates.git
Cloning into 'rajesh-kushwaha-dates'...
remote: Enumerating objects: 20, done.
remote: Counting objects: 100% (20/20), done.
remote: Compressing objects: 100% (12/12), done.
remote: Total 20 (delta 0), reused 20 (delta 0), pack-reused 0
Receiving objects: 100% (20/20), 59.52 KiB | 628.00 KiB/s, done.
~ $
~ $ cd rajesh-kushwaha-dates
rajesh-kushwaha-dates (main)$
rajesh-kushwaha-dates (main)$ ./gradlew run -q --console=plain
Enter First Date MM/dd/yyyy: 01/05/2022
Enter Second Date MM/dd/yyyy: 02/26/2022
Duration: 52
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
