Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic human readable time interval [closed]

I would like to express some time intervals in a human readable format. I was looking for a library automatically able to choose the correct size of the units. The intervals will range from milliseconds to weeks. Something like

  • 0.2s
  • 2:00
  • 4 days
  • ...

I know I could manually check the size of the argument and then convert it but before reinventing the wheel I wanted to ask if there is a (standard) library to achieve that.

  • TimeUnit does the conversions but there is no knowledge about the size of the argument
  • a Joda-Time Duration represents exactly what I need but I did not find much about representation (but for the ISO8601 formatting of ReadableDuration)

Edit:

My input in in nanoseconds and output as described above.

like image 530
Matteo Avatar asked Jul 17 '12 11:07

Matteo


1 Answers

Check PrettyTime

PrettyTime is an OpenSource time formatting library. Completely customizable, PrettyTime creates human readable, relative timestamps like those seen on Digg, Twitter, and Facebook. It’s simple, get started “right now!” and in over 18 languages!

For Example

    PrettyTime p = new PrettyTime();
    System.out.println(p.format(new Date()));
    //prints: “right now”

    PrettyTime t = new PrettyTime(new Date(3155692597470L * 3L));
    t.format(new Date(0));
    //3 centuries ago 
like image 192
jmj Avatar answered Nov 14 '22 13:11

jmj