How to get DayOfWeek from an Instant.now()

走远了吗. 提交于 2020-07-10 05:36:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!