1

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);
ernest_k
  • 44,416
  • 5
  • 53
  • 99
Veerendra
  • 39
  • 1
  • 4
  • So you want to start and end with a count of whole seconds since first moment of 1970 in UTC? And ignore time zones when subtracting your spans of time? – Basil Bourque Dec 09 '19 at 17:33
  • What does this have to do with the `JSON` in your tags? If extraneous, edit to remove. – Basil Bourque Dec 09 '19 at 17:36
  • It sounds simple, but may not be that simple. What result would you want from subtracting 3 months from some time of day on May 31? There’s no February 31. What if it’s only May 31 in *some* time zones and not in others? – Ole V.V. Dec 10 '19 at 21:27

3 Answers3

2

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
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
2

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);
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
1

tl;dr

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()

java.time

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 ) ; 

Strings

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154