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 aTimeAgo
object and initializes the reference date to the current date.TimeAgo(long from)
Allocates aTimeAgo
object and initializes the reference date to the given float.TimeAgo(java.time.Instant instant)
Allocates aTimeAgo
object and initializes the reference date (float) to the given Instant using the constructorTimeAgo(Date)
andDate.from(Instant)
.TimeAgo(java.util.Date from)
Allocates aTimeAgo
object and initializes the reference date (float) to the given date usingDate.getTime()
-
Method Summary
Modifier and Type Method Description TimeAgo
language(Languages language)
Changes the default used languageEnglishDefault
to one of the languages inLanguages
TimeAgo
language(Language language)
Changes the default used languageEnglishDefault
to a custom language using the interfaceLanguage
java.lang.String
timeUntil(long targetDate)
takes as a parameter along
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 anInstant
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 aDate
of 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 aTimeAgo
object and initializes the reference date (float) to the given date usingDate.getTime()
- Parameters:
from
- the reference date
-
TimeAgo
public TimeAgo(long from)Allocates aTimeAgo
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 aTimeAgo
object 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 aTimeAgo
object and initializes the reference date to the current date.
-
-
Method Details
-
language
Changes the default used languageEnglishDefault
to one of the languages inLanguages
- Parameters:
language
- language from theLanguages
enumeration- Returns:
- TimeAgo returns the object after changing the language
-
language
Changes the default used languageEnglishDefault
to a custom language using the interfaceLanguage
- Parameters:
language
- a custom class following theLanguage
interface.- Returns:
- TimeAgo returns the object after changing the language
-
timeUntil
public java.lang.String timeUntil(long targetDate)takes as a parameter along
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 aDate
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 anInstant
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
-