These issues have been discussed many times already on Stack Overflow. Yours is a duplicate of many other Questions. So search for details. Search for the class names seen below and for words such as elapsed
.
Here is a brief nutshell answer.
ISO 8601
Convert your input string from SQL format to standard ISO 8601 format by replacing the SPACE in the middle with a T
.
String input = "2016-12-11 14:26:35".replace( " " , "T" );
LocalDateTime
Parse as a LocalDateTime
as this string lacks any indication of time zone or offset-from-UTC.
LocalDateTime ldt = LocalDateTime.parse( input );
ZonedDateTime
Apply the time zone intended as the meaning behind this string. Did you mean two in the afternoon of Auckland, Paris, or Montréal?
ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdtThen = ldt.atZone( z );
Get the current moment. Again, the time zone is crucial. Cannot be ignored or wished away.
ZonedDateTime zdtNow = ZonedDateTime.now( z );
Difference?
As for "get the difference", you do not explain what that means.
If you want to represent a span of time in between as whole days, use Period
.
Period p = Period.between( zdtThen.toLocalDate() , zdtNow.toLocalDate() );
If you want hour-minutes-seconds elapsed, use Duration
.
Duration d = Duration.between( zdtThen , zdtNow );
To track as a pair of points in time, obtain the ThreeTen-Extra library, and use Interval
class.
Interval interval = Interval.of( zdtThen.toInstant() , zdtNow.toInstant() );