Calendar的getActualMinimum和getGreatestMinimum方法之间有什么区别? [英] What's the difference between Calendar's getActualMinimum and getGreatestMinimum methods?

查看:419
本文介绍了Calendar的getActualMinimum和getGreatestMinimum方法之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于 getActualMinimum() getGreatestMinimum()将为每个字段返回相同的值,因此确切

Since getActualMinimum() and getGreatestMinimum() are going to return the same value for every field what's the exact difference?

推荐答案

getActualMinimum() 方法返回一个属性的最小可能值可能有。例如, Calendar.DAY_OF_MONTH 永远不会小于1,因为没有月份以0或更短的一天开始。

The getActualMinimum() method returns the minimum possible value that an attribute could possibly have. For example, Calendar.DAY_OF_MONTH is never less than 1, since no month starts with a day numbered 0 or less.

a href = https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#getGreatestMinimum(int) rel = nofollow noreferrer> getGreatestMinimum( ) 方法返回 getActualMinimum()可能具有的最大可能值。尽管在大多数情况下,这些值将始终相同(几个月几乎总是以1开头),但是代码在某些罕见情况下允许它们有所不同。对于数据验证方案,使用 getGreatestMinimum()是合适的。

The getGreatestMinimum() method returns the maximum possible value that getActualMinimum() could ever have. While in most cases these values will always be the same (months nearly always start with 1), the code allows for them to be different in some rare circumstances. Using getGreatestMinimum() is appropriate for data validation scenarios.

一个可能发生(确实发生)的地方是跳过日期或日历中的不连续性。例如,Java的 GregorianCalendar 在转换日期之前在儒略历中实现日期,在该日期之后实现阳历,因此在日历中的日期之间存在几天的间隔根本不存在。

One place this can (and does) occur is when there are skipped dates or discontinuities in a calendar. Java's GregorianCalendar, for example implements dates in the Julian calendar before a cutover date, and the Gregorian calendar after that date, and thus has a gap of several days where dates in the calendar simply don't exist.

日历 getGreatestMinimum()方法是抽象的,因此必需要由子类实现。 JRE对 GregorianCalendar 的实现表明,如果跳过日期与 DAY_OF_MONTH 字段不同,则JRE可能会有所不同。交接月的月份不包括该月的第一天:

The Calendar class getGreatestMinimum() method is abstract, so it is required to be implemented by a subclass. The JRE's implementation for GregorianCalendar shows that it could differ on the DAY_OF_MONTH field if the "skipped days" on the cutover month don't include the first of the month:

public int getGreatestMinimum(int field) {
    if (field == DAY_OF_MONTH) {
        BaseCalendar.Date d = getGregorianCutoverDate();
        long mon1 = getFixedDateMonth1(d, gregorianCutoverDate);
        d = getCalendarDate(mon1);
        return Math.max(MIN_VALUES[field], d.getDayOfMonth());
    }
    return MIN_VALUES[field];
}

以此为线索,可以使用 setGregorianChange()设置朱利安到格里高利安变更的转换日期。默认情况下是1582年10月15日(从1582年10月4日起跳过了10天)。不同的语言环境在不同的时间切换,例如,英国在1752年9月14日(9月2日之后的一天)切换。

Using this as a cue, you can use setGregorianChange() to set the cutover date for Julian-to-Gregorian change. By default it is 15 October 1582 (having skipped 10 days from 4 October 1582). Different Locales switched at different times, for example Great Britain switched on 14 September 1752 (the day after 2 September).

通过将此转换设置为一个月中的某个日期,从而可以跳过这一天

By setting this cutover to a date in a month that makes the skipped days overlap the first of the month, we can generate these edge cases.

一个实际的基于区域设置的不连续点,其真实交接日期可能是罗马尼亚,其中1919年3月31日之后的那一天是14 1919年4月。因此在罗马尼亚语的语言环境中,1919年4月仅包括14到30天,而 getGreatestMin()将返回14。

An actual locale-based discontinuity with a real cutover date is probably Romania, in which the day after 31 March 1919 was 14 April 1919. So in a Romanian locale, the month of April 1919 would only include days from 14 to 30, and getGreatestMin() will return 14.

因为我没有安装该语言环境,所以可以更改转换日期以模拟会发生的情况:

Since I don't have that locale installed I can change the cutover date to simulate what would happen:

GregorianCalendar cal = new GregorianCalendar();
// Deprecated, but an easy way of showing this example
cal.setGregorianChange(new Date(1911, Calendar.APRIL, 14));
System.out.println("actual min = " + cal.getActualMinimum(cal.DAY_OF_MONTH));
System.out.println("greatest min = " + cal.getGreatestMinimum(cal.DAY_OF_MONTH));

输出:

actual min = 1
greatest min = 14

如果使用 Date(Long .MIN_VALUE)作为割据,给出纯阳历日历没有差距。但是在那种情况下,时间开始是指时间。是在公历月的16号,因此在这种情况下,最大的最小值是16。

Another edge case exists if you use Date(Long.MIN_VALUE) as the cutover, giving a "pure Gregorian" calendar with no gaps. But in that case, the "beginning of time" is on the 16th of the month (in the Gregorian calendar), so greatest min in that case is 16.

其他日历系统可能具有相似的不连续性和边缘情况。

Other calendar systems may have similar discontinuities and edge cases.

这篇关于Calendar的getActualMinimum和getGreatestMinimum方法之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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