skip to Main Content

I’m trying to get the day number to put that number in a textView. for example, we know that today is Wednesday the 26th, knowing this information, I want to take this number "26" and put it inside a textView that will be displayed inside a screen that has a weekly calendar. so I try to help create a logic that tells me if it’s Wednesday it returns the current day of the week (26), if I tell Thursday, it returns me (27) and so on for the entire week. tks. 😉

val diaDaSemana: DayOfWeek
    diaDaSemana.get(DayOfWeek.THURSDAY)
    binding.txtNumeroQuarta = diaDaSemana.toString()

I’m trying this way to get the day.

calender image :

calender image

basically I just want the number informing the week, for example, I inform you that it’s Wednesday, then you have to return me on the 26th so I can put it in the textView.

3

Answers


  1. val day = Calendar.DAY_OF_MONTH.toString()
    binding.txtNumeroTerca.text = day
    
    Login or Signup to reply.
  2. It is because add() method returns nothing. If you want to get day of month, try something like that:

    val calendar: Calendar = Calendar.getInstance()
    
    calendar.add(Calendar.DAY_OF_MONTH, Calendar.TUESDAY)
    val day= calendar.get(Calendar.DAY_OF_MONTH)
    
    binding.txtNumeroTerca.text = day.toString()
    

    And I recommend using numbers in add() method than code of week days.

    Login or Signup to reply.
  3. You can do this with LocalDate. From a given date (I set it to today by default in the example code), find the first day of the week by using TemporalAdjusters.previousOrSame to get the most recent Sunday. Then use another TemporalAdjuster to get next occurrence of the input day of the week.

    fun DayOfWeek.dayOfMonth(dateInWeek: LocalDate = LocalDate.now()): Int {
        val firstDateOfWeek = dateInWeek.with(TemporalAdjusters.previousOrSame(DayOfWeek.SUNDAY))
        val dateOfDayOfWeek = firstDateOfWeek.with(TemporalAdjusters.nextOrSame(this))
        return dateOfDayOfWeek.dayOfMonth
    }
    
    // ...
    
    binding.txtNumeroQuarta = DayOfWeek.THURSDAY.dayOfMonth().toString()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search