Search code examples
androidkotlincalendar

Chinese New Year Date on Android


How one could get Chinese New Year Date on Android?

Since API level 24 Android has Chinese Calendar class. https://developer.android.com/reference/android/icu/util/ChineseCalendar

However, doing it like this returns wrong date (Feb 12 for 2023).

    val chinese = ChineseCalendar.getInstance()
    chinese.set(ChineseCalendar.MONTH, 0)
    chinese.set(ChineseCalendar.DAY_OF_MONTH, 1)

Solution

  • I was able to get Gregorian date for Chinese new year in the following way. Getting Chinese calendar is done using simple instantiation ChineseCalendar(). No need to call getInstance().

        val chinese = ChineseCalendar()
        chinese.set(ChineseCalendar.MONTH, 0)
        chinese.set(ChineseCalendar.DAY_OF_MONTH, 1)
        println("chinese " + chinese.time.toString())
    

    In the logs I got

    chinese Sun Jan 22 13:24:41 GMT+02:00 2023

    You can also add year to get next new year date, like this

        chinese.add(ChineseCalendar.YEAR, 1)
    

    and get

    chinese Sat Feb 10 13:27:41 GMT+02:00 2024