I want to take two times (in seconds since epoch) and show the difference between the two in formats like:
- 2 minutes
- 1 hour, 15 minutes
- 3 hours, 9 minutes
- 1 minute ago
- 1 hour, 2 minutes ago
How can I accomplish this??
I want to take two times (in seconds since epoch) and show the difference between the two in formats like:
How can I accomplish this??
Formatting time spans in java pops up for me frequently in ETL or DB pulls. The following code snippet is how I get the pulse of the operation.
This question is old and already has an answer but i have a better solution. Use relativeSpan from date utils
it takes arguments: long/int time, current time and how you want it, _month,minute,second etc
I'd recommend you have a look at HumanTime
I always start with Joda Time. Working with dates and times in Java is always "fun" but Joda Time takes the strain off.
They have Interval and Duration classes which do half of what you are looking for. Not sure if they have a function for outputing in readable format though. I'll keep looking.
HTH
To create a string like "2 Minutes" you should use DateFormatter/DateFormat. You can find more details on this in the the Java API spec (java.sun.com).