[英]convert timestamp to day of the week android
I am trying to convert a timestamp to a day of the week.我正在尝试将时间戳转换为一周中的某一天。
The goal would be to translate to something like ts -> MON or TUE....目标是翻译成 ts -> MON 或 TUE....
I have tried the code below but it's not working.我已经尝试了下面的代码,但它不起作用。
fun convertToReadableDate(timestamp: Long): String {
val formatter = SimpleDateFormat("dd-mm-yyyy")
val cal = Calendar.getInstance(Locale.ENGLISH)
cal.timeInMillis = timestamp * 1000
val date: LocalDate = LocalDate.parse(formatter.format(cal.time))
return date.dayOfWeek.toString()
}
Any idea?任何想法? Thanks谢谢
You can use this formatter:您可以使用此格式化程序:
fun convertToReadableDate(timestamp: Long): String =
SimpleDateFormat("EEE", Locale.ENGLISH).format(timestamp)
.toUpperCase(Locale.ENGLISH)
get the day of the week from Unix timestamp:-从 Unix 时间戳获取星期几:-
fun getDayOfWeek(timestamp: Long): String { return SimpleDateFormat("EEEE", Locale.ENGLISH).format(timestamp * 1000)
} }
getMonth:获取月份:
fun getMonthFromTimeStamp(timestamp: Long): String { return SimpleDateFormat("MMM", Locale.ENGLISH).format(timestamp * 1000)
} }
getYear:获取年份:
fun getYearFromTimeStamp(timestamp: Long): String { return SimpleDateFormat("yyyy", Locale.ENGLISH).format(timestamp * 1000)
} }
if you need all three combined in one function: (WED-MAY-2021)如果您需要将所有三个组合在一起 function:(WED-MAY-2021)
fun getDayOfWeek(timestamp: Long): String { return SimpleDateFormat("EEEE-MMM-yyyy", Locale.ENGLISH).format(timestamp * 1000) }
if you need a combination of either of two in one function:(WED-MAY)如果您需要二合一 function:(WED-MAY)
fun getDayOfWeek(timestamp: Long): String { return SimpleDateFormat("EEEE-MMM", Locale.ENGLISH).format(timestamp * 1000) }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.