0

Can't find any solution how to get tomorrow's date 13:00. For example: today is 16.01.2019, I need to find how in unix timestrap is 17.01.2019 13:00.

tried this:

LocalDateTime tomorrowWithTime = 
LocalDateTime.of(LocalDate.now().plusDays(1), 13:00);

to set manually 13:00, add it to tomorrows date and then convert to unix timestrap, but no luck :(

  • What is the actual output? – Sid Jan 16 '19 at 11:24
  • Welcome to Stack Overflow. What exactly do you mean by "but no luck"? You have two operations you need to perform: 1) Obtain the appropriate LocalDateTime (or ZonedDateTime, which I'd suggest might be a better fit); 2) Convert a `LocalDateTime` (or `ZonedDateTime`) to a Unix timestamp. Those are very separate - if you've managed the first part, then it would be worth making this question *just* about the second part, ideally showing what you've tried and what happened. – Jon Skeet Jan 16 '19 at 11:24
  • no output. It's not allowed to add time like this and code doesnt work. I've tried different types to write time like: 13, 00, 00 or 13-00-00. Dunno how to set time manually – Michael Blala Jan 16 '19 at 11:41

1 Answers1

0

You were very close to it. To get the LocalDateTime it's

LocalDateTime tomorrowWithTime = LocalDateTime.of(LocalDate.now().plusDays(1), LocalTime.of(13, 0));

Then to convert it to unix timestamp please have a look at this question. In summary you have to give a ZoneId: (to give a working answer I'll use your system zoneId):

ZoneId zoneId = ZoneId.systemDefault(); //Or the appropriate zone
long timestamp = tomorrowWithTime.atZone(zoneId).toEpochSecond();

By the way, if your issue was that you didn't know what to give as parameters to LocalDateTime.of(), your first reflex should be to have a look at the API to see what parameters it accepts.

Ricola
  • 2,621
  • 12
  • 22
  • thank you VERY much. it's exactly what I need. BTW, from what library is "time.atZone(zoneId).toEpochSecond()"? My IntelliJ doesn't know :) – Michael Blala Jan 16 '19 at 11:56
  • Sorry, it was supposed to be `tomorrowWithTime.atZone()...`, I edited it. It comes from [LocalDateTime](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html#atZone-java.time.ZoneId-) – Ricola Jan 16 '19 at 12:00
  • Thank you :) the code now works `LocalDateTime tomorrowWithTime = LocalDateTime.of(LocalDate.now().plusDays(1), LocalTime.of(13, 0)); ZoneId zoneId = ZoneId.systemDefault(); long epochSec = tomorrowWithTime.atZone(zoneId).toEpochSecond();` – Michael Blala Jan 16 '19 at 12:09