The modern approach uses the java.time classes that supplant the troublesome old legacy date-time classes.
Instant
Parse your input string. That string uses a format defined in the ISO 8601 standard. The java.time classes use these standard formats by default when parsing/generating strings. So no need to specify a formatting pattern.
Instant later = instant.now() ; // Capture the current moment in UTC.
Let’s use an moment five hour later.
Instant later = instant.plus( 5L , ChronoUnit.HOURS ) ;
Duration
Represent elapsed time of hours-minutes-seconds with Duration class.
Duration d = Duration.between( instant , later ) ;
In Java 9 and later, call to…Part to get each part of days, hours, minutes, seconds, nanoseconds. These methods were strangely missing in Java 8, but added in Java 9 and later.
JodaTime supports parsing from a user-defined format. See DateTimeFormatterBuilder and DateTimeBuilder.parseDateTime().
Once you have a DateTime, you can create a Duration or Period from that and the current time, and use another formatter to pretty-print. [See the PeriodFormatter example referenced by BalusC in comments above.]
tl;dr
java.time
The modern approach uses the java.time classes that supplant the troublesome old legacy date-time classes.
Instant
Parse your input string. That string uses a format defined in the ISO 8601 standard. The java.time classes use these standard formats by default when parsing/generating strings. So no need to specify a formatting pattern.
Get our later moment.
Let’s use an moment five hour later.
Duration
Represent elapsed time of hours-minutes-seconds with
Duration
class.In Java 9 and later, call
to…Part
to get each part of days, hours, minutes, seconds, nanoseconds. These methods were strangely missing in Java 8, but added in Java 9 and later.ISO 8601 duration
You may find the ISO 8601 compliant string for durations generated by
Duration::toString
to be useful:PnYnMnDTnHnMnS
The
P
marks the beginning. TheT
separates any years-months-days from any hours-minutes-seconds.So our example above for five hours would be:
Such strings can be parsed into
Duration
hours.I know a plugin in Jquery for this : http://plugins.jquery.com/project/CuteTime
For Java i assume you will need to use your brain :) ( You can translate it to Java )
JodaTime supports parsing from a user-defined format. See DateTimeFormatterBuilder and DateTimeBuilder.parseDateTime().
Once you have a DateTime, you can create a Duration or Period from that and the current time, and use another formatter to pretty-print. [See the PeriodFormatter example referenced by BalusC in comments above.]