0

I'm trying to round a unix time to the first day of the month in Java, but without success. Example:

1314057600 (Tue, 23 Aug 2011 00:00:00 GMT)

to

1312156800 (Mon, 01 Aug 2011 00:00:00 GMT)

The unix time I'm reading from a file and storing it as a Long inside a variable (variable named "valor"). So far I've been able to create a Java timestamp with it

LocalDateTime timestamp = LocalDateTime.ofInstant(Instant.ofEpochSecond(valor), ZoneId.systemDefault());

and create a new timestamp for the beggining of the month:

LocalDate key = LocalDate.of(timestamp.getYear(), timestamp.getMonthValue(), 1);

How do I get this new timestamp as a Long?

Vini.g.fer
  • 11,639
  • 16
  • 61
  • 90

1 Answers1

2

Something like this sounds like it would work:

key.atStartOfDay().atZone(ZoneId.systemDefault()).toEpochSecond()

(Assuming you want it in the system's default timezone).

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • Perfect! My mistake is that I used LocalDate instead of LocalDateTime. LocalDate doesn't have `toEpochSecond`, only `toEpochDay`, which is not the same. – Vini.g.fer May 31 '16 at 20:30