Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get epoch time from yesterday in a range of hours

First off I'm new in this incredible community. This is an amazing site. I'm happy to be part finally.

Every day I have to insert yesterday's data in the DB. For example, today May 22, I have to insert the data of the 21st from 00:00:00 to 23:59:59 in epoch time.

So far I get the epoch time from today with

long now = Instant.now().toEpochMilli();

How could I get yesterday's epoch time? and store the range of hours in two variables? Like

String startDay = 21/05/2020 00:00:00
String endDay = 21/05/2020 23:59:59
like image 819
Rusty23 Avatar asked Dec 31 '22 01:12

Rusty23


1 Answers

You can use java LocalDate like this:

final LocalDate now = LocalDate.now();
final LocalDate yesterday = now.minusDays(1);
final LocalDateTime start = yesterday.atStartOfDay();
final LocalDateTime end = yesterday.atTime(LocalTime.MAX);

And then format date to your desired format.

like image 115
Artyom Rebrov Avatar answered Jan 07 '23 23:01

Artyom Rebrov