问题
Assuming the following code...
Instant x = Instant.now();
How do I get day of week from x?
回答1:
You have to convert it to ZonedDateTime
Instant.now().atZone(ZoneId.systemDefault()).getDayOfWeek()
回答2:
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...
- Instant
- LocalDate
- LocalTime
- LocalDateTime
- OffsetDateTime
- ZoneOffset
- ZonedDateTime
Rhetorical questions:
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.
回答3:
Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
will give you the same result
来源:https://stackoverflow.com/questions/56364324/how-to-get-dayofweek-from-an-instant-now