Class KiwiInstants
- java.lang.Object
-
- org.kiwiproject.time.KiwiInstants
-
public class KiwiInstants extends Object
Provides utilities related toInstant.First,
KiwiInstantsadds convenience methods for adding and subtracting common time increments toInstantobjects in the form ofplus<TemporalField>andminus<TemporalField>methods such asplusMinutes(Instant, long)andminusDays(Instant, long).The
Instantclass does not support all of theChronoUnitvalues via the genericInstant.plus(long, TemporalUnit)andInstant.minus(long, TemporalUnit)methods. Specifically it only supports (as of this writing):ChronoUnit.NANOSChronoUnit.MICROSChronoUnit.MILLISChronoUnit.SECONDSChronoUnit.MINUTESChronoUnit.HOURSChronoUnit.HALF_DAYSChronoUnit.DAYS
Instantdoes not support any of the other units larger thanChronoUnit.DAYSsuch as weeks, months, years, etc. This class supports adding and subtracting months and years directly, as well as other units via theplusUsingZDT(Instant, long, TemporalUnit)andminusUsingZDT(Instant, long, TemporalUnit)methods.Second,
KiwiInstantssupports adding and subtracting months and years by first converting theInstantto aZonedDateTimeatZoneOffset.UTC. For exampleplusMonths(Instant, long). For otherTemporalUnitvalues you can use theplusUsingZDT(Instant, long, TemporalUnit)andminusUsingZDT(Instant, long, TemporalUnit)methods, which will work as long asZonedDateTimesupports the providedTemporalUnit.Third,
KiwiInstantsprovides utilities to retrieve individual standard parts of anInstant(second, minute, hour, day, month, year) using the variousget<TemporalField>methods, for examplegetHour(Instant).
-
-
Constructor Summary
Constructors Constructor Description KiwiInstants()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static intgetDayOfMonth(Instant instant)Return the value of the day of the month frominstant.static intgetHour(Instant instant)Return the value of the hour frominstant.static intgetMinute(Instant instant)Return the value of the minute frominstant.static MonthgetMonth(Instant instant)Return theMonthfrominstant.static intgetMonthValue(Instant instant)Return the value of the month (1-12) frominstant.static intgetSecond(Instant instant)Return the value of the second frominstant.static intgetYear(Instant instant)Return the value of the year frominstant.static InstantminusDays(Instant instant, long days)static InstantminusHours(Instant instant, long hours)static InstantminusMinutes(Instant instant, long minutes)static InstantminusMonths(Instant instant, long months)static InstantminusUsingZDT(Instant instant, long amountToSubtract, TemporalUnit unit)Subtract any givenamountToSubtractof typeTemporalUnitfrom the given instant.static InstantminusYears(Instant instant, long years)static InstantplusDays(Instant instant, long days)static InstantplusHours(Instant instant, long hours)static InstantplusMinutes(Instant instant, long minutes)static InstantplusMonths(Instant instant, long months)static InstantplusUsingZDT(Instant instant, long amountToAdd, TemporalUnit unit)Add any givenamountToAddof typeTemporalUnitfrom the given instant.static InstantplusYears(Instant instant, long years)
-
-
-
Method Detail
-
getSecond
public static int getSecond(Instant instant)
Return the value of the second frominstant.- Parameters:
instant- theInstant- Returns:
- the value of the second
-
getMinute
public static int getMinute(Instant instant)
Return the value of the minute frominstant.- Parameters:
instant- theInstant- Returns:
- the value of the minute
-
getHour
public static int getHour(Instant instant)
Return the value of the hour frominstant.- Parameters:
instant- theInstant- Returns:
- the value of the hour
-
getDayOfMonth
public static int getDayOfMonth(Instant instant)
Return the value of the day of the month frominstant.- Parameters:
instant- theInstant- Returns:
- the value of the day of the month
-
getMonthValue
public static int getMonthValue(Instant instant)
Return the value of the month (1-12) frominstant.- Parameters:
instant- theInstant- Returns:
- the value the month, i.e. 1-12
-
getYear
public static int getYear(Instant instant)
Return the value of the year frominstant.- Parameters:
instant- theInstant- Returns:
- the value of the year
-
minusMonths
public static Instant minusMonths(Instant instant, long months)
- Parameters:
instant- theInstantmonths- the amount of time to go backward in months- Returns:
- a new
Instantappropriately adjusted - Implementation Note:
- uses
ZonedDateTime(atZoneOffset.UTC) to traverse by month
-
minusYears
public static Instant minusYears(Instant instant, long years)
- Parameters:
instant- theInstantyears- the amount of time to go backward in years- Returns:
- a new
Instantappropriately adjusted - Implementation Note:
- uses
ZonedDateTime(atZoneOffset.UTC) to traverse by month
-
minusUsingZDT
public static Instant minusUsingZDT(Instant instant, long amountToSubtract, TemporalUnit unit)
Subtract any givenamountToSubtractof typeTemporalUnitfrom the given instant. This first converts theInstantinto aZonedDateTimeatZoneOffset.UTCbefore applying the subtraction.
-
plusMonths
public static Instant plusMonths(Instant instant, long months)
- Parameters:
instant- theInstantmonths- the amount of time to go forward in months- Returns:
- a new
Instantappropriately adjusted - Implementation Note:
- uses
ZonedDateTime(atZoneOffset.UTC) to traverse by month
-
plusYears
public static Instant plusYears(Instant instant, long years)
- Parameters:
instant- theInstantyears- the amount of time to go forward in years- Returns:
- a new
Instantappropriately adjusted - Implementation Note:
- uses
ZonedDateTime(atZoneOffset.UTC) to traverse by month
-
plusUsingZDT
public static Instant plusUsingZDT(Instant instant, long amountToAdd, TemporalUnit unit)
Add any givenamountToAddof typeTemporalUnitfrom the given instant. This first converts theInstantinto aZonedDateTimeatZoneOffset.UTCbefore applying the addition.
-
-