I have an EpochTime for Now and I want to get 30 days, 3 Months, and 1 Year back epoch time How to do that in java code.
long todayEpochTime = System.currentTimeMillis()/1000;
System.out.println(todayEpochTime);
I have an EpochTime for Now and I want to get 30 days, 3 Months, and 1 Year back epoch time How to do that in java code.
long todayEpochTime = System.currentTimeMillis()/1000;
System.out.println(todayEpochTime);
Using LocalDate
we can try:
LocalDate date = LocalDate.now().minusDays(30).minusMonths(3).minusYears(1);
ZoneId zoneId = ZoneId.systemDefault();
long newEpoch = date.atStartOfDay(zoneId).toEpochSecond();
As of the writing of this question, the date was 9th December, 2019, and the new epoch value corresponded to this date:
Wednesday, August 8, 2018 10:00:00 PM
You can obtains an instance of Instant
using milliseconds from the epoch at particular ZoneId
and then subtract days,months and years
long todayEpochTime = System.currentTimeMillis();
long dateTime = Instant.ofEpochMilli(todayEpochTime)
.atZone(ZoneId.systemDefault())
.minusDays(30)
.minusMonths(3)
.minusYears(1)
.toEpochSecond();
System.out.println(todayEpochTime);
System.out.println(dateTime);
If you want to work in UTC only, rather than a time zone:
Instant
.ofEpochSecond( secondsSinceEpoch )
.minus(
Period.ofDays( 30 )
)
.toEpochSecond()
Beware: While days are always 24 hours long in UTC, days in various time zones may run longer or shorter, yielding different results.
Another issue: For any given moment, the date varies by time zone.
If you want to work with dates rather than moments:
Instant
.ofEpochSecond( secondsSinceEpoch )
.atZone(
ZoneId.of( "Africa/Tunis" )
)
.toLocalDate()
.minusDays( 30 )
.atStartOfDay(
ZoneId.of( "Africa/Tunis" )
)
.toEpochSecond()
Apparently you have a count of whole seconds since the epoch reference of first moment of 1970 in UTC, 1970-01-01T00:00:00.0Z.
Instant
Parse as an Instant
.
Instant instant = Instant.ofEpochSecond( secondsSinceEpoch ) ;
For any given moment, the date varies around the globe by time zone. A few minutes after midnight in Tokyo Japan is a new day while still “yesterday” in Paris France and in Toledo Ohio US.
ZonedDateTime
Apply the time zone by which you want to perceive a date. Pass a ZoneId
to the Instant
to get a ZonedDateTime
.
ZoneId z = ZoneId.of( "Asia/Tokyo" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;
Subtract your spans of time.
ZonedDateTime thirtyDaysEarlier = zdt.minusDays( 30 ) ;
ZonedDateTime threeMonthsEarlier = zdt.minusMonths( 3 ) ;
ZonedDateTime oneYearEarlier = zdt.minusYears 1 ) ;
LocalDate
If you care only about the date portion, without time-of-day and without time zone, extract a LocalDate
. Then do the same date-math.
LocalDate localDate = zdt.toLocalDate() ;
Period
You can represent your spans of time using a Period
. Then do your date math. Named variables kept as constants is preferred over mere integers to make your code more self-documenting, provide type-safety, and ensure valid values.
Period periodDays30 = Period.ofDays( 30 ) ;
Period periodMonths3 = Period.ofMonths( 3 ) ;
Period periodYears1 = Period.ofYears( 1 ) ;
LocalDate then = localDate.minus( periodDays30 ) ;
As for strings, to generate text in standard ISO 8601 format, merely call .toString()
.
For other formats, use a DateTimeFormatter
. This has been addressed many times already, so search Stack Overflow to learn more.