Japanese Date Check Comparison

I need to confirm the Japanese date entered by the user.

Let's say the user selects ERA→ Enter DOB as a format YY - MM - dd.

On the server side, I get input dateand ERAselected by the user.

Now I need to check the entered date with the selected ERA if the date falls in the period of a specific ERA or not.

I know that there is support in the Java Calendar API, there is also a class JapaneseImperialCalendar, but I cannot figure out how to use it, although it uses it internally.

This is what I have done so far.

public static void main(String[] args) {
        Locale locale = new Locale("ja", "JP", "JP");
        Calendar now = Calendar.getInstance(locale);
        System.out.println(now.get(Calendar.ERA));
        Map<String, Integer> names = now.getDisplayNames(Calendar.ERA, Calendar.LONG, locale);
        System.out.println(names);
        System.out.printf("It is year %tY of the current era%n", now);
        System.out.printf("The calendar class is: %s%n", now.getClass().getName());
    }

Output

4
{??=3, ??=4, ??=2, ??=1, ??=0}
It is year 0026 of the current era
The calendar class is: java.util.JapaneseImperialCalendar

Suppose user logs in, selected ERA SHOWA, expected period1926–1989

YY   MM    DD
34   05    28  // which is valid date

again

YY   MM    DD
62   12    28  // which should be invalid date

Therefore, you need to create some logic to check the user input date using ERA

+4
1

:

now.getDisplayNames(Calendar.ERA, Calendar.LONG, locale)

, . , ??, , , , .

now.getDisplayNames(Calendar.ERA, Calendar.LONG, new Locale("en"))

{Heisei=4, Taisho=2, Meiji=1, Showa=3}

Java Calendar , "", , Calendar#getActualMaximum(YEAR) , . , , .

:

public static void main(String[] args) {
    Locale locale = new Locale("ja", "JP", "JP");
    Calendar now = Calendar.getInstance(locale);
    Map<String, Integer> ears = now.getDisplayNames(Calendar.ERA, Calendar.LONG, new Locale("en"));
    for (Map.Entry<String, Integer> era : ears.entrySet()) {
        Integer eraIndex = era.getValue();
        String eraName = era.getKey();
        System.out.printf("Era #%d [%s]%n", eraIndex, eraName);
        now.set(Calendar.ERA, eraIndex);
        now.set(Calendar.YEAR, 1);
        now.set(Calendar.DAY_OF_YEAR, 1);
        System.out.printf("Actual max year in era is %d%n", now.getActualMaximum(Calendar.YEAR));
    }
}

Era #4 [Heisei]
Actual max year in era is 292277005 // current era
Era #2 [Taisho]
Actual max year in era is 15
Era #1 [Meiji]
Actual max year in era is 44
Era #3 [Showa]
Actual max year in era is 63
+1

Source: https://habr.com/ru/post/1568644/


All Articles