-1

I'm having a Java Date and I want to convert this to something like

0s ago or 38m ago.

I've searched for a build in function but I didn't find anything.

What's the best way to convert this?

user1007522
  • 7,858
  • 17
  • 69
  • 113
  • A Java `Date` object is just an arbitrary point in time. What is the _other_ point in time against which you want to compute a difference? – Tim Biegeleisen Apr 09 '17 at 12:14
  • so it can be `1 year 2 months 5 days 6 hours 36 min 12 sec ago`? – Youcef LAIDANI Apr 09 '17 at 12:14
  • Can you use Java 8? If so, you will want to look into `Duration` class and friends. – Ole V.V. Apr 09 '17 at 12:56
  • Please search Stack Overflow before posting a Question. This topic has been addressed many times already. – Basil Bourque Apr 09 '17 at 15:39
  • I'm sorry I didn't found a topic. Because I don't know how such a format is called. – user1007522 Apr 11 '17 at 11:44
  • @BasilBourque Maybe there are duplicates for this question, but I don't think the question you have pointed (from 2010) has valid non out-of-date answers for this one. – Tristan Apr 12 '17 at 08:15
  • @Tristan What would an up-to-date Answer address that is not covered by the Answers on that original Question? And, why not add an up-to-date Answer to that original Question rather than here? I do not know how Stack Overflow policy works in this regard. Please point me to rules or a Meta page if you can. – Basil Bourque Apr 12 '17 at 15:59
  • Another duplicate: [How can I calculate a time span in Java and format the output?](http://stackoverflow.com/q/635935/642706). @Tristan See [my up-to-date Answer](http://stackoverflow.com/a/42462296/642706) ;-) – Basil Bourque Apr 12 '17 at 16:22

1 Answers1

1

First, dont use the ambiguous "Date" in java, use the new classes introduced in Java 8.

Now, if u have 2 LocalDateTime, (the date of the past event and "now"), you can evaluate the difference between and format it : Java 8: Calculate difference between two LocalDateTime

UPDATE : I have to say it's available for java 7 (and 6) only through the backport library : http://www.threeten.org/threetenbp/, standard only in java 8

Community
  • 1
  • 1
Tristan
  • 8,733
  • 7
  • 48
  • 96