日本日期验证 - 比较 [英] Japanese Date Validation - Comparison

查看:184
本文介绍了日本日期验证 - 比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要验证用户输入的日期

I need to validate Japanese date entered by user.

假设用户选择 ERA - >输入DOB为 YY - MM - dd 格式。

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

在服务器端,我收到输入日期 ERA 由用户选择。

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

now我需要验证所选ERA的输入日期,如果日期属于特定ERA期间。

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

我知道Java Calendar API支持这个,还有类 JapaneseImperialCalendar ,但我不知道如何使用它,虽然它在内部使用它。

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.

这就是我现在所做的。

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());
    }

输出

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

假设用户输入,选择的ERA为 SHOWA ,预期期限为 1926-1989

suppose user enters , selected ERA is SHOWA, expected period is 1926–1989

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

再次

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

因此需要构建一些逻辑以使用ERA验证用户输入日期

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

推荐答案

首先:

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

返回给定的显示名称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"))

你会得到一个不错的

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

Java 日历不验证您是否设置了与该时代相对应的合理字段,但您可以请求 Calendar#getActualMaximum(YEAR)在给定时代的日历实例上获得最大合理年份。然后,您可以切换到那一年找到最大合理月份并重复查找最大合理日期。

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));
    }
}

打印

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

这篇关于日本日期验证 - 比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆