使用Databinding給xml的時(shí)間等數(shù)據(jù)代碼報(bào)紅

在使用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 ""
            }
        }
    }
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容