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)
}
Android 通过时间戳获取农历日期、星座、干支、属相
于 2024-06-14 16:58:26 首次发布