/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
import java.time.*;
import java.time.format.*;
import java.time.temporal.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
{
String inputStart
= "2017-01-23 05:25:00".
replace ( " " ,
"T" ); String inputStop
= "2018-03-20 07:29:50".
replace ( " " ,
"T" );
LocalDateTime start = LocalDateTime.parse ( inputStart );
LocalDateTime stop = LocalDateTime.parse ( inputStop );
Period p = Period.between ( start.toLocalDate () , stop.toLocalDate () ).minusDays ( 1 ); // Subtract one, as we account for hours of first day.
// Get the Duration of first day's hours-minutes-seconds.
LocalDateTime startNextDay = start.toLocalDate ().plusDays ( 1 ).atStartOfDay ();
Duration dStart = Duration.between ( start , startNextDay );
// Get the Duration of first day's hours-minutes-seconds.
LocalDateTime stopStartOfDay = stop.toLocalDate ().atStartOfDay ();
Duration dStop = Duration.between ( stopStartOfDay , stop );
// Combine the pair of partial days into a single Duration.
Duration d = dStart.plus ( dStop );
System.
out.
println ( "start/stop: " + start
+ "/" + stop
); System.
out.
println ( "p: " + p
); System.
out.
println ( "dStart: " + dStart
+ " and dStop: " + dStop
); System.
out.
println ( "d: " + d
);
int years = p.getYears ();
int months = p.getMonths ();
int days = p.getDays ();
System.
out.
println( "Years: " + years
+ "\nMonths: " + months
+ "\nDays: " + days
);
// Enable if in Java 9 or later (but not in Java 8)
// long durationDays = d.toDaysPart();
// long hours = d.toHoursPart();
// long minutes = d.toMinutesPart();
// long seconds = d.toSecondsPart();
// int nanos = d.toNanosPart();
}
}