简体   繁体   中英

Turn ISO 8601 date/time into “x minutes ago” type of string

So I have an ISO 8601 date/time stamp like "2011-04-21T17:07:50-07:00", and I would like to convert it to say something like "23 minutes ago" or something of that sort. Is there a small bit of Java code or a library that would be able to do something like that easily?

Thanks!

What you are looking for is known as fuzzy time . A quick Google on this term found jFuzzyDate

First, parse it into a Date object. Then, make a new Date which will have the current date/time. Then, call the .getTime() methods on both of the Date objects, and subtract. Now you have your time in milliseconds.

You can do your own math from there, because until you start worrying about leap years it's all easy division.

Joda is a great time library and should have all the methods you need to do this: http://joda-time.sourceforge.net/ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM