Android 时间工具类 kotlin版本

前言:为方便调用使用时间的工具类花了一些时间改造成kt版本
通过扩展函数,部分使用infix来代替Util,
使用详情大部分可以看注释,最底下有main方法,可以自己跑跑看看

/**
 * @auther :Gy
 * @date: 2022/11/14
 * java 调取方式 GyDateUtilKt.getDateYMD(long:Long)
 * kt   调取方式 在本类最下面的main方法即可查看和测试
 * xml 导入包方式 '包名.GyDateUtilKT' 在xml中使用和java使用的方式是一样的
 * <import type="com.oxgrass.cut.reform.GyDateUtilKT" />
 *
 *  <TextView
 *      android:id="@+id/my_time"
 *      android:layout_width="wrap_content"
 *      android:layout_height="wrap_content"
 *      android:text='@{myFragmentStates.userInfo.vipGrade>0?"会员到期日期:"+GyDateUtilKT.getDateYMD(myFragmentStates.userInfo.vipExpireDate):"暂未开通会员"}'
 *      android:textSize="12sp" />
 */
@file:JvmName("GyDateUtilKT")

package 改成你自己的包名地址

import android.annotation.SuppressLint
import java.text.SimpleDateFormat
import java.util.*
import kotlin.math.abs

private val getTimeError = "获取日期异常"
private val ymdhms = "yyyy-MM-dd HH:mm:ss"
private val ymdhms2 = "yyyyMMddHHmmss"
private val hms = "HH:mm:ss"
private val hm = "HH:mm"
private val ymd = "yyyy-MM-dd" // ?压脉带?
private val am = "AM" // 上午
private val pm = "PM" // 下午


@SuppressLint("SimpleDateFormat")
fun getDataYMD() = System.currentTimeMillis().getDateYMD()

@SuppressLint("SimpleDateFormat")
fun getDataYMDHMS() = System.currentTimeMillis().getDateYMDHMS()

/**
 * 直接通过long 获取转换为yyyy-MM-dd格式的时间
 */
@SuppressLint("SimpleDateFormat")
fun Long.getDateYMD(): String {
    val sdf = SimpleDateFormat(ymd)
    sdf.timeZone = TimeZone.getTimeZone("GMT+8")
    return try {
        sdf.format(this)
    } catch (e: Exception) {
        sdf.format(System.currentTimeMillis())
    }
}

/**
 * 直接通过long 获取转换为yyyy-MM-dd HH:mm:ss格式的时间
 */
@SuppressLint("SimpleDateFormat")
fun Long.getDateYMDHMS(): String {
    val sdf = SimpleDateFormat(ymdhms)
    sdf.timeZone = TimeZone.getTimeZone("GMT+8")
    return try {
        sdf.format(this)
    } catch (e: Exception) {
        sdf.format(System.currentTimeMillis())
    }
}

@SuppressLint("SimpleDateFormat")
fun Long.getDateHM(): String {
    val sdf = SimpleDateFormat(hm)
    sdf.timeZone = TimeZone.getTimeZone("GMT+8")
    return try {
        sdf.format(this)
    } catch (e: Exception) {
        sdf.format(System.currentTimeMillis())
    }
}

/**
 * 自定义获取返回的类型
 */
fun getDate(type: Int): String {
    return System.currentTimeMillis().getDate(type)
}

/**
 * 自定义返回的类型
 * 1 :[ymdhms]
 * 2 :[hms]
 * 3 :[hm]
 */
@SuppressLint("SimpleDateFormat")
fun Long.getDate(type: Int = 1): String {
    val sdf = if (type == 1) {
        SimpleDateFormat(ymdhms)
    } else if (type == 2) {
        SimpleDateFormat(hms)
    } else if (type == 3) {
        SimpleDateFormat(hm)
    } else {
        SimpleDateFormat(ymdhms)
    }
    sdf.timeZone = TimeZone.getTimeZone("GMT+8")
    return try {
        sdf.format(this)
    } catch (e: Exception) {
        sdf.format(System.currentTimeMillis())
    }
}

// 时间转换为long 格式要求为 ymdhms
@SuppressLint("SimpleDateFormat")
fun String.changeToYMDHMSString(): String =
    "${SimpleDateFormat(ymdhms).parse(this)?.time ?: getTimeError}"

@SuppressLint("SimpleDateFormat")
fun String.changeToYMDString(): String =
    "${SimpleDateFormat(ymd).parse(this)?.time ?: getTimeError}"

@SuppressLint("SimpleDateFormat")
fun String.changeToHMString(): String =
    "${SimpleDateFormat(hm).parse(this)?.time ?: getTimeError}"

/**
 * 转换时间为long
 * 需要格式为 ymdhms 要知道每个信息才可以正确转换为long
 */
@SuppressLint("SimpleDateFormat")
fun String.changeYMDHMSTimeToLong(): Long {
    val simple = SimpleDateFormat(ymdhms)
    val date = simple.parse(this)
    return date.time
}

/**
 * string 秒换算为时分秒
 */
fun String.secondChangeHourMinuteSecond(needHour: Boolean = true) =
    this.toInt().secondChangeHourMinuteSecond(needHour)

/**
 * int 秒换算为时分秒
 */
fun Int.secondChangeHourMinuteSecond(needHour: Boolean = true): String {
    var h = 0
    var d = 0
    var s = 0
    val temp = this % 3600
    if (this > 3600) {
        h = this / 3600
        if (temp != 0) {
            if (temp > 60) {
                d = temp / 60
                if (temp % 60 != 0) {
                    s = temp % 60
                }
            } else {
                s = temp
            }
        }
    } else { // 小于1小时
        // 当前时间除以60 获取当前是多少分钟
        d = this / 60
        if (this % 60 != 0) {
            s = this % 60
        }
    }
    val hStrings = if (h < 10) {
        "0${h}"
    } else {
        "$h"
    }
    val dStrings = if (d < 10) {
        "0${d}"
    } else {
        "$d"
    }
    val sStrings = if (s < 10) {
        "0${s}"
    } else {
        "$s"
    }
    return if (needHour)
        "${hStrings}:${dStrings}:${sStrings}"
    else
        "${dStrings}:${sStrings}"
}


/**
 * 计算两个日期所差的天数
 * 返回为int = 0 则同一天
 *          < 0 为前一天
 * GyDateUtilKT.differDay(c1.getTimeInMillis(), c2.getTimeInMillis());
 * infix 在java中调用和正常调用是一样的
 */
infix fun Long.differDay(milliseconds2: Long): Int {
    val dar1 = Calendar.getInstance()
    dar1.timeInMillis = this
    val dar2 = Calendar.getInstance()
    dar2.timeInMillis = milliseconds2
    // 先判断是否同年
    val y1 = dar1.get(Calendar.YEAR)
    val y2 = dar2.get(Calendar.YEAR)
    val d1 = dar1.get(Calendar.DAY_OF_YEAR)
    val d2 = dar2.get(Calendar.DAY_OF_YEAR)
    return if (y1 - y2 > 0) {
        d1 - d2 + dar2.getActualMaximum(Calendar.DAY_OF_YEAR)
    } else if (y1 - y2 < 0) {
        d1 - d2 - dar1.getActualMaximum(Calendar.DAY_OF_YEAR)
    } else {
        d1 - d2
    }
}

/**
 * 计算两个日期所差的小时数
 * GyDateUtilKT.differHours(c1.getTimeInMillis(), c2.getTimeInMillis());
 * infix 在java中调用和正常调用是一样的
 */
infix fun Long.differHours(date2: Long): Int {
    val dar1 = Calendar.getInstance()
    dar1.timeInMillis = this
    val dar2 = Calendar.getInstance()
    dar2.timeInMillis = date2
    val h1: Int = dar1.get(Calendar.HOUR_OF_DAY)
    val h2: Int = dar2.get(Calendar.HOUR_OF_DAY)
    val day = this differDay date2
    return h1 - h2 + day * 24
}

/**
 * 计算两个日期所差的分钟数
 * GyDateUtilKT.differMinutes(c1.getTimeInMillis(), c2.getTimeInMillis());
 * infix 在java中调用和正常调用是一样的
 */
infix fun Long.differMinutes(date2: Long): Int {
    val dar1 = Calendar.getInstance()
    dar1.timeInMillis = this
    val dar2 = Calendar.getInstance()
    dar2.timeInMillis = date2
    val m1: Int = dar1.get(Calendar.MINUTE)
    val m2: Int = dar2.get(Calendar.MINUTE)
    val h = this differHours date2
    return m1 - m2 + h * 60
}

/**
 * 获取周几
 * Monday周一 以下方法以此类推
 * 带时分秒的获取出来的日期 他的日期都是你某个时间段调用的日期
 */
fun getMondayYMDHMS() = Calendar.MONDAY.getDayOfWeek(ymdhms)
fun getTuesdayYMDHMS() = Calendar.TUESDAY.getDayOfWeek(ymdhms)
fun getWenesdayYMDHMS() = Calendar.WEDNESDAY.getDayOfWeek(ymdhms)
fun getThursdayYMDHMS() = Calendar.THURSDAY.getDayOfWeek(ymdhms)
fun getFridayYMDHMS() = Calendar.FRIDAY.getDayOfWeek(ymdhms)
fun getSaturdayYMDHMS() = Calendar.SATURDAY.getDayOfWeek(ymdhms)
fun getSundayYMDHMS() = Calendar.SUNDAY.getDayOfWeek(ymdhms)

fun getMondayYMD() = Calendar.MONDAY.getDayOfWeek(ymd)
fun getTuesdayYMD() = Calendar.TUESDAY.getDayOfWeek(ymd)
fun getWenesdayYMD() = Calendar.WEDNESDAY.getDayOfWeek(ymd)
fun getThursdayYMD() = Calendar.THURSDAY.getDayOfWeek(ymd)
fun getFridayYMD() = Calendar.FRIDAY.getDayOfWeek(ymd)
fun getSaturdayYMD() = Calendar.SATURDAY.getDayOfWeek(ymd)
fun getSundayYMD() = Calendar.SUNDAY.getDayOfWeek(ymd)

/**
 * 获取本月的第一天
 */
@SuppressLint("SimpleDateFormat")
fun getFirstDayInMonth(): String {
    return tryCatch {
        val c = GregorianCalendar()
        val simple = SimpleDateFormat(ymd)
        c.set(GregorianCalendar.DAY_OF_MONTH, 1)
        simple.format(c.time)
    }
}

/**
 * 获取本月最后一天
 */
@SuppressLint("SimpleDateFormat")
fun getLastDayInMonth() = tryCatch {
    val c = GregorianCalendar()
    val simple = SimpleDateFormat(ymd)
    c.set(Calendar.DATE, 1)
    c.roll(Calendar.DATE, -1)
    simple.format(c.time)
}



/**
 * 获取本周的某一天
 */
@SuppressLint("SimpleDateFormat")
private fun Int.getDayOfWeek(format: String): String {
    return tryCatch {
        val c = GregorianCalendar()
        val simple = SimpleDateFormat(format)
        val week = c.get(Calendar.DAY_OF_WEEK)
        if (week == this) {
            simple.format(c.time)
        } else {
            var offectDay = this - week
            if (this == Calendar.SUNDAY) {
                offectDay = 7 - abs(offectDay)
            }
            c.add(Calendar.DATE, offectDay)
            simple.format(c.time)
        }
    }
}

/**
 * 检测当前是否是闰年
 * (year能被4整除 并且 不能被100整除) 或者 year能被400整除,则该年为闰年.
 */
fun Int.checkIsLeapYear() = this % 4 == 0 && this % 400 != 0 || this % 400 == 0

fun String.checkIsLeapYear() = this.toInt().checkIsLeapYear()

/**
 * 根据当前日期判断上午还是下午
 */
@SuppressLint("SimpleDateFormat")
fun String.getTimeQuantumYMDHMS(): String {
    val date = SimpleDateFormat(ymdhms).parse(this) ?: return getTimeError
    return if (date.hours >= 12) pm else am
}

/**
 * 严格要求时间格式为[ymdhms]
 */
@SuppressLint("SimpleDateFormat")
fun String.getDynamicTime(): String {
    val simple = SimpleDateFormat(ymdhms)
    val c1 = Calendar.getInstance()
    val c2 = Calendar.getInstance()
    return tryCatch {
        c1.time = Date()
        c2.time = simple.parse(this)
        val d = c1.timeInMillis differDay c2.timeInMillis
        if (d == 0) {
            val h = c1.timeInMillis differHours c2.timeInMillis
            if (h > 0) {
                return "今天${getStringByFormat(2)}"
                // return "${h}小时前"
            } else if (h < 0) {
                // return "${abs(h)}小时后"
            } else if (h == 0) {
                val m = c1.timeInMillis differMinutes c2.timeInMillis
                if (m > 0) {
                    return "${m}分钟前"
                } else if (m < 0) {
                    // return "${abs(m)}分钟后"
                } else {
                    return "刚刚"
                }
            }
        } else if (d > 0) {
            if (d == 1) {
                return "昨天${getStringByFormat(2)}"
            } else if (d == 2) {
                return "前天${getStringByFormat(2)}"
            } else {
                return "${getStringByFormat()}"
            }
        } else if (d < 0) {
            if (d == -1) {
                //  return "明天${getStringByFormat()}"
            } else if (d == -2) {
                // return "后天${getStringByFormat()}"
            } else {
                // return "${abs(d)}天后${getStringByFormat()}"
            }
        }
        getStringByFormat()
    }


}

/**
 * @param type 要返回数据格式类型: 需要传入一个YMDHMS 会返回 type类型的
 */
@SuppressLint("SimpleDateFormat")
fun String.getStringByFormat(type: Int = 1): String {
    val long = this.changeYMDHMSTimeToLong()
    return long.getDate(type)
}

@SuppressLint("SimpleDateFormat")
fun String.getTimeQuantumHMS(): String {
    val date = SimpleDateFormat(hms).parse(this) ?: return getTimeError
    return if (date.hours >= 12) pm else am
}


/**
 * 获取距离当天晚上00:00:00的时间
 * 分割方式以':'冒号分割
 * 可以用解构去解析
 *
 */
fun getTodayLastTime(): String {
    return tryCatch {
        val simpleDateFormat = SimpleDateFormat(ymdhms2)
        val currentTime = simpleDateFormat.format(Date())
        val substring = currentTime.substring(0, 8) + "235959"
        val nowTime = simpleDateFormat.parse(currentTime).time / 1000
        val endTime = simpleDateFormat.parse(substring).time / 1000
        val totalTime = endTime - nowTime
        val hour = Math.floor((totalTime / 3600).toDouble()).toInt()
        val minute = Math.floor(((totalTime - hour * 3600) / 60).toDouble()).toInt()
        val second = Math.floor((totalTime - hour * 3600 - minute * 60).toDouble()).toInt()
        "$hour:$minute:$second"
    }
}


private inline fun tryCatch(block: () -> String): String {
    return try {
        block()
    } catch (e: Exception) {
        e.printStackTrace()
        getTimeError
    }
}

fun main(args: Array<String>) {
    println("guo:${3701.secondChangeHourMinuteSecond()}")
    println("guo:${"3701".secondChangeHourMinuteSecond()}")
    println("guo:${61.secondChangeHourMinuteSecond()}")
    println("guo:${60.secondChangeHourMinuteSecond()}")
    println("guo:${2.secondChangeHourMinuteSecond()}")
    println("guo:${0.secondChangeHourMinuteSecond()}")

    val nowTime = System.currentTimeMillis()
    println("guo:${nowTime} ----${nowTime.getDateYMD()}")
    println("guo:${nowTime} ----${nowTime.getDateHM()}")

    println("guo:${nowTime} ----${nowTime.getDateYMDHMS().changeToYMDHMSString()}")
    // 因转换的数据没有时分秒 所以数据和有时分秒的数据是有偏差的
    println("guo:${nowTime} ----${nowTime.getDateYMD().changeToYMDString()}")

    println("guo:${nowTime} ----${nowTime.getDateYMD().changeToYMDString()}")
    println("guo---------------------------")

    println(getMondayYMDHMS())
    println(getTuesdayYMDHMS())
    println(getWenesdayYMDHMS())
    println(getThursdayYMDHMS())
    println(getFridayYMD())
    println(getSaturdayYMD())
    println(getSundayYMD())
    println("guo---------------------------")
    println("2020".checkIsLeapYear())
    println("2013".checkIsLeapYear())
    println("guo---------------------------")
    println("20:22:10".getTimeQuantumHMS())
    println("2022-11-15 10:22:10".getTimeQuantumYMDHMS())
    println("guo---------------------------")
    println("动态时间${"2023-01-18 01:30:02".getDynamicTime()}")

}




  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值