Created
August 2, 2021 10:20
-
-
Save dzungvu/d0841aaa829ecac37b76ef4f06ae2bbe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.text.SimpleDateFormat | |
import java.util.* | |
fun SimpleDateFormat.formatToVN(date: Date): String { | |
var strReturn = toPattern() | |
val cal = Calendar.getInstance() | |
cal.time = date | |
val hour = cal.get(Calendar.HOUR).toString().toNumberWithPadding() | |
strReturn = strReturn.replace("hh", hour, true) | |
val minute = cal.get(Calendar.MINUTE).toString().toNumberWithPadding() | |
strReturn = strReturn.replace("mm", minute) | |
val ampm: String = | |
when (cal.get(Calendar.AM_PM)) { | |
Calendar.AM -> "SA" | |
else -> "CH" | |
} | |
strReturn = strReturn.replace("a", ampm) | |
val longDate: String | |
val shortDate: String | |
val veryShortDate: String | |
when (cal.get(Calendar.DAY_OF_WEEK)) { | |
Calendar.SUNDAY -> { | |
longDate = "Chủ nhật" | |
shortDate = "Chủ nhật" | |
veryShortDate = "CN" | |
} | |
Calendar.MONDAY -> { | |
longDate = "Thứ hai" | |
shortDate = "Thứ 2" | |
veryShortDate = "T2" | |
} | |
Calendar.TUESDAY -> { | |
longDate = "Thứ ba" | |
shortDate = "Thứ 3" | |
veryShortDate = "T3" | |
} | |
Calendar.WEDNESDAY -> { | |
longDate = "Thứ tư" | |
shortDate = "Thứ 4" | |
veryShortDate = "T4" | |
} | |
Calendar.THURSDAY -> { | |
longDate = "Thứ năm" | |
shortDate = "Thứ 5" | |
veryShortDate = "T5" | |
} | |
Calendar.FRIDAY -> { | |
longDate = "Thứ sáu" | |
shortDate = "Thứ 6" | |
veryShortDate = "T6" | |
} | |
else -> { | |
longDate = "Thứ bảy" | |
shortDate = "Thứ 7" | |
veryShortDate = "T7" | |
} | |
} | |
strReturn = strReturn.replace("EEEE", longDate, true) | |
strReturn = strReturn.replace("EEE", shortDate, true) | |
strReturn = strReturn.replace("EE", veryShortDate, true) | |
val day: String = cal.get(Calendar.DAY_OF_MONTH).toString().toNumberWithPadding() | |
strReturn = strReturn.replace("dd", day, true) | |
val longMonth: String | |
val shortMonth: String | |
val veryShortMonth: String | |
when (cal.get(Calendar.MONTH)) { | |
0 -> { | |
longMonth = "Tháng một" | |
shortMonth = "Tháng 1" | |
veryShortMonth = "01" | |
} | |
1 -> { | |
longMonth = "Tháng hai" | |
shortMonth = "Tháng 2" | |
veryShortMonth = "02" | |
} | |
2 -> { | |
longMonth = "Tháng ba" | |
shortMonth = "Tháng 3" | |
veryShortMonth = "03" | |
} | |
3 -> { | |
longMonth = "Tháng tư" | |
shortMonth = "Tháng 4" | |
veryShortMonth = "04" | |
} | |
4 -> { | |
longMonth = "Tháng năm" | |
shortMonth = "Tháng 5" | |
veryShortMonth = "05" | |
} | |
5 -> { | |
longMonth = "Tháng sáu" | |
shortMonth = "Tháng 6" | |
veryShortMonth = "06" | |
} | |
6 -> { | |
longMonth = "Tháng bảy" | |
shortMonth = "Tháng 7" | |
veryShortMonth = "07" | |
} | |
7 -> { | |
longMonth = "Tháng tám" | |
shortMonth = "Tháng 8" | |
veryShortMonth = "08" | |
} | |
8 -> { | |
longMonth = "Tháng chín" | |
shortMonth = "Tháng 9" | |
veryShortMonth = "09" | |
} | |
9 -> { | |
longMonth = "Tháng mười" | |
shortMonth = "Tháng 10" | |
veryShortMonth = "10" | |
} | |
10 -> { | |
longMonth = "Tháng mười một" | |
shortMonth = "Tháng 11" | |
veryShortMonth = "11" | |
} | |
else -> { | |
longMonth = "Tháng mười hai" | |
shortMonth = "Tháng 12" | |
veryShortMonth = "12" | |
} | |
} | |
strReturn = strReturn.replace("MMMM", longMonth) | |
strReturn = strReturn.replace("MMM", shortMonth) | |
strReturn = strReturn.replace("MM", veryShortMonth) | |
val year = cal.get(Calendar.YEAR).toString() | |
val shortYear = year.substring(2, 4) | |
strReturn = strReturn.replace("yyyy", year, true) | |
strReturn = strReturn.replace("yy", shortYear, true) | |
return strReturn | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment