Kotlin - Find current week start and end date

355 Views Asked by At

I have a spring-boot application that will be deployed in multiple countries, and have a requirement to generate the start and end date of the current week. Eg:

TimeZone : London
now : 7th March (Tuesday)
startDate : 2023-03-06T00:00:00Z (Monday)
endDate :   2023-03-12T23:59:59Z (Sunday)  

For doing so, I was using java locale with the following code

now = ZonedDateTime.now(timeZone)
val dayOfWeek = WeekFields.of(locale).dayOfWeek()
val startDate = now.with(dayOfWeek, 1).with(LocalTime.MIN)
val endDate = now.with(dayOfWeek, 7).with(LocalTime.MAX)

Here, the locale will vary with the countries. Eg :

Country Locale Obtained Week start Expected Start
Germany de_DE Monday Monday
Singapore en-SG Sunday Monday
Saudi Arabia ar-SA-u-nu-latn Saturday Saturday

As you can see there is mismatch in Singapore.

Could anyone suggest a better way or library perform this operation.

I checked the first day of the week from this link: First Day of the Week in Different Countries, where it says that the first day of the week for Singapore is Monday.

Another Evidence: Singapore Government Website

0

There are 0 best solutions below