Class TimeAgo

java.lang.Object
io.github.anass_abea.TimeAgo

public class TimeAgo
extends java.lang.Object
The class Date is made to create a reference date to calculate the relative difference with second precision.

The class has 4 different constructors and uses two primary methods timeUntil() and language().

the method timeUntil() returns the result as a String of the relative date formatted based on the selected language the method language() is used to set the language to be used for the String result.
Version:
1.0.2
Author:
Anass AIT BEN EL ARBI
See Also:
Date, Instant
  • Constructor Summary

    Constructors 
    Constructor Description
    TimeAgo()
    Allocates a TimeAgo object and initializes the reference date to the current date.
    TimeAgo​(long from)
    Allocates a TimeAgo object and initializes the reference date to the given float.
    TimeAgo​(java.time.Instant instant)
    Allocates a TimeAgo object and initializes the reference date (float) to the given Instant using the constructor TimeAgo(Date) and Date.from(Instant).
    TimeAgo​(java.util.Date from)
    Allocates a TimeAgo object and initializes the reference date (float) to the given date using Date.getTime()
  • Method Summary

    Modifier and Type Method Description
    TimeAgo language​(Languages language)
    Changes the default used language EnglishDefault to one of the languages in Languages
    TimeAgo language​(Language language)
    Changes the default used language EnglishDefault to a custom language using the interface Language
    java.lang.String timeUntil​(long targetDate)
    takes as a parameter a long of the second date, calculates the difference, the unit and the state (future or past) and returns a String with the formatted relative date.
    java.lang.String timeUntil​(java.time.Instant targetDate)
    takes as a parameter an Instant of the second date, calculates the difference, the unit and the state (future or past) and returns a String with the formatted relative date.
    java.lang.String timeUntil​(java.util.Date targetDate)
    takes as a parameter a Date of the second date, calculates the difference, the unit and the state (future or past) and returns a String with the formatted relative date.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • TimeAgo

      public TimeAgo​(java.util.Date from)
      Allocates a TimeAgo object and initializes the reference date (float) to the given date using Date.getTime()
      Parameters:
      from - the reference date
    • TimeAgo

      public TimeAgo​(long from)
      Allocates a TimeAgo object and initializes the reference date to the given float.
      Parameters:
      from - the long value of the reference date
    • TimeAgo

      public TimeAgo​(java.time.Instant instant)
      Allocates a TimeAgo object and initializes the reference date (float) to the given Instant using the constructor TimeAgo(Date) and Date.from(Instant).
      Parameters:
      instant - the instant of the reference date
    • TimeAgo

      public TimeAgo()
      Allocates a TimeAgo object and initializes the reference date to the current date.
  • Method Details

    • language

      public TimeAgo language​(Languages language)
      Changes the default used language EnglishDefault to one of the languages in Languages
      Parameters:
      language - language from the Languages enumeration
      Returns:
      TimeAgo returns the object after changing the language
    • language

      public TimeAgo language​(Language language)
      Changes the default used language EnglishDefault to a custom language using the interface Language
      Parameters:
      language - a custom class following the Language interface.
      Returns:
      TimeAgo returns the object after changing the language
    • timeUntil

      public java.lang.String timeUntil​(long targetDate)
      takes as a parameter a long of the second date, calculates the difference, the unit and the state (future or past) and returns a String with the formatted relative date.
      Parameters:
      targetDate - the target date as long from which the relative date will be calculated
      Returns:
      String
    • timeUntil

      public java.lang.String timeUntil​(java.util.Date targetDate)
      takes as a parameter a Date of the second date, calculates the difference, the unit and the state (future or past) and returns a String with the formatted relative date.

      if The parameter is null the default value will be the current time

      Parameters:
      targetDate - the target date from which the relative date will be calculated
      Returns:
      String
    • timeUntil

      public java.lang.String timeUntil​(java.time.Instant targetDate)
      takes as a parameter an Instant of the second date, calculates the difference, the unit and the state (future or past) and returns a String with the formatted relative date.

      if The parameter is null the default value will be the current time

      Parameters:
      targetDate - the target Instant from which the relative date will be calculated
      Returns:
      String