Using Java 8 java.time.LocalDate, can you calculate the Chinese New Year date?

Using Java 8 java.time.LocalDate , can you calculate the Chinese New Year date?

+5
source share
2 answers

Probably no.

Oracle documentation claims that this feature uses the Gregorian calendar system. Chinese New Year is based on the Chinese calendar and the lunar calendar. ( Source )

+1
source

Of course, it is impossible without an external library, because standard Java does not contain a Chinese calendar. Calculating the Chinese New Year requires complex astronomical calculations, and standard Java also does not support astronomy.

As a workaround, use my Time4J library (v4.35), and then write the following code:

 LocalDate gregorian = LocalDate.now(ZoneId.of("Asia/Shanghai")); ChineseCalendar cc = ChineseCalendar.ofNewYear(gregorian.getYear()); LocalDate newYearAsGregorian = cc.transform(PlainDate.axis()).toTemporalAccessor(); System.out.println( newYearAsGregorian); // 2018-02-16 System.out.println( "Year of the " + cc.get(ChineseCalendar.YEAR_OF_CYCLE).getZodiac(Locale.ENGLISH)); // Year of the Dog 

See also API for details.

0
source

All Articles