Allow getting the week number of a LocalDate and a LocalDateTime
See original GitHub issueHello,
I think it’d be great to have natively the ability to know which week number a particular date is from.
It’s very easy to do with GregorianCalendar
in the JVM or Android (calendar.get(Calendar.WEEK_OF_YEAR)
), but I see no such facility for Kotlin common code yet.
Have a great day!
Issue Analytics
- State:
- Created 2 years ago
- Comments:17 (7 by maintainers)
Top Results From Across the Web
Get Week Number of LocalDate (Java 8) - Stack Overflow
LocalDate date = LocalDate.now(); TemporalField woy = WeekFields.of(Locale.getDefault()).weekOfWeekBasedYear(); int weekNumber = ...
Read more >Getting the Week Number From Any Date - Baeldung
In this article, we'll study several options that we can use in Java to get the week number for a given date. We'll...
Read more >get week number from localdate java - Code Grepper
LocalDate date = LocalDate.now(); TemporalField woy = WeekFields.of(Locale.getDefault()).weekOfWeekBasedYear(); int weekNumber = date.get(woy);
Read more >WeekFields (Java Platform SE 8 ) - Oracle Help Center
The unit that represents week-based-years for the purpose of addition and subtraction. This allows a number of week-based-years to be added to, or...
Read more >Java 8 Date Time - 20 Examples of LocalDate, LocalTime ...
Instant - It represents a timestamp · LocalDate - a date without time e.g. 2014-01-14. · LocalTime - represents time without a date...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Let’s go back to the use case of checking if two dates belong to the same week. How would one implement that, if we did provide the week numbering?
Obviously, this won’t work if the dates are from different years. Jan 1st, 2007 is not the same week as Jan 1st, 2023.
This is much less obviously wrong, but still wrong since some weeks are shared by two years.
Now, this will almost always work, but not always! For example, Jan 1st, 2007, is week 1, but Dec 31st, 2007, is also week 1. The reason is that, by the ISO-8601 numbering, if a week is split between two years, it is assigned a number in terms of the year that has the bigger part of the week.
I don’t see a simple way to check if two dates are in the same week using a week numbering. Here’s a working approach though:
Maybe this is the approach we should be expanding, especially if operations like “find next Monday” or “find last day before this one that was in January” have some other uses.
One use case that I can think of is check if two dates belong to same week, to show a header or something like “This week”. Although I guess this can be achieved by floorDiv of dayOfYear by 7.