본문 바로가기
IT/Android

Kotlin] 안드로이드 국가별 시간, 날짜 표시 // getBestDateTimePattern

by 깻잎쌈 2021. 5. 20.
반응형

 

 

Format DateTime in Android - AndroidWave

In this tutorials, We explain How to Format DateTime in Android using SimpleDateFormat. Example Include - Format Date to Text and Parse Text to Date

androidwave.com

 

 

 

Codota

Codota search - find any Java class or method

www.codota.com

 

 val skeleton: String = DateFormat.getBestDateTimePattern(Locale.getDefault(), "MMMM d")
            val formatter = SimpleDateFormat(skeleton, Locale.getDefault())
            formatter.timeZone = TimeZone.getDefault()
            formatter.applyLocalizedPattern(skeleton)

            time = formatter.format(msgTime.time)

각 국가별로 날짜 형식에 맞게 보여진다.

다른 나라는 날짜부터 표시되기도 하고.

한국이면 2월 11일. 

 

private fun timeText(time1: Long, time2: Long) {
            val cal1 = Calendar.getInstance()         
            cal1.timeInMillis = time1                               

            val datePattern: String = getBestDateTimePattern(Locale.getDefault(), "MMMM d")
            val format = SimpleDateFormat(datePattern, Locale.getDefault())
            format.timeZone = TimeZone.getDefault()
            format.applyLocalizedPattern(datePattern)
            timeText!!.text = format.format(cal1.time)                                            
}

 

 

 

반응형

댓글