Android 通过时间戳获取农历日期、星座、干支、属相

import android.icu.util.ChineseCalendar
import android.os.Build
import androidx.annotation.RequiresApi
import java.util.*
import kotlin.math.abs

object LunarCalendarUtils {
    private val animals = listOf("鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪")
    private val ganZhiCycle = listOf("甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸")
    private val zhiCycle = listOf("子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥")


    @RequiresApi(Build.VERSION_CODES.N)
    fun convertSolarToLunar(timestamp: Long): LunarInfo? {
        val calendar = Calendar.getInstance()
        calendar.timeInMillis = timestamp
        val year = calendar.get(Calendar.YEAR)
        val month = calendar.get(Calendar.MONTH) + 1
        val day = calendar.get(Calendar.DAY_OF_MONTH)

        val chineseCalendar = ChineseCalendar(Date(timestamp))
        chineseCalendar.timeInMillis = timestamp

        val lunarYear =   chineseCalendar.get(ChineseCalendar.EXTENDED_YEAR)-2637
        val lunarMonth = chineseCalendar.get(ChineseCalendar.MONTH) + 1
        val lunarDay = chineseCalendar.get(ChineseCalendar.DAY_OF_MONTH)

        val animal = animals[(year - 4) % 12]
        val ganIndex = (year - 4) % 10
        val zhiIndex = (year - 4) % 12
        val ganZhiYear = "${ganZhiCycle[ganIndex]}${zhiCycle[zhiIndex]}"
        val constellation = getConstellation(month, day)

        return LunarInfo(lunarYear, lunarMonth, lunarDay, animal, ganZhiYear,constellation)
    }

    fun getZodiac(year: Int): String = animals[(year - 4) % 12]

    fun getGanZhiYear(year: Int): String {
        val ganIndex = (year - 4) % 10
        val zhiIndex = (year - 4) % 12
        return "${ganZhiCycle[ganIndex]}${zhiCycle[zhiIndex]}"
    }


    // 根据公历日期计算星座
    fun getConstellation(month : Int, day : Int): String {
        return  when {
            (month == 1 && day >= 20) || (month == 2 && day <= 17) -> "水瓶座"
            (month == 2 && day >= 18) || (month == 3 && day <= 19) -> "双鱼座"
            (month == 3 && day >= 20) || (month == 4 && day <= 19) -> "白羊座"
            (month == 4 && day >= 20) || (month == 5 && day <= 20) -> "金牛座"
            (month == 5 && day >= 21) || (month == 6 && day <= 20) -> "双子座"
            (month == 6 && day >= 21) || (month == 7 && day <= 22) -> "巨蟹座"
            (month == 7 && day >= 23) || (month == 8 && day <= 22) -> "狮子座"
            (month == 8 && day >= 23) || (month == 9 && day <= 22) -> "处女座"
            (month == 9 && day >= 23) || (month == 10 && day <= 22) -> "天秤座"
            (month == 10 && day >= 23) || (month == 11 && day <= 21) -> "天蝎座"
            (month == 11 && day >= 22) || (month == 12 && day <= 21) -> "射手座"
            (month == 12 && day >= 22) || (month == 1 && day <= 19) -> "摩羯座"
            else -> "无法确定的星座日期"
        }
    }
     
    //将月日转为中文农历表示
    fun lunarDayInChinese(month:Int ,dayOfMonth: Int): String {
        val chineseNumberCharacters = listOf("零", "一", "二", "三", "四", "五", "六", "七", "八", "九")

        val lunarMonths = listOf("正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "冬", "腊")
        val specialPrefixes = listOf("初", "", "十", "廿", "卅")

        val month = lunarMonths[month-1]

        val tenPlace = dayOfMonth / 10
        val onePlace = dayOfMonth % 10
        val day = when {
            dayOfMonth <= 10 -> "${specialPrefixes[tenPlace]}${if (onePlace > 0) chineseNumberCharacters[onePlace] else ""}"
            else -> "${specialPrefixes[tenPlace + 1]}${chineseNumberCharacters[onePlace]}"
        }
        return "${month}月 $day"
    }
    data class LunarInfo(val year: Int, val month: Int, val day: Int, val animal: String, val ganZhiYear: String,val constellation :String)

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小趴菜8227

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值