Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Japanese Date Validation - Comparison

I need to validate Japanese date entered by user.

Let say user selects the ERA -> Enter DOB as YY - MM - dd format.

At server side, i receive input date and ERA selected by user.

now i need to validate entered date with the selected ERA, if the date falls in the period of particular ERA or not.

i know there is support for this in Java Calendar API, Also there is class JapaneseImperialCalendar, but i cannot get any clue how to use it, though it uses it internally.

This is what i did till now.

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 enters , selected ERA is SHOWA, expected period is 1926–1989

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

again

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

So need to build some logic to validate user input date with ERA

like image 375
Ankur Singhal Avatar asked Dec 22 '14 16:12

Ankur Singhal


1 Answers

First of all: the

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

returns the display names in the given locale, which in your case is Japanese. So the ?? marks you are getting are valid Japanese names of the eras, it's just that your console cannot print them. Change to

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

and you will get a nice

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

Java Calendar does not validate if you are setting the fields "reasonably" corresponding to the era, but you can request Calendar#getActualMaximum(YEAR) on an instance of a calendar in a given era to get the maximum reasonable year. Then, you can switch to that year to find the maximum reasonable month and repeat to find the maximum reasonable day.

Here's a little showcase:

public static void main(String[] args) {
    Locale locale = new Locale("ja", "JP", "JP");
    Calendar now = Calendar.getInstance(locale);
    Map<String, Integer> eras = now.getDisplayNames(Calendar.ERA, Calendar.LONG, new Locale("en"));
    for (Map.Entry<String, Integer> era : eras.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));
    }
}

prints

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
like image 87
Adam Michalik Avatar answered Oct 06 '22 19:10

Adam Michalik