Formatting Chinese converted date by time4j to Gregorian in Java

AliDK :

I am working on a project in Java and I need to change different types of dates to each other like (Gregorian to Chinese and vice versa and Gregorian to Hijri and vice versa). I try to use Time4j which has this facility to convert different types of dates to each other but unfortunately, I didn't understand how to work with this library even after I tried to read its documentation.

Right now I am trying to convert Gregorian date to Chinese date but I can't get the normal year in Chinese date when I use object.getYear() it returns cyclic year but I need normal year like 2019.

This is my code:

import net.time4j.PlainDate;
import net.time4j.calendar.ChineseCalendar;
public class DateConverter {

static PlainDate gregorian = PlainDate.nowInSystemTime();
static ChineseCalendar obj = gregorian.transform(ChineseCalendar.axis());

public static void main(String[] args) {

    String result = obj.getMonth() + "/" + obj.getDayOfMonth() + "/" + obj.getYear() ;

    System.out.println(result);         // 8/30/ji-hai(36)
    System.out.println(obj);            // chinese[ji-hai(2019)-8-30]
}
}

I need this type of year ji-hai(2019) and it is in default toString() method and when I use object.getYear() method it returns ji-hai(36).

Now how can I change my return value from ji-hai(36) to ji-hai(2019). Also if I want to receive the Gregorian date from a user how should I prepared for this conversion? Can I receive it as a String text and cast it to Gregorian date and then use it? Is is possible?

Also if you know a good resource that explains the usage of classes and methods of Time4j with example please share it with me.

thank you.

Meno Hochschild :

Short solution:

Instead of trying to rely on the toString()-methods of various types like CyclicYear etc., I recommend to use a dedicated format engine which is capable of handling Chinese calendars. Time4J has its own format engine based on the class ChronoFormatter:

    ChineseCalendar obj = PlainDate.nowInSystemTime().transform(ChineseCalendar.axis());

    ChronoFormatter<ChineseCalendar> f =
        ChronoFormatter.ofPattern(
          "d/M/U(r)", 
          PatternType.CLDR, 
          Locale.ROOT, 
          ChineseCalendar.axis());

    System.out.println(f.print(obj));   
    // output today: 1/9/ji-hai(2019)

Pattern and Chinese years:

I have used the pattern "d/M/U(r)". The "normal" year of the Chinese calendar IS the cyclic year represented by the CLDR pattern symbol "U". Cycliy years repeat after 60 years starting a new cycle. Any other definition of year is clearly unusual. This might astonish western users because they often expect that there is any kind of proleptic year counting just in a forward manner. However, Time4J also supports two other kinds of years. One is the year of era which is now only of historic interest. And the other type of year is the related gregorian year (using the pattern symbol "r"). Its main purpose is just to make the cyclic year unambigous. Hence many people like to structure Chinese years as a combination of cyclic year and related gregorian year in this form:

U(r) or r(U)

It is even possible to determine the Chinese zodiac which repeats after only 12 years. Given you have a Chinese calendar object, you can determine the zodiac this way:

    String zodiac = obj.getYear().getZodiac(Locale.ENGLISH);
    System.out.println("Year of " + zodiac); // Year of Pig

Reverse way (parsing and transforming to gregorian):

Just use the formatter defined above and then parse:

    PlainDate gregorian = f.parse("1/9/ji-hai(2019)").transform(PlainDate.axis());
    System.out.println(gregorian); // output: 2019-09-29

About transcriptions:

I have used the root locale for constructing the formatter. It produces the cyclic year in simple latin letters without any diacritic marks. If you might want to use the official pinyin transcription you can apply the English locale instead. The cyclic year would then appear as "jǐ-hài". Special transcriptions using other scripts are supported for the languages Chinese (same script also for Korean and Japanese), Vietnamese and Russian.

The API-documentation of Time4J can be found online. The doc already contains examples for formatting and parsing the Chinese calendar. But I welcome any further contributions to it or concrete suggestions to improve it.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=307495&siteId=1