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??
TIME Type. The time data type. The format is yyyy- MM -dd hh:mm:ss, with both the date and time parts maintained. Mapped to java.
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, use PeriodFormatter
which you can build by PeriodFormatterBuilder
.
Here's a kickoff example:
DateTime myBirthDate = new DateTime(1978, 3, 26, 12, 35, 0, 0); DateTime now = new DateTime(); Period period = new Period(myBirthDate, now); PeriodFormatter formatter = new PeriodFormatterBuilder() .appendYears().appendSuffix(" year, ", " years, ") .appendMonths().appendSuffix(" month, ", " months, ") .appendWeeks().appendSuffix(" week, ", " weeks, ") .appendDays().appendSuffix(" day, ", " days, ") .appendHours().appendSuffix(" hour, ", " hours, ") .appendMinutes().appendSuffix(" minute, ", " minutes, ") .appendSeconds().appendSuffix(" second", " seconds") .printZeroNever() .toFormatter(); String elapsed = formatter.print(period); System.out.println(elapsed + " ago");
Much more clear and concise, isn't it?
This prints by now
32 years, 1 month, 1 week, 5 days, 6 hours, 56 minutes, 24 seconds ago
(Cough, old, cough)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With