Git Product home page Git Product logo

elapsedtime's Introduction

ElapsedTime

Build Status Coverage Status License Info

ElapsedTime is a lightweight Java library which generates a string representation of an elapsed time. (e.g.: "2 hours ago").

Installation

Add the maven dependency to your project or download the library and compile it (mvn clean install):

<dependency>
  <groupId>com.vdurmont</groupId>
  <artificatId>elapsedtime</artifactId>
  <version>1.1.0</version>
</dependency>

Usage

Configuration

The configuration is optional and should be done once in your app lifetime.

// Change the default locale (default is ENGLISH)
ElapsedTime.defaultLocale = Locale.FRENCH;
// Change the default smallest division.
// Everything smaller than this will be printed as "Moments ago"
// Default is SECOND
ElapsedTime.smallestTimeDivision = TimeDivision.MINUTE;

Code

// Basic usage
String str1 = ElapsedTime.getFromDurationMillis(500); // Moments ago
String str2 = ElapsedTime.getFromDurationMillis(1000); // 1 second ago
String str3 = ElapsedTime.getFromDurationMillis(3650000); // 1 hour ago
// And so on...

// Specify a locale, just for this call
String str4 = ElapsedTime.getFromDurationMillis(1000, ElapsedTime.Locale.FRENCH); // Il y a 1 seconde

// Alternatives
String str5 = ElapsedTime.getFromDurationSeconds(/** seconds */);
String str6 = ElapsedTime.getFromDurationSeconds(/** seconds */, /** locale */);
String str7 = ElapsedTime.getFromDate(/** a date in the past */);
String str8 = ElapsedTime.getFromDate(/** a date in the past */, /** locale */);

Duration breakdown

Range Sample Output
0 to 750 milliseconds Moments ago
750 milliseconds to 1750 milliseconds 1 second ago
1.75 to 45 seconds x seconds ago
45 to 105 seconds 1 minute ago
105 seconds to 45 minutes x minutes ago
45 to 105 minutes 1 hour ago
105 minutes to 22 hours x hours ago
22 to 36 hours 1 day ago
36 hours to 25 days x days ago
26 days to 55 days 1 month ago
56 days to 11 months x months ago
11 months to 23 months 1 year ago
23 months and more x years ago

This breakdown was inspired by momentjs.

Contribute

Find a bug or give an idea for a feature

Leave a message in the issues!

Add a language

¿Habla usted español? Вы говорите по-русски? 你说**话?Sprechen Sie Deutsch? Puhutteko suomea?

Pull requests are more than welcome for new languages!
Add an entry in the ElapsedTime.Locale enum with the right strings and you're done!

Current todolist

  • Accept < 0 duration (e.g.: "in 24 minutes")
  • Implement a better personalization of the generated string (e.g.: "1 hour and 32 minutes ago" could be possible)
  • Optimize
    • less string instanciations
    • general smaller footprint
    • stop the divideDuration() method asap
  • Android support? (locale in system)

License

See LICENSE.md

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.