I'm building a Weather App for a project at uni. I'm trying to put the names of the week days instead of the dates. But for some reason it only writes Thursday. I can't figure what I'm missing.
This is what I tried. I get the myDate from a weather API and I've checked and all dates are right.
var myDate = objData.getString("datetime")
var date = SimpleDateFormat("yyyy-MM-dd").parse(myDate)
val cal = Calendar.getInstance()
cal.time = date
var dayNumber = cal[Calendar.DAY_OF_WEEK]
val formatter: DateFormat = SimpleDateFormat("EEEE", currentLocale)
val dayWeek = formatter.format(dayNumber)
findViewById<TextView>(R.id.textDay1).text = dayWeek
Appreciate any help.
CodePudding user response:
Try the following code.
fun getWeekDay(dateString:String){
val date = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).parse(dateString)
val calendar = Calendar.getInstance()
calendar.time = date!!
val formatter= SimpleDateFormat("EEEE", Locale.getDefault())
val dayWeek = formatter.format(calendar.time)
Log.e("TAG", "getWeekDay: $dayWeek" )
}