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?
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 usedEurope/Berlinfor testing purposes, bacause I'm in germany).In the next steps we create
Calendarinstance for each desired date and manipulate the date to our needs.Important: You have to add the line
firstDayOfWeek = Calendar.MONDAYonly if your week starts at another date than Sunday.Expected output: