Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

代码中的一个疑问 #43

Open
Felis-nigripes opened this issue Jul 3, 2024 · 2 comments
Open

代码中的一个疑问 #43

Felis-nigripes opened this issue Jul 3, 2024 · 2 comments

Comments

@Felis-nigripes
Copy link

首先非常感谢您提供了这么好的一个库,我有个疑问:
在 Solar 的构造方法中,为什么将 MILLISECOND 清除呢,是会有什么隐藏问题吗?

public Solar(Calendar calendar) {
calendar.set(Calendar.MILLISECOND, 0);
......
}

其次想请问下,在Lunar类中考虑增加 nextYear() 和 nextMonth() 方法吗?
非常感谢

@6tail
Copy link
Owner

6tail commented Jul 4, 2024

第一个问题,因为日历用不到毫秒级别,只需要到秒精度就足够了。
第二个问题,精力有限,lunar自认为设计不合理,以后只修复bug,不会再新增功能了,以后主要维护tyme。当然,lunar已经没什么显而易见的bug了。

@Felis-nigripes
Copy link
Author

非常感谢您的回复
我写了一段代码,主要功能是在一个时间戳基础上加n个农历年,然后返回增加后的结果日期的时间戳
但是我对农历方面的知识很欠缺,想请您帮忙看看这段代码是否存在什么问题,非常感谢您!
代码如下:

// 代码是Koltin写的,我尽量保持其一些写法与Java一致
fun addYear(milli: Long, amount: Int = 0): Long {
        val calendar = Calendar.getInstance()
        calendar.setTimeInMillis(milli)

        val solar = Solar.fromCalendar(calendar)
        val lunar = solar.getLunar()
        // 增加年份
        val amountLunarYear = LunarYear.fromYear(lunar.getYear() + amount)
        // 处理月份,在增加年份后,判断原来的月份在新的年份中是否存在
        val amountLunarYearMonths: List<Int> = amountLunarYear.getMonthsInYear().map { lunarMonth ->
            lunarMonth.getMonth() 
        }
        val validMonth = if (lunar.getMonth() in amountLunarYearMonths) {
            lunar.getMonth()
        } else if (-lunar.getMonth() in amountLunarYearMonths) {
            -lunar.getMonth()
        } else {
            amountLunarYearMonths[0]
        }
        val amountLunarMonth = LunarMonth.fromYm(amountLunarYear.getYear(), validMonth)
        // 处理日子,在处理完月份后,判断原来的Day是否在新的月份范围内
        val amountLunarMonthDayCount = amountLunarMonth.getDayCount()
        val validDay = if (lunar.getDay() > amountLunarMonthDayCount) {
            amountLunarMonthDayCount
        } else {
            lunar.getDay()
        }
        val newLunar = Lunar.fromYmd(amountLunarMonth.getYear(), amountLunarMonth.getMonth(), validDay)
        val amountSolar = newLunar.getSolar()

        calendar.set(Calendar.YEAR, amountSolar.getYear())
        calendar.set(Calendar.MONTH, amountSolar.getMonth() - 1)
        calendar.set(Calendar.DATE, amountSolar.getDay())
        return calendar.getTimeInMillis()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants