Search code examples
androidkotlindatedayofweekjava.util.calendar

How to get Thisweek start and end date and Lastweek start and end date in Kotlin


I need to get this week startdate like 2023-02-20 and last week startdate and end date.

startdate will be monday.

so I create 4 variables like below.

private var thisWeekStart : Long = 0
    private var thisWeekEnd : Long = 0
    private var lastWeekStart : Long = 0
    private var lastWeekEnd : Long = 0

And I tried to assign something like below..

var cal = Calendar.getInstance()
        cal.time = Date()
        thisWeekEnd = cal.timeInMillis

        // 이번주 시작
        cal.get(Calendar.DAY_OF_WEEK)
        thisWeekStart = cal.timeInMillis

        cal.add(Calendar.DAY_OF_WEEK, -1)
        lastWeekStart = cal.timeInMillis

        cal.clear()
        cal.set(Calendar.DAY_OF_WEEK, -1)
        lastWeekStart = cal.timeInMillis

But it throws milliseconds not like yyyy-MM-dd format.

And I'm not sure is that correct way of keep clearing calendar like above.

Most of all, I can't get last week's end date with above way.

Is there any good way to get this weed and last week start, end date?


Solution

  • At first, we define our desired output format. In this case we will use yyyy-MM-dd. In the next step we save the current date in a variable. In the third line we define timezone in which we are working (I used Europe/Berlin for testing purposes, bacause I'm in germany).

    In the next steps we create Calendar instance for each desired date and manipulate the date to our needs.

    Important: You have to add the line firstDayOfWeek = Calendar.MONDAY only if your week starts at another date than Sunday.

    val dateFormat = SimpleDateFormat("yyyy-MM-dd")
    val today = Calendar.getInstance()
    val timeZone = TimeZone.getTimeZone("Europe/Berlin")
    
    val startOfWeek = Calendar.getInstance(timeZone).apply {
        firstDayOfWeek = Calendar.MONDAY
        set(Calendar.DAY_OF_WEEK, Calendar.MONDAY)
    }.time
    
    val endOfWeek = Calendar.getInstance(timeZone).apply {
        firstDayOfWeek = Calendar.MONDAY
        set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY)
    }.time
    
    val startOfLastWeek = Calendar.getInstance(timeZone).apply {
        firstDayOfWeek = Calendar.MONDAY
        set(Calendar.WEEK_OF_YEAR, today.get(Calendar.WEEK_OF_YEAR) - 1)
        set(Calendar.DAY_OF_WEEK, Calendar.MONDAY)
    }.time
    
    val endOfLastWeek = Calendar.getInstance(timeZone).apply {
        firstDayOfWeek = Calendar.MONDAY
        set(Calendar.WEEK_OF_YEAR, today.get(Calendar.WEEK_OF_YEAR) - 1)
        set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY)
    }.time
    
    val startOfWeekAsString = dateFormat.format(startOfWeek)
    val endOfWeekAsString = dateFormat.format(endOfWeek)
    val startOfLastWeekAsString = dateFormat.format(startOfLastWeek)
    val endOfLastWeekAsString = dateFormat.format(endOfLastWeek)
    
    println("Start of week: $startOfWeekAsString")
    println("End of week: $endOfWeekAsString")
    println("Start of last week: $startOfLastWeekAsString")
    println("End of last week: $endOfLastWeekAsString")
    

    Expected output:

    Start of week: 2023-02-13
    End of week: 2023-02-19
    Start of last week: 2023-02-06
    End of last week: 2023-02-12