使用Databinding给xml的时间等数据代码报红

在使用DataBinding的时候我们在xml上想格式化时间戳变成我们想要的格式,这时候就需要一个我们自己自定的工具类了,比如Object XXX 或者 一个companion object写的伴生单例

比如这样的格式,在xml是无法使用

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


其实就是需要你告诉jvm这个方法是静态的

        ---> @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 ""
            }
        }

以下是小弟随便写的一个时间类 ,自己用

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 ""
            }
        }
    }
}

你可能感兴趣的:(使用Databinding给xml的时间等数据代码报红)