Package io.github.anass_abea
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().
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 aTimeAgoobject and initializes the reference date to the current date.TimeAgo(long from)Allocates aTimeAgoobject and initializes the reference date to the given float.TimeAgo(java.time.Instant instant)Allocates aTimeAgoobject and initializes the reference date (float) to the given Instant using the constructorTimeAgo(Date)andDate.from(Instant).TimeAgo(java.util.Date from)Allocates aTimeAgoobject and initializes the reference date (float) to the given date usingDate.getTime() -
Method Summary
Modifier and Type Method Description TimeAgolanguage(Languages language)Changes the default used languageEnglishDefaultto one of the languages inLanguagesTimeAgolanguage(Language language)Changes the default used languageEnglishDefaultto a custom language using the interfaceLanguagejava.lang.StringtimeUntil(long targetDate)takes as a parameter alongof 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.StringtimeUntil(java.time.Instant targetDate)takes as a parameter anInstantof 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.StringtimeUntil(java.util.Date targetDate)takes as a parameter aDateof the second date, calculates the difference, the unit and the state (future or past) and returns a String with the formatted relative date.
-
Constructor Details
-
TimeAgo
public TimeAgo(java.util.Date from)Allocates aTimeAgoobject and initializes the reference date (float) to the given date usingDate.getTime()- Parameters:
from- the reference date
-
TimeAgo
public TimeAgo(long from)Allocates aTimeAgoobject 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 aTimeAgoobject and initializes the reference date (float) to the given Instant using the constructorTimeAgo(Date)andDate.from(Instant).- Parameters:
instant- the instant of the reference date
-
TimeAgo
public TimeAgo()Allocates aTimeAgoobject and initializes the reference date to the current date.
-
-
Method Details
-
language
Changes the default used languageEnglishDefaultto one of the languages inLanguages- Parameters:
language- language from theLanguagesenumeration- Returns:
- TimeAgo returns the object after changing the language
-
language
Changes the default used languageEnglishDefaultto a custom language using the interfaceLanguage- Parameters:
language- a custom class following theLanguageinterface.- Returns:
- TimeAgo returns the object after changing the language
-
timeUntil
public java.lang.String timeUntil(long targetDate)takes as a parameter alongof 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 aDateof 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
nullthe 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 anInstantof 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
nullthe default value will be the current time- Parameters:
targetDate- the target Instant from which the relative date will be calculated- Returns:
- String
-