question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow getting the week number of a LocalDate and a LocalDateTime

See original GitHub issue

Hello,

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:open
  • Created 2 years ago
  • Comments:17 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
dkhalanskyjbcommented, Jun 10, 2022

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?

// wrong attempt 1
fun LocalDate.sameWeekAs(other: LocalDate) =
    weekNumber(weekStart = DayOfWeek.MONDAY) ==
    other.weekNumber(weekStart = DayOfWeek.MONDAY)

Obviously, this won’t work if the dates are from different years. Jan 1st, 2007 is not the same week as Jan 1st, 2023.

// wrong attempt 2
fun LocalDate.sameWeekAs(other: LocalDate) =
    year == other.year &&
    weekNumber(weekStart = DayOfWeek.MONDAY) ==
    other.weekNumber(weekStart = DayOfWeek.MONDAY)

This is much less obviously wrong, but still wrong since some weeks are shared by two years.

// wrong attempt 3
fun LocalDate.sameWeekAs(other: LocalDate) =
    monthsUntil(other).absoluteValue < 12 &&
    weekNumber(weekStart = DayOfWeek.MONDAY) ==
    other.weekNumber(weekStart = DayOfWeek.MONDAY)

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:

fun LocalDate.sameWeekAs(other: LocalDate, firstDayOfWeek: DayOfWeek): Boolean {
    fun LocalDate.firstDayInWeek(): LocalDate =
        minus((dayOfWeek.isoDayNumber - firstDayOfWeek.isoDayNumber).mod(7), DateTimeUnit.DAY)
    return firstDayInWeek() == other.firstDayInWeek()
}

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.

2reactions
4shutoshcommented, Jul 8, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found