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??
I'm not an expert in Java, but you can do t1-t2=t3(in seconds) then divide that by 60, would give you minutes, by another 60 would give you seconds. Then it's just a matter of figuring out how many divisions you need.
Hope it helps.
Since everyone shouts "YOODAA!!!" but noone posts a concrete example, here's my contribution.
You could also do this with Joda-Time. Use
Period
to represent a period. To format the period in the desired human representation, usePeriodFormatter
which you can build byPeriodFormatterBuilder
.Here's a kickoff example:
Much more clear and concise, isn't it?
This prints by now
(Cough, old, cough)
Avoid legacy date-time classes
The other Answers may be correct but are outdated. The troublesome old date-time classes in Java are now legacy, supplanted by the java.time classes.
Likewise, the Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
Using java.time
Instant
In java.time, we represent moments on the timeline in UTC as
Instant
. I will assume that your epoch is the same as that of java.time, the first moment of 1970 in UTC (1970-01-01T00:00:00Z
). Note that anInstant
has a resolution of nanoseconds. A convenience factory method constructs aInstant
from whole seconds, as asked in the Question.Here we use the current moment and some minutes later.
Duration
In java.time, a span of time unattached to the timeline is represented in two ways. For years-month-days, we have
Period
. For hours-minutes-seconds, we haveDuration
.Half-Open
The elapsed time is calculated with the Half-Open approach. In this approach, the beginning is inclusive while the ending is exclusive. This approach is commonly used in date-time work. I believe using this approach consistently across your code base will help to eliminate errors and misunderstandings due to ambiguities, and will ease the cognitive load of your programming.
ISO 8601
The ISO 8601 standard defines many practical formats for representing date-time values as text. These formats are designed to avoid ambiguity, be easy to parse by machine, and be intuitive to read by humans across cultures.
The java.time classes use these formats by default when parsing & generating strings.
For a span of time unattached to the timeline, the standard defines a format of
PnYnMnDTnHnMnS
whereP
marks the beginning andT
separates any years-months-days from any hours-minutes-seconds. So an hour and a half isPT1H30M
.In our example code using seven minutes:
See the above example code running live at IdeOne.com.
I suggest sticking with these formats whenever possible, certainly when exchanging or serializing date-time data, but also consider using in a user interface where appropriate and your users can be trained to use them.
I recommend never using the clock-hour format (ex: 01:30 for hour-and-a-half) as that format is completely ambiguous with a time-of-day.
Retrieving parts to build a String
If you must spell out the span of time as seen in the Question, you will need to build up the text yourself.
Period
, callgetYears
,getMonths
, andgetDays
to retrieve each part.Duration
class lacked such getters in Java 8, but gained them in Java 9 and later:toDaysPart
,toHoursPart
,toMinutesPart
,toSecondsPart
, andtoNanosPart
.Some example code, simple and basic to get you started.
Or perhaps you could write slicker code using
DateTimeFormatterBuilder
in a manner shown with Joda-Time in the Answer by Balus C.About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
,Calendar
, &SimpleDateFormat
.The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as
Interval
,YearWeek
,YearQuarter
, andfz more.The Calendar class can handle most date related math. You will have to get the result of compareTo and output the format yourself though. There isn't a standard library that does exactly what you're looking for, though there might be a 3rd party library that does.
OK, after a brief peruse of the API it seems that you could do the following: -
HTH
The solution of "Abduliam Rehmanius" seems pretty nice, or you can use some above library or you can create new simple things, refer the JS solution at here: http://www.datejs.com/. It's not difficult to transform into Java lang :-)
Hope my link useful for you!