Android Studio/ReviewMate

[Android Studio/Kotlin 공부] Calendar

kangchaewon 2023. 12. 6. 01:11

private var selectedDate: String = "0"
private var selectedDate_add1: String = "0"

binding.calendarView.setOnDateChangeListener{view, year, month, dayOfMonth->
val calendar = Calendar.getInstance() // 일단 현재 날짜 가져옴
    calendar.set(year, month, dayOfMonth) // 사용자가 선택한 날짜로 Calendar 객체를 업데이트

    val dateFormat = SimpleDateFormat("yyyy-MM-dd")
    selectedDate = dateFormat.format(calendar.time)
    calendar.add(Calendar.DAY_OF_MONTH, 1)
    selectedDate_add1 = dateFormat.format(calendar.time) //

    Toast.makeText(context, "선택한 날짜: $selectedDate", Toast.LENGTH_SHORT).show()
    updateReviewListForSelectedDate() // 수행할 함수

}
  • SimpleDateFormat(”yyyy-MM-dd”) : 날짜 형식 format
  • Calendar.add(Calendar.DAY_OF_MONTH, 1) : 현재 날짜에 1일을 더함(현재 날짜 강제 지정)
  • dataFormat(형식 설정한 변수).format(날짜 변수)
    binding.calendarView.setOnDateChangeListener{view, year, month, dayOfMonth->
    val calendar = Calendar.getInstance() // 일단 현재 날짜 가져옴
        calendar.set(year, month, dayOfMonth) // 사용자가 선택한 날짜로 Calendar 객체를 업데이트
    
        val dateFormat = SimpleDateFormat("yyyy-MM-dd")
        selectedDate = dateFormat.format(calendar.time)
        calendar.add(Calendar.DAY_OF_MONTH, 1)
        selectedDate_add1 = dateFormat.format(calendar.time) //
    
        Toast.makeText(context, "선택한 날짜: $selectedDate", Toast.LENGTH_SHORT).show()
        updateReviewListForSelectedDate() // 수행할 함수
    
    }
    
    • SimpleDateFormat(”yyyy-MM-dd”) : 날짜 형식 format
    • Calendar.add(Calendar.DAY_OF_MONTH, 1) : 현재 날짜에 1일을 더함(현재 날짜 강제 지정)
    • dataFormat(형식 설정한 변수).format(날짜 변수)
  • private var selectedDate: String = "0" private var selectedDate_add1: String = "0"
private fun updateReviewListForSelectedDate() {
        if(MyApplication.checkAuth()){
            MyApplication.db.collection("reviews")
                .whereGreaterThanOrEqualTo("date", selectedDate)// 비효율..
                .whereLessThan("date", selectedDate_add1)
                .orderBy("date", Query.Direction.DESCENDING)
                .get()
                .addOnSuccessListener { result ->
                    val itemList = mutableListOf<ItemFeedModel>()
                    for(document in result){
                        val item = document.toObject(ItemFeedModel::class.java)
                        if(MyApplication.email.equals(item.email)){
                            item.docId = document.id
                            itemList.add(item)
                        }
                    }
                    binding.feedRecyclerView.layoutManager = LinearLayoutManager(requireContext())
                    binding.feedRecyclerView.adapter = MyFeedAdapter(requireContext(), itemList)
                }
                .addOnFailureListener{
                    onError()
                }
        }
    }

문제점 :

  • firebase 필드에 있는 date 형식이 “yyyy-MM-dd 시:초” 인데 현재 내가 선택한 달력의 날짜 “yyyy-MM-dd”와 비교하는 코드가 비효율적인 것 같다.