We help IT Professionals succeed at work.

Find difference between two times.

sukhoi35
sukhoi35 used Ask the Experts™
on
Hello Experts
Can I please know what would be the easiest way in JAVA to find the difference between two times in the following format yyyyMMddhhmmss? The two times I am trying to compare will not differ by more than a couple of minutes. Any suggestions  for a simple solution greatly appreciated :)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Analyst Assistant
Commented:
I don't know if it's the easiest way but something like this should work.
		String strdate1 = "20191113100103";
		String strdate2 = "20191113101403";
		
		DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuuMMddHHmmss" ) ;		
		
		LocalDateTime date1 =   LocalDateTime.parse( strdate1 , f );
		LocalDateTime date2 =   LocalDateTime.parse( strdate2 , f );
		
		long mins =Duration.between(date1,date2).getSeconds()/60;

		System.out.println(String.format("Between %s and %s there are %s minutes.", date1, date2, mins));

Open in new window

Author

Commented:
Thank you!