Assuming the following code...
Instant x = Instant.now();
How do I get day of week from x?
Instant now() Method in Java This method requires no parameters and it returns the current instant from the system UTC clock.
The Instant now() method will query the system UTC clock to obtain the current instant. The Instant now(Clock clock) method will query the specified now to obtain the current time.
Get Instant object which you want to convert to LocalDate. Create ZoneId instance based on Location. Pass ZoneId to atZone() method to get ZoneDateTime . Call toLocalDate() on ZoneDateTime object to get LocalDate .
You have to convert it to ZonedDateTime
Instant.now().atZone(ZoneId.systemDefault()).getDayOfWeek()
I have awarded points to techtabu, but I ended up using atOffset instead. Here is where I ended up...
int currentDayOfWeekValue = Instant.now().atOffset(ZoneOffset.UTC).getDayOfWeek().getValue();
I am amazed how difficult the Java8 datetime libraries are. There are so many variations of similar concepts...
Is Zulu and UTC the same or different?
What is the timezone associated with Instant.now() - the results suggest Zulu?
Why can't I manipulate an Instant object like a LocalDateTime - methods are similar but different?
How are ZonedDateTime and OffsetDateTime different - they seem to be addressing the same concept.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With