在使用DataBinding的時(shí)候我們?cè)趚ml上想格式化時(shí)間戳變成我們想要的格式,這時(shí)候就需要一個(gè)我們自己自定的工具類了,比如Object XXX 或者 一個(gè)companion object寫的伴生單例
比如這樣的格式,在xml是無(wú)法使用
在xml中
android:text='@{MBDateUtil.convertTime(model.start_time, MBDateUtil.TIME_STYLE_ONT)}'
fun convertTime(tiem: Long, type: Int = 99): String {
if (tiem > 0) {
val formatEnd: SimpleDateFormat
if (type == TIME_STYLE_ONT) {
formatEnd = SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault())
} else if (type == TIME_STYLE_TWO) {
formatEnd = SimpleDateFormat("yyyy.MM.dd HH:mm", Locale.getDefault())
} else if (type == TIME_STYLE_THREE) {
formatEnd = SimpleDateFormat("yyyy MM dd HH:mm", Locale.getDefault())
} else if (type == TIME_STYLE_FOUR) {
formatEnd = SimpleDateFormat("yyyy/MM/dd HH:mm", Locale.getDefault())
} else if (type == TIME_STYLE_FIVE) {
formatEnd = SimpleDateFormat("yyyy年MM月dd日 HH:mm", Locale.getDefault())
} else {
formatEnd = SimpleDateFormat("yyyy/MM/dd/HH/mm", Locale.getDefault())
}
val dateEnd = Date(tiem)
return formatEnd.format(dateEnd)
} else {
return ""
}
}
其實(shí)就是需要你告訴jvm這個(gè)方法是靜態(tài)的
---> @JvmStatic
fun convertTime(tiem: Long, type: Int = 99): String {
if (tiem > 0) {
val formatEnd: SimpleDateFormat
if (type == TIME_STYLE_ONT) {
formatEnd = SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault())
} else if (type == TIME_STYLE_TWO) {
formatEnd = SimpleDateFormat("yyyy.MM.dd HH:mm", Locale.getDefault())
} else if (type == TIME_STYLE_THREE) {
formatEnd = SimpleDateFormat("yyyy MM dd HH:mm", Locale.getDefault())
} else if (type == TIME_STYLE_FOUR) {
formatEnd = SimpleDateFormat("yyyy/MM/dd HH:mm", Locale.getDefault())
} else if (type == TIME_STYLE_FIVE) {
formatEnd = SimpleDateFormat("yyyy年MM月dd日 HH:mm", Locale.getDefault())
} else {
formatEnd = SimpleDateFormat("yyyy/MM/dd/HH/mm", Locale.getDefault())
}
val dateEnd = Date(tiem)
return formatEnd.format(dateEnd)
} else {
return ""
}
}
以下是小弟隨便寫的一個(gè)時(shí)間類 ,自己用
class MBDateUtil private constructor() {
companion object {
private val DEBUG = true
private val TAG = "MBDateUtil"
const val TIME_STYLE_ONT: Int = 1
const val TIME_STYLE_TWO: Int = 2
const val TIME_STYLE_THREE: Int = 3
const val TIME_STYLE_FOUR: Int = 4
const val TIME_STYLE_FIVE: Int = 5
fun convertRideLineHeadTime(timeStart: Long, timeEnd: Long): String {
if (timeStart > 0 && timeEnd > 0) {
val cal1 = Calendar.getInstance()
cal1.time = Date(timeStart)
val cal2 = Calendar.getInstance()
cal2.time = Date(timeEnd)
if (cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)) {
val formatStart = SimpleDateFormat("yyyy.MM.dd HH:mm", Locale.getDefault())
val formatEnd = SimpleDateFormat("MM.dd HH:mm", Locale.getDefault())
val dateStart = Date(timeStart)
val dateEnd = Date(timeEnd)
return "${formatStart.format(dateStart)}-${formatEnd.format(dateEnd)}"
} else {
val formatStart = SimpleDateFormat("yyyy.MM.dd HH:mm", Locale.getDefault())
val formatEnd = SimpleDateFormat("yyyy.MM.dd HH:mm", Locale.getDefault())
val dateStart = Date(timeStart)
val dateEnd = Date(timeEnd)
return "${formatStart.format(dateStart)}-${formatEnd.format(dateEnd)}"
}
} else if (timeStart > 0) {
val formatStart = SimpleDateFormat("yyyy.MM.dd HH:mm", Locale.getDefault())
val dateStart = Date(timeStart)
return formatStart.format(dateStart)
} else if (timeEnd > 0) {
val formatEnd = SimpleDateFormat("yyyy.MM.dd HH:mm", Locale.getDefault())
val dateEnd = Date(timeEnd)
return formatEnd.format(dateEnd)
} else {
return ""
}
}
@JvmStatic
fun convertRideLineMedalTime(tiem: Long): String {
if (tiem > 0) {
val formatEnd = SimpleDateFormat("yyyy/MM/dd", Locale.getDefault())
val dateEnd = Date(tiem)
return formatEnd.format(dateEnd)
} else {
return ""
}
}
fun convertRideCurrentTime(tiem: Long): String {
if (tiem > 0) {
val formatEnd = SimpleDateFormat("HH:mm:ss", Locale.getDefault())
val dateEnd = Date(tiem)
return formatEnd.format(dateEnd)
} else {
return ""
}
}
fun convertCurrentTime(tiem: Long): String {
if (tiem > 0) {
val formatEnd = SimpleDateFormat("HH:mm", Locale.getDefault())
val dateEnd = Date(tiem)
return formatEnd.format(dateEnd)
} else {
return ""
}
}
fun convertRideBoxWaringTime(tiem: Long): String {
if (tiem > 0) {
val formatEnd = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
val dateEnd = Date(tiem)
return formatEnd.format(dateEnd)
} else {
return ""
}
}
@JvmStatic
fun convertTime(tiem: Long, type: Int = 99): String {
if (tiem > 0) {
val formatEnd: SimpleDateFormat
if (type == TIME_STYLE_ONT) {
formatEnd = SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault())
} else if (type == TIME_STYLE_TWO) {
formatEnd = SimpleDateFormat("yyyy.MM.dd HH:mm", Locale.getDefault())
} else if (type == TIME_STYLE_THREE) {
formatEnd = SimpleDateFormat("yyyy MM dd HH:mm", Locale.getDefault())
} else if (type == TIME_STYLE_FOUR) {
formatEnd = SimpleDateFormat("yyyy/MM/dd HH:mm", Locale.getDefault())
} else if (type == TIME_STYLE_FIVE) {
formatEnd = SimpleDateFormat("yyyy年MM月dd日 HH:mm", Locale.getDefault())
} else {
formatEnd = SimpleDateFormat("yyyy/MM/dd/HH/mm", Locale.getDefault())
}
val dateEnd = Date(tiem)
return formatEnd.format(dateEnd)
} else {
return ""
}
}
fun convertYearMonthDay(tiem: Long, type: Int = 99): String {
if (tiem > 0) {
val formatEnd: SimpleDateFormat
if (type == TIME_STYLE_ONT) {
formatEnd = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
} else if (type == TIME_STYLE_TWO) {
formatEnd = SimpleDateFormat("yyyy.MM.dd", Locale.getDefault())
} else if (type == TIME_STYLE_THREE) {
formatEnd = SimpleDateFormat("yyyy MM dd", Locale.getDefault())
} else if (type == TIME_STYLE_FOUR) {
formatEnd = SimpleDateFormat("yyyy/MM/dd", Locale.getDefault())
} else if (type == TIME_STYLE_FIVE) {
formatEnd = SimpleDateFormat("yyyy年MM月dd日", Locale.getDefault())
} else {
formatEnd = SimpleDateFormat("yyyy/MM/dd", Locale.getDefault())
}
val dateEnd = Date(tiem)
return formatEnd.format(dateEnd)
} else {
return ""
}
}
fun convertMonthDay(tiem: Long, type: Int = 99): String {
if (tiem > 0) {
val formatEnd: SimpleDateFormat
if (type == TIME_STYLE_ONT) {
formatEnd = SimpleDateFormat("MM-dd", Locale.getDefault())
} else if (type == TIME_STYLE_TWO) {
formatEnd = SimpleDateFormat("MM.dd", Locale.getDefault())
} else if (type == TIME_STYLE_THREE) {
formatEnd = SimpleDateFormat("MM dd", Locale.getDefault())
} else if (type == TIME_STYLE_FOUR) {
formatEnd = SimpleDateFormat("MM/dd", Locale.getDefault())
} else if (type == TIME_STYLE_FIVE) {
formatEnd = SimpleDateFormat("MM月dd日", Locale.getDefault())
} else {
formatEnd = SimpleDateFormat("MM/dd", Locale.getDefault())
}
val dateEnd = Date(tiem)
return formatEnd.format(dateEnd)
} else {
return ""
}
}
}
}