package comlunarcalendar;import javatimeLocalDate;public class LunarCalendarConverter private static final String LUNAR_MONTHS = 正 二 三 四 五 六 七 八 九 十 冬 腊 ; private static final String LUNAR_DAY
The LunarCalendarConverter class is a utility class that converts a given Gregorian (Western) calendar date to its corresponding lunar (Chinese) calendar date.
The class contains the following constants:
- LUNAR_MONTHS: An array of strings representing the names of the lunar months.
- LUNAR_DAYS: An array of strings representing the names of the lunar days.
- LUNAR_DAY_COUNTS: An array of integers representing the number of days in each lunar month.
- LUNAR_DIGITS: An array of strings representing the Chinese characters for the digits 0-9.
- BASE_YEAR: An integer representing the base year of the lunar calendar (1900).
The class also contains a main method that gets the current Gregorian date, calculates the corresponding lunar date, and prints both dates.
The main method performs the following steps:
- Gets the current Gregorian date using the LocalDate.now() method.
- Extracts the year, month, and day from the Gregorian date.
- Checks if the year is within the supported range (1900-2100).
- Calculates the lunar year by subtracting the base year from the Gregorian year.
- Retrieves the lunar data for the lunar year.
- Extracts the high and low bytes from the lunar data.
- Retrieves the leap month and leap day from the high byte.
- Checks if the Gregorian year is a leap year.
- Calculates the number of leap months before the current month.
- Adjusts the current month if it is after the leap month.
- Calculates the lunar month by counting the set bits in the low byte.
- Sets the lunar day to the Gregorian day.
- Adds the leap day to the lunar day if the current month is the leap month.
- Converts the lunar day to its Chinese representation.
- Prints the Gregorian date and the lunar date.
原文地址: https://www.cveoy.top/t/topic/igJ1 著作权归作者所有。请勿转载和采集!