[Android][JAVA][kotlin] 获取各种语言的星期,月份,长月份,缩写月份

kotlin 写法

import java.text.DateFormatSymbols

    private fun loadTime() {
        val calendarInstance = Calendar.getInstance()
        val day = calendarInstance.get(Calendar.DAY_OF_MONTH)
        val month = calendarInstance.get(Calendar.MONTH)
        val weekday = calendarInstance.get(Calendar.DAY_OF_WEEK)
        binding!!.weekdaysTv.text = getWeekday(weekday)

        binding!!.monthAndDayTv.text = baseContext.resources.getString(
            R.string.month_and_day,
            getMonth(month),
            day.toString()
        )
    }

fun getMonth(month: Int): String? {
    return DateFormatSymbols().months[month - 1]
}
fun getWeekday(weekday: Int): String? {
    return DateFormatSymbols().shortWeekdays[weekday]
    //return DateFormatSymbols().weekdays[weekday]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

kotlin 获取各种语言的星期,月份,长月份,缩写月份等,非常方便
这样在不同的语言下返回不同的日期,不需要自己关注翻译;
DateFormatSymbols().shortWeekdays[weekday] -------Sun
return DateFormatSymbols().weekdays[weekday] -----Sunday