Fix package name
X-SVN-Rev: 2944
This commit is contained in:
parent
0458a59216
commit
de33014a47
@ -5,8 +5,8 @@
|
||||
*******************************************************************************
|
||||
*
|
||||
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/util/BuddhistCalendar.java,v $
|
||||
* $Date: 2000/10/17 18:26:44 $
|
||||
* $Revision: 1.5 $
|
||||
* $Date: 2000/11/18 00:30:54 $
|
||||
* $Revision: 1.6 $
|
||||
*
|
||||
*****************************************************************************************
|
||||
*/
|
||||
@ -35,6 +35,7 @@ import java.util.Locale;
|
||||
* @see com.ibm.util.GregorianCalendar
|
||||
*
|
||||
* @author Laura Werner
|
||||
* @author Alan Liu
|
||||
*/
|
||||
public class BuddhistCalendar extends GregorianCalendar {
|
||||
|
||||
@ -99,8 +100,8 @@ public class BuddhistCalendar extends GregorianCalendar {
|
||||
* @param date The date to which the new calendar is set.
|
||||
*/
|
||||
public BuddhistCalendar(Date date) {
|
||||
super(TimeZone.getDefault(), Locale.getDefault());
|
||||
this.setTime(date);
|
||||
this();
|
||||
setTime(date);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -148,124 +149,42 @@ public class BuddhistCalendar extends GregorianCalendar {
|
||||
// take care of that....
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Override of the <code>GregorianCalendar</code> method that computes the
|
||||
* fields such as <code>YEAR</code>, <code>MONTH</code>, and <code>DATE</code>
|
||||
* from the date in milliseconds since 1/1/1970 AD.
|
||||
*
|
||||
* This method calls {@link GregorianCalendar#computeFields} to do most
|
||||
* of the work,
|
||||
* then adjusts the {@link #YEAR YEAR} and {@link #ERA ERA} fields to use the
|
||||
* Buddhist Era rather than the Gregorian {@link #AD AD} or {@link #BC BC}.
|
||||
*/
|
||||
protected void computeFields() {
|
||||
// Let GregorianCalendar do its thing.
|
||||
super.computeFields();
|
||||
|
||||
// Now adjust the year and era to the proper Buddhist values
|
||||
fromGregorian();
|
||||
|
||||
//
|
||||
// If we're in strict mode and the year is less than 1, fail.
|
||||
// But do this after setting both the year and era to the right values
|
||||
// anyway, so that this object is in a consistent state.
|
||||
//
|
||||
if (!isLenient() && fields[YEAR] < 1) {
|
||||
throw new IllegalArgumentException("Time before start of Buddhist era");
|
||||
}
|
||||
}
|
||||
// Starts in -543 AD, ie 544 BC
|
||||
private static final int BUDDHIST_ERA_START = -543;
|
||||
|
||||
/**
|
||||
* Override of the <code>GregorianCalendar</code> method that computes the
|
||||
* elapsed time in milliseconds since 1/1/1970 AD from the fields such
|
||||
* as <code>YEAR</code>, <code>MONTH</code>, and <code>DATE</code>.
|
||||
*
|
||||
* This method adjusts the {@link #YEAR YEAR} and {@link #ERA ERA} from their
|
||||
* values in the Buddhist calendar to the corresponding Gregorian values, calls
|
||||
* {@link GregorianCalendar#computeTime} to do the real millisecond
|
||||
* calculation, and then restores the Buddhist <code>YEAR</code> and
|
||||
* <code>ERA</code>.
|
||||
*/
|
||||
protected void computeTime() {
|
||||
int year = fields[YEAR];
|
||||
int era = fields[ERA];
|
||||
|
||||
if (!isLenient()) {
|
||||
if (era != BE) {
|
||||
throw new IllegalArgumentException("Illegal value for ERA");
|
||||
}
|
||||
if (year < 1) {
|
||||
throw new IllegalArgumentException("YEAR must be greater than 0");
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
toGregorian();
|
||||
super.computeTime();
|
||||
}
|
||||
finally {
|
||||
// Set the year and era back to the Buddhist values, even if
|
||||
// GregorianCalendar fails because other fields are invalid.
|
||||
fields[YEAR] = year;
|
||||
fields[ERA] = era;
|
||||
}
|
||||
}
|
||||
|
||||
public void add(int field, int amount) {
|
||||
toGregorian();
|
||||
try {
|
||||
super.add(field, amount);
|
||||
}
|
||||
finally {
|
||||
fromGregorian();
|
||||
}
|
||||
}
|
||||
|
||||
public void roll(int field, int amount) {
|
||||
toGregorian();
|
||||
try {
|
||||
super.roll(field, amount);
|
||||
}
|
||||
finally {
|
||||
fromGregorian();
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Methods for converting between Gregorian and Buddhist calendars
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
private static int BUDDHIST_ERA_START = -543; // Starts in -543 AD, ie 544 BC
|
||||
|
||||
/**
|
||||
* Convert the YEAR and ERA fields from Buddhist to Gregorian values
|
||||
* Return the (Buddhist) value of the YEAR field on input;
|
||||
*/
|
||||
private void toGregorian() {
|
||||
int year = fields[YEAR];
|
||||
|
||||
if (year > 0) {
|
||||
fields[YEAR] = year + BUDDHIST_ERA_START;
|
||||
fields[ERA] = AD;
|
||||
protected int handleGetExtendedYear() {
|
||||
int year;
|
||||
if (newerField(EXTENDED_YEAR, YEAR) == EXTENDED_YEAR) {
|
||||
year = internalGet(EXTENDED_YEAR, 1);
|
||||
} else {
|
||||
fields[YEAR] = 1 - year - BUDDHIST_ERA_START;
|
||||
fields[ERA] = BC;
|
||||
// Ignore the era, as there is only one
|
||||
year = internalGet(YEAR, 1);
|
||||
}
|
||||
return year;
|
||||
}
|
||||
|
||||
|
||||
// Return JD of start of given month/year
|
||||
protected int handleComputeMonthStart(int eyear, int month) {
|
||||
return super.handleComputeMonthStart(eyear + BUDDHIST_ERA_START, month);
|
||||
}
|
||||
|
||||
protected void handleComputeFields(int julianDay) {
|
||||
super.handleComputeFields(julianDay);
|
||||
int y = internalGet(EXTENDED_YEAR) - BUDDHIST_ERA_START;
|
||||
internalSet(EXTENDED_YEAR, y);
|
||||
internalSet(ERA, 0);
|
||||
internalSet(YEAR, y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adjust the year and era from Gregorian to Buddhist values
|
||||
* Override GregorianCalendar. There is only one Buddhist ERA. We
|
||||
* should really handle YEAR, YEAR_WOY, and EXTENDED_YEAR here too to
|
||||
* implement the 1..5000000 range, but it's not critical.
|
||||
*/
|
||||
private void fromGregorian() {
|
||||
// Now adjust the year and era to the proper Buddhist values
|
||||
int year = fields[YEAR];
|
||||
|
||||
if (fields[ERA] == BC) {
|
||||
fields[YEAR] = 1 - year - BUDDHIST_ERA_START;
|
||||
} else {
|
||||
fields[YEAR] = year - BUDDHIST_ERA_START;
|
||||
protected int handleGetLimit(int field, int limitType) {
|
||||
if (field == ERA) {
|
||||
return BE;
|
||||
}
|
||||
fields[ERA] = BE;
|
||||
return super.handleGetLimit(field, limitType);
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
@ -5,8 +5,8 @@
|
||||
*******************************************************************************
|
||||
*
|
||||
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/util/HebrewCalendar.java,v $
|
||||
* $Date: 2000/10/27 22:25:52 $
|
||||
* $Revision: 1.5 $
|
||||
* $Date: 2000/11/18 00:30:54 $
|
||||
* $Revision: 1.6 $
|
||||
*
|
||||
*****************************************************************************************
|
||||
*/
|
||||
@ -64,6 +64,7 @@ import java.util.Locale;
|
||||
* @see com.ibm.util.GregorianCalendar
|
||||
*
|
||||
* @author Laura Werner
|
||||
* @author Alan Liu
|
||||
*/
|
||||
public class HebrewCalendar extends Calendar {
|
||||
|
||||
@ -124,35 +125,31 @@ public class HebrewCalendar extends Calendar {
|
||||
*/
|
||||
private static final long EPOCH_MILLIS = -180799862400000L; // 1/1/1 HY
|
||||
|
||||
// Useful millisecond constants
|
||||
private static final int SECOND_MS = 1000;
|
||||
private static final int MINUTE_MS = 60*SECOND_MS;
|
||||
private static final int HOUR_MS = 60*MINUTE_MS;
|
||||
private static final long DAY_MS = 24*HOUR_MS;
|
||||
private static final long WEEK_MS = 7*DAY_MS;
|
||||
|
||||
/**
|
||||
* The minimum and maximum values for all of the fields, for validation
|
||||
*/
|
||||
private static final int MinMax[][] = {
|
||||
// Min Greatest Min Least Max Max
|
||||
{ 0, 0, 0, 0 }, // ERA
|
||||
{ 1, 1, 5000000, 5000000 }, // YEAR
|
||||
{ 0, 0, 12, 12 }, // MONTH
|
||||
{ 0, 0, 51, 56 }, // WEEK_OF_YEAR
|
||||
{ 0, 0, 5, 6 }, // WEEK_OF_MONTH
|
||||
{ 1, 1, 29, 30 }, // DAY_OF_MONTH
|
||||
{ 1, 1, 353, 385 }, // DAY_OF_YEAR
|
||||
{ 1, 1, 7, 7 }, // DAY_OF_WEEK
|
||||
{ -1, -1, 4, 6 }, // DAY_OF_WEEK_IN_MONTH
|
||||
{ 0, 0, 1, 1 }, // AM_PM
|
||||
{ 0, 0, 11, 11 }, // HOUR
|
||||
{ 0, 0, 23, 23 }, // HOUR_OF_DAY
|
||||
{ 0, 0, 59, 59 }, // MINUTE
|
||||
{ 0, 0, 59, 59 }, // SECOND
|
||||
{ 0, 0, 999, 999 }, // MILLISECOND
|
||||
{ -12*HOUR_MS, -12*HOUR_MS, 12*HOUR_MS, 12*HOUR_MS }, // ZONE_OFFSET
|
||||
{ 0, 0, 1*HOUR_MS, 1*HOUR_MS },
|
||||
private static final int LIMITS[][] = {
|
||||
// Minimum Greatest Least Maximum
|
||||
// Minimum Maximum
|
||||
{ 0, 0, 0, 0 }, // ERA
|
||||
{ 1, 1, 5000000, 5000000 }, // YEAR
|
||||
{ 0, 0, 12, 12 }, // MONTH
|
||||
{ 1, 1, 51, 56 }, // WEEK_OF_YEAR
|
||||
{ 0, 0, 5, 6 }, // WEEK_OF_MONTH
|
||||
{ 1, 1, 29, 30 }, // DAY_OF_MONTH
|
||||
{ 1, 1, 353, 385 }, // DAY_OF_YEAR
|
||||
{/* */}, // DAY_OF_WEEK
|
||||
{ -1, -1, 4, 6 }, // DAY_OF_WEEK_IN_MONTH
|
||||
{/* */}, // AM_PM
|
||||
{/* */}, // HOUR
|
||||
{/* */}, // HOUR_OF_DAY
|
||||
{/* */}, // MINUTE
|
||||
{/* */}, // SECOND
|
||||
{/* */}, // MILLISECOND
|
||||
{/* */}, // ZONE_OFFSET
|
||||
{/* */}, // DST_OFFSET
|
||||
{ -5000001, -5000001, 5000001, 5000001 }, // YEAR_WOY
|
||||
{/* */}, // DOW_LOCAL
|
||||
{ -5000000, -5000000, 5000000, 5000000 }, // EXTENDED_YEAR
|
||||
{/* */}, // JULIAN_DAY
|
||||
{/* */}, // MILLISECONDS_IN_DAY
|
||||
};
|
||||
|
||||
/**
|
||||
@ -184,7 +181,7 @@ public class HebrewCalendar extends Calendar {
|
||||
* Although this can be calculated from the MONTH_LENGTH table,
|
||||
* keeping it around separately makes some calculations a lot faster
|
||||
*/
|
||||
private static final int NUM_DAYS[][] = {
|
||||
private static final int MONTH_START[][] = {
|
||||
// Deficient Normal Complete
|
||||
{ 0, 0, 0 }, // (placeholder)
|
||||
{ 30, 30, 30 }, // Tishri
|
||||
@ -205,7 +202,7 @@ public class HebrewCalendar extends Calendar {
|
||||
/**
|
||||
* The cumulative # of days to the end of each month in a leap year
|
||||
*/
|
||||
private static final int LEAP_NUM_DAYS[][] = {
|
||||
private static final int LEAP_MONTH_START[][] = {
|
||||
// Deficient Normal Complete
|
||||
{ 0, 0, 0 }, // (placeholder)
|
||||
{ 30, 30, 30 }, // Tishri
|
||||
@ -227,14 +224,6 @@ public class HebrewCalendar extends Calendar {
|
||||
// Data Members...
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Since TimeZone rules are all defined in terms of GregorianCalendar,
|
||||
* we need a GregorianCalendar object for doing time zone calculations
|
||||
* There's no point in lazy-allocating this since it's needed for
|
||||
* almost anything this class does.
|
||||
*/
|
||||
private static GregorianCalendar gregorian = new GregorianCalendar();
|
||||
|
||||
private static CalendarCache cache = new CalendarCache();
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
@ -340,99 +329,6 @@ public class HebrewCalendar extends Calendar {
|
||||
this.set(SECOND, second);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Minimum / Maximum access functions
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Returns the minimum value for the given field.
|
||||
* e.g. for DAY_OF_MONTH, 1
|
||||
*
|
||||
* @param field The field whose minimum value is desired.
|
||||
*
|
||||
* @see com.ibm.util.Calendar#getMinimum
|
||||
*/
|
||||
public int getMinimum(int field)
|
||||
{
|
||||
return MinMax[field][0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the highest minimum value for the given field. For the Hebrew
|
||||
* calendar, this always returns the same result as <code>getMinimum</code>.
|
||||
*
|
||||
* @param field The field whose greatest minimum value is desired.
|
||||
*
|
||||
* @see #getMinimum
|
||||
*/
|
||||
public int getGreatestMinimum(int field)
|
||||
{
|
||||
return MinMax[field][1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the maximum value for the given field.
|
||||
* e.g. for {@link #DAY_OF_MONTH DAY_OF_MONTH}, 30
|
||||
*
|
||||
* @param field The field whose maximum value is desired.
|
||||
*
|
||||
* @see #getLeastMaximum
|
||||
* @see #getActualMaximum
|
||||
*/
|
||||
public int getMaximum(int field)
|
||||
{
|
||||
return MinMax[field][3];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the lowest maximum value for the given field. For most fields,
|
||||
* this returns the same result as {@link #getMaximum getMaximum}. However,
|
||||
* for some fields this can be a lower number. For example,
|
||||
* the maximum {@link #DAY_OF_MONTH DAY_OF_MONTH} in the Hebrew caleandar varies
|
||||
* from month to month, so this method returns 29 while <code>getMaximum</code>
|
||||
* returns 30.
|
||||
*
|
||||
* @param field The field whose least maximum value is desired.
|
||||
*
|
||||
* @see #getMaximum
|
||||
* @see #getActualMaximum
|
||||
*/
|
||||
public int getLeastMaximum(int field)
|
||||
{
|
||||
return MinMax[field][2];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the maximum value that a field could have, given the current date.
|
||||
* For example, with the date "Kislev 3, 5757" and the {@link #DAY_OF_MONTH DAY_OF_MONTH} field,
|
||||
* the actual maximum would be 29; for "Kislev 3, 5758" it would be 30,
|
||||
* since the length of the month Kislev varies from year to year.
|
||||
*
|
||||
* @param field The field whose actual maximum value is desired.
|
||||
*
|
||||
* @see #getMaximum
|
||||
* @see #getLeastMaximum
|
||||
*/
|
||||
public int getActualMaximum(int field)
|
||||
{
|
||||
if (!isSet(YEAR) || !isSet(MONTH)) {
|
||||
complete();
|
||||
}
|
||||
switch (field) {
|
||||
case MONTH:
|
||||
return isLeapYear(fields[YEAR]) ? 13 : 12;
|
||||
|
||||
case DAY_OF_MONTH:
|
||||
return monthLength(fields[YEAR], fields[MONTH]);
|
||||
|
||||
case DAY_OF_YEAR:
|
||||
return yearLength(fields[YEAR]);
|
||||
|
||||
default:
|
||||
return super.getActualMaximum(field);
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Rolling and adding functions overridden from Calendar
|
||||
//
|
||||
@ -469,34 +365,70 @@ public class HebrewCalendar extends Calendar {
|
||||
public void add(int field, int amount)
|
||||
{
|
||||
switch (field) {
|
||||
case MONTH:
|
||||
case MONTH:
|
||||
{
|
||||
//
|
||||
// MONTH is tricky, because the number of months per year varies
|
||||
// It's easiest to just convert to an absolute # of months
|
||||
// since the epoch, do the addition, and convert back.
|
||||
//
|
||||
int month = (235 * get(YEAR) - 234) / 19 + get(MONTH);
|
||||
month += amount;
|
||||
//~ // This seems like a good idea but it isn't working :( - Alan
|
||||
|
||||
// Now convert back to year and month values
|
||||
int year = (19 * month + 234) / 235;
|
||||
month -= (235 * year - 234) / 19;
|
||||
|
||||
// In a non-leap year, months after the (missing) leap month
|
||||
// must be bumped up by one.
|
||||
// TODO: but only if we started before the leap month
|
||||
if (month >= ADAR_1 && !isLeapYear(year)) {
|
||||
month++;
|
||||
//~ // MONTH is tricky, because the number of months per year varies
|
||||
//~ // It's easiest to just convert to an absolute # of months
|
||||
//~ // since the epoch, do the addition, and convert back.
|
||||
//~ int month = (235 * get(YEAR) - 234) / 19 + get(MONTH);
|
||||
//~ month += amount;
|
||||
|
||||
//~ // Now convert back to year and month values
|
||||
//~ int year = (19 * month + 234) / 235;
|
||||
//~ month -= (235 * year - 234) / 19;
|
||||
//~
|
||||
//~ // In a non-leap year, months after the (missing) leap month
|
||||
//~ // must be bumped up by one.
|
||||
//~ // TODO: but only if we started before the leap month
|
||||
//~ if (month >= ADAR_1 && !isLeapYear(year)) {
|
||||
//~ month++;
|
||||
//~ }
|
||||
//~ set(YEAR, year);
|
||||
//~ set(MONTH, month);
|
||||
//~ pinField(DAY_OF_MONTH);
|
||||
|
||||
// We can't just do a set(MONTH, get(MONTH) + amount). The
|
||||
// reason is ADAR_1. Suppose amount is +2 and we land in
|
||||
// ADAR_1 -- then we have to bump to ADAR_2 aka ADAR. But
|
||||
// if amount is -2 and we land in ADAR_1, then we have to
|
||||
// bump the other way -- down to SHEVAT. - Alan 11/00
|
||||
int month = get(MONTH);
|
||||
int year = get(YEAR);
|
||||
//public static final int SHEVAT = 4;
|
||||
//public static final int ADAR_1 = 5;
|
||||
//public static final int ADAR = 6;
|
||||
if (amount > 0) {
|
||||
while (amount-- > 0) {
|
||||
++month;
|
||||
if (month == ADAR_1 && !isLeapYear(year)) {
|
||||
++month;
|
||||
}
|
||||
if (month > ELUL) { // Last month of year
|
||||
month = 0;
|
||||
++year;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
while (amount++ < 0) {
|
||||
--month;
|
||||
if (month == ADAR_1 && !isLeapYear(year)) {
|
||||
--month;
|
||||
}
|
||||
if (month < 0) {
|
||||
month = ELUL; // Last month of year
|
||||
--year;
|
||||
}
|
||||
}
|
||||
}
|
||||
this.set(YEAR, year);
|
||||
this.set(MONTH, month);
|
||||
|
||||
set(MONTH, month);
|
||||
set(YEAR, year);
|
||||
pinField(DAY_OF_MONTH);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
|
||||
default:
|
||||
super.add(field, amount);
|
||||
break;
|
||||
}
|
||||
@ -536,18 +468,17 @@ public class HebrewCalendar extends Calendar {
|
||||
public void roll(int field, int amount)
|
||||
{
|
||||
switch (field) {
|
||||
case MONTH:
|
||||
case MONTH:
|
||||
{
|
||||
int month = get(MONTH);
|
||||
int year = get(YEAR);
|
||||
|
||||
boolean leapYear = isLeapYear(year);
|
||||
int yearLength = leapYear ? 13 : 12;
|
||||
int yearLength = monthsInYear(year);
|
||||
int newMonth = month + (amount % yearLength);
|
||||
//
|
||||
// If it's not a leap year and we're rolling past the missing month
|
||||
// of ADAR_1, we need to roll an extra month to make up for it.
|
||||
// TODO: fix cases like Av + 12 -> Tammuz
|
||||
//
|
||||
if (!leapYear) {
|
||||
if (amount > 0 && month < ADAR_1 && newMonth >= ADAR_1) {
|
||||
@ -560,385 +491,13 @@ public class HebrewCalendar extends Calendar {
|
||||
pinField(DAY_OF_MONTH);
|
||||
return;
|
||||
}
|
||||
default:
|
||||
default:
|
||||
super.roll(field, amount);
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Functions for converting from field values to milliseconds and back...
|
||||
//
|
||||
// These are overrides of abstract methods on com.ibm.util.Calendar
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Converts time field values to UTC as milliseconds.
|
||||
*
|
||||
* @exception IllegalArgumentException if an unknown field is given.
|
||||
*/
|
||||
protected void computeTime()
|
||||
{
|
||||
if (isTimeSet) return;
|
||||
|
||||
if (!isLenient() && !validateFields())
|
||||
throw new IllegalArgumentException("Invalid field values for HebrewCalendar");
|
||||
|
||||
if (isSet(ERA) && internalGet(ERA) != 0)
|
||||
throw new IllegalArgumentException("ERA out of range in HebrewCalendar");
|
||||
|
||||
// The year is required. We don't have to check if it's unset,
|
||||
// because if it is, by definition it will be 0.
|
||||
|
||||
int year = internalGet(YEAR);
|
||||
long dayNumber = 0, date = 0;
|
||||
|
||||
if (year <= 0) {
|
||||
throw new IllegalArgumentException("YEAR out of range in HebrewCalendar");
|
||||
}
|
||||
|
||||
// The following code is somewhat convoluted. The various nested
|
||||
// if's handle the different cases of what fields are present.
|
||||
if (isSet(MONTH) &&
|
||||
(isSet(DATE) ||
|
||||
(isSet(DAY_OF_WEEK) &&
|
||||
(isSet(WEEK_OF_MONTH) || isSet(DAY_OF_WEEK_IN_MONTH))
|
||||
)))
|
||||
{
|
||||
// We have the month specified. Make it 1-based for the algorithm.
|
||||
int month = internalGet(MONTH);
|
||||
|
||||
// normalize month
|
||||
// TODO: I think this is wrong, since months/year can vary
|
||||
if (month < 0) {
|
||||
year += month / 13 - 1;
|
||||
month = 13 + month % 13;
|
||||
} else if (month > 12) {
|
||||
year += month / 13;
|
||||
month = month % 13;
|
||||
}
|
||||
|
||||
dayNumber = startOfYear(year);
|
||||
if (isLeapYear(year)) {
|
||||
dayNumber += LEAP_NUM_DAYS[month][yearType(year)];
|
||||
} else {
|
||||
dayNumber += NUM_DAYS[month][yearType(year)];
|
||||
}
|
||||
|
||||
if (isSet(DATE))
|
||||
{
|
||||
date = internalGet(DATE);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Compute from day of week plus week number or from the day of
|
||||
// week plus the day of week in month. The computations are
|
||||
// almost identical.
|
||||
|
||||
// Find the day of the week for the first of this month. This
|
||||
// is zero-based, with 0 being the locale-specific first day of
|
||||
// the week. Add 1 to get the 1st day of month. Subtract
|
||||
// getFirstDayOfWeek() to make 0-based.
|
||||
int fdm = absoluteDayToDayOfWeek(dayNumber + 1) - getFirstDayOfWeek();
|
||||
if (fdm < 0) fdm += 7;
|
||||
|
||||
// Find the start of the first week. This will be a date from
|
||||
// 1..-6. It represents the locale-specific first day of the
|
||||
// week of the first day of the month, ignoring minimal days in
|
||||
// first week.
|
||||
date = 1 - fdm + internalGet(DAY_OF_WEEK) - getFirstDayOfWeek();
|
||||
|
||||
if (isSet(WEEK_OF_MONTH))
|
||||
{
|
||||
// Adjust for minimal days in first week.
|
||||
if ((7 - fdm) < getMinimalDaysInFirstWeek()) date += 7;
|
||||
|
||||
// Now adjust for the week number.
|
||||
date += 7 * (internalGet(WEEK_OF_MONTH) - 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Adjust into the month, if needed.
|
||||
if (date < 1) date += 7;
|
||||
|
||||
// We are basing this on the day-of-week-in-month. The only
|
||||
// trickiness occurs if the day-of-week-in-month is
|
||||
// negative.
|
||||
int dim = internalGet(DAY_OF_WEEK_IN_MONTH);
|
||||
if (dim >= 0) {
|
||||
date += 7*(dim - 1);
|
||||
} else {
|
||||
// Move date to the last of this day-of-week in this
|
||||
// month, then back up as needed. If dim==-1, we don't
|
||||
// back up at all. If dim==-2, we back up once, etc.
|
||||
// Don't back up past the first of the given day-of-week
|
||||
// in this month. Note that we handle -2, -3,
|
||||
// etc. correctly, even though values < -1 are
|
||||
// technically disallowed.
|
||||
date += ((monthLength(year, fields[MONTH]) - date) / 7 + dim + 1) * 7;
|
||||
}
|
||||
}
|
||||
}
|
||||
dayNumber += date;
|
||||
}
|
||||
else if (isSet(DAY_OF_YEAR)) {
|
||||
dayNumber = startOfYear(year) + internalGet(DAY_OF_YEAR);
|
||||
}
|
||||
else if (isSet(DAY_OF_WEEK) && isSet(WEEK_OF_YEAR))
|
||||
{
|
||||
dayNumber = startOfYear(year);
|
||||
|
||||
// Compute from day of week plus week of year
|
||||
|
||||
// Find the day of the week for the first of this year. This
|
||||
// is zero-based, with 0 being the locale-specific first day of
|
||||
// the week. Add 1 to get the 1st day of month. Subtract
|
||||
// getFirstDayOfWeek() to make 0-based.
|
||||
int fdy = absoluteDayToDayOfWeek(dayNumber + 1) - getFirstDayOfWeek();
|
||||
if (fdy < 0) fdy += 7;
|
||||
|
||||
// Find the start of the first week. This may be a valid date
|
||||
// from 1..7, or a date before the first, from 0..-6. It
|
||||
// represents the locale-specific first day of the week
|
||||
// of the first day of the year.
|
||||
|
||||
// First ignore the minimal days in first week.
|
||||
date = 1 - fdy + internalGet(DAY_OF_WEEK) - getFirstDayOfWeek();
|
||||
|
||||
// Adjust for minimal days in first week.
|
||||
if ((7 - fdy) < getMinimalDaysInFirstWeek()) date += 7;
|
||||
|
||||
// Now adjust for the week number.
|
||||
date += 7 * (internalGet(WEEK_OF_YEAR) - 1);
|
||||
|
||||
dayNumber += date;
|
||||
}
|
||||
else { // Not enough information
|
||||
throw new IllegalArgumentException("Not enough fields set to calculate time");
|
||||
}
|
||||
|
||||
long millis = dayNumber * DAY_MS + EPOCH_MILLIS;
|
||||
|
||||
// Now we can do the time portion of the conversion.
|
||||
int millisInDay = 0;
|
||||
|
||||
// Hours
|
||||
if (isSet(HOUR_OF_DAY))
|
||||
// Don't normalize here; let overflow bump into the next period.
|
||||
// This is consistent with how we handle other fields.
|
||||
millisInDay += internalGet(HOUR_OF_DAY);
|
||||
|
||||
else if (isSet(HOUR))
|
||||
{
|
||||
// Don't normalize here; let overflow bump into the next period.
|
||||
// This is consistent with how we handle other fields.
|
||||
millisInDay += internalGet(HOUR);
|
||||
millisInDay += 12 * internalGet(AM_PM);
|
||||
}
|
||||
|
||||
// Minutes. We use the fact that unset == 0
|
||||
millisInDay *= 60;
|
||||
millisInDay += internalGet(MINUTE);
|
||||
|
||||
// Seconds. unset == 0
|
||||
millisInDay *= 60;
|
||||
millisInDay += internalGet(SECOND);
|
||||
|
||||
// Milliseconds. unset == 0
|
||||
millisInDay *= 1000;
|
||||
millisInDay += internalGet(MILLISECOND);
|
||||
|
||||
// Now add date and millisInDay together, to make millis contain local wall
|
||||
// millis, with no zone or DST adjustments
|
||||
millis += millisInDay;
|
||||
|
||||
//
|
||||
// Compute the time zone offset and DST offset.
|
||||
// Since the TimeZone API expects the Gregorian year, month, etc.,
|
||||
// We have to convert to local Gregorian time in order to
|
||||
// figure out the time zone calculations. This is a bit slow, but
|
||||
// it saves us from doing some *really* nasty calculations here.
|
||||
//
|
||||
TimeZone zone = getTimeZone();
|
||||
int dstOffset = 0;
|
||||
|
||||
if (zone.useDaylightTime()) {
|
||||
synchronized(gregorian) {
|
||||
gregorian.setTimeZone(zone);
|
||||
gregorian.setTime(new Date(millis)); // "millis" is local wall clock time
|
||||
dstOffset = gregorian.get(DST_OFFSET);
|
||||
}
|
||||
}
|
||||
// Store our final computed GMT time, with timezone adjustments.
|
||||
time = millis - dstOffset - zone.getRawOffset();
|
||||
isTimeSet = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the value of the given time field.
|
||||
*/
|
||||
private boolean boundsCheck(int value, int field)
|
||||
{
|
||||
return value >= getMinimum(field) && value <= getMaximum(field);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Validates the values of the set time fields.
|
||||
*/
|
||||
private boolean validateFields()
|
||||
{
|
||||
for (int field = 0; field < FIELD_COUNT; field++)
|
||||
{
|
||||
// Ignore DATE and DAY_OF_YEAR which are handled below
|
||||
if (field != DATE &&
|
||||
field != DAY_OF_YEAR &&
|
||||
isSet(field) &&
|
||||
!boundsCheck(internalGet(field), field))
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Values differ in Least-Maximum and Maximum should be handled
|
||||
// specially.
|
||||
if (isSet(DATE))
|
||||
{
|
||||
int date = internalGet(DATE);
|
||||
return (date >= getMinimum(DATE) &&
|
||||
date <= monthLength(fields[YEAR], fields[MONTH]));
|
||||
}
|
||||
|
||||
if (isSet(DAY_OF_YEAR))
|
||||
{
|
||||
int days = internalGet(DAY_OF_YEAR);
|
||||
|
||||
if (days < 1 || days > yearLength(internalGet(YEAR)))
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isSet(YEAR))
|
||||
{
|
||||
int year = internalGet(YEAR);
|
||||
if (year < 1)
|
||||
return false;
|
||||
}
|
||||
|
||||
// Handle DAY_OF_WEEK_IN_MONTH, which must not have the value zero.
|
||||
// We've checked against minimum and maximum above already.
|
||||
if (isSet(DAY_OF_WEEK_IN_MONTH) &&
|
||||
0 == internalGet(DAY_OF_WEEK_IN_MONTH)) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the time as milliseconds since 1/1/1970 to the Calendar fields
|
||||
* such as YEAR, MONTH and DAY.
|
||||
*/
|
||||
protected void computeFields()
|
||||
{
|
||||
if (areFieldsSet) return;
|
||||
|
||||
// The following algorithm only works for dates from the start of the Hebrew
|
||||
// calendar onward.
|
||||
if (time < EPOCH_MILLIS && !isLenient()) {
|
||||
throw new IllegalArgumentException("HebrewCalendar does not handle dates before 1/1/1 AM");
|
||||
}
|
||||
|
||||
//
|
||||
// Compute the time zone offset and DST offset.
|
||||
// Since the TimeZone API expects the Gregorian year, month, etc.,
|
||||
// We have to convert to local Gregorian time in order to
|
||||
// figure out the time zone calculations. This is a bit slow, but
|
||||
// it saves us from doing some *really* nasty calculations here.
|
||||
//
|
||||
TimeZone zone = getTimeZone();
|
||||
int rawOffset = zone.getRawOffset();
|
||||
int dstOffset = 0; // Extra DST offset
|
||||
|
||||
if (zone.useDaylightTime()) {
|
||||
synchronized(gregorian) {
|
||||
gregorian.setTimeZone(zone);
|
||||
gregorian.setTime(new Date(time));
|
||||
dstOffset = gregorian.get(DST_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
long localMillis = time + rawOffset + dstOffset;
|
||||
|
||||
// We need to find out which Hebrew year the given time is in.
|
||||
// Once we know that, we find the time when the year started,
|
||||
// and everything else is straightforward
|
||||
|
||||
long epochMillis = localMillis - EPOCH_MILLIS; // Millis since epoch
|
||||
long d = epochMillis / DAY_MS; // Days
|
||||
long m = (d * DAY_PARTS) / MONTH_PARTS; // Months (approx)
|
||||
|
||||
int year = (int)((19 * m + 234) / 235) + 1; // Years (approx)
|
||||
long ys = startOfYear(year); // 1st day of year
|
||||
int dayOfYear = (int)(d - ys);
|
||||
|
||||
// Because of the postponement rules, it's possible to guess wrong. Fix it.
|
||||
while (dayOfYear < 1) {
|
||||
year--;
|
||||
ys = startOfYear(year);
|
||||
dayOfYear = (int)(d - ys);
|
||||
}
|
||||
|
||||
int dayOfWeek = absoluteDayToDayOfWeek((long)d);
|
||||
|
||||
// Now figure out which month we're in, and the date within that month
|
||||
int yearType = yearType(year);
|
||||
int numDays[][] = isLeapYear(year) ? LEAP_NUM_DAYS : NUM_DAYS;
|
||||
|
||||
int month = 0;
|
||||
while (dayOfYear > numDays[month][yearType]) {
|
||||
month++;
|
||||
}
|
||||
month--;
|
||||
int date = dayOfYear - numDays[month][yearType];
|
||||
|
||||
fields[ERA] = 0;
|
||||
fields[YEAR] = year;
|
||||
fields[MONTH] = month;
|
||||
fields[DATE] = date;
|
||||
|
||||
fields[DAY_OF_YEAR] = dayOfYear;
|
||||
fields[DAY_OF_WEEK] = dayOfWeek;
|
||||
|
||||
fields[WEEK_OF_YEAR] = weekNumber(dayOfYear, dayOfWeek);
|
||||
fields[WEEK_OF_MONTH] = weekNumber(date, dayOfWeek);
|
||||
|
||||
fields[DAY_OF_WEEK_IN_MONTH] = (date-1) / 7 + 1;
|
||||
|
||||
//long days = (long) (localMillis / DAY_MS);
|
||||
//int millisInDay = (int) (localMillis - (days * DAY_MS));
|
||||
//if (millisInDay < 0) millisInDay += DAY_MS;
|
||||
|
||||
int millisInDay = (int)(localMillis % DAY_MS);
|
||||
|
||||
// Fill in all time-related fields based on millisInDay.
|
||||
fields[MILLISECOND] = millisInDay % 1000;
|
||||
millisInDay /= 1000;
|
||||
fields[SECOND] = millisInDay % 60;
|
||||
millisInDay /= 60;
|
||||
fields[MINUTE] = millisInDay % 60;
|
||||
millisInDay /= 60;
|
||||
fields[HOUR_OF_DAY] = millisInDay;
|
||||
fields[AM_PM] = millisInDay / 12;
|
||||
fields[HOUR] = millisInDay % 12;
|
||||
|
||||
fields[ZONE_OFFSET] = rawOffset;
|
||||
fields[DST_OFFSET] = dstOffset;
|
||||
|
||||
areFieldsSet = true;
|
||||
|
||||
// Careful here: We are manually setting the isSet flags to true, so we
|
||||
// must be sure that the above code actually does set all these fields.
|
||||
_TEMPORARY_markAllFieldsSet();
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Functions for converting from milliseconds to field values
|
||||
// Support methods
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
// Hebrew date calculations are performed in terms of days, hours, and
|
||||
@ -1011,7 +570,7 @@ public class HebrewCalendar extends Calendar {
|
||||
cache.put(year, day);
|
||||
}
|
||||
return day;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the day of the week for a given day
|
||||
@ -1025,23 +584,15 @@ public class HebrewCalendar extends Calendar {
|
||||
return (int)(day % 7) + 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of days in the given Hebrew year
|
||||
*/
|
||||
private static int yearLength(int year)
|
||||
{
|
||||
return (int)(startOfYear(year+1) - startOfYear(year));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the the type of a given year.
|
||||
* 0 "Deficient" year with 353 or 383 days
|
||||
* 1 "Normal" year with 354 or 384 days
|
||||
* 2 "Complete" year with 355 or 385 days
|
||||
*/
|
||||
private static int yearType(int year)
|
||||
private final int yearType(int year)
|
||||
{
|
||||
int yearLength = yearLength(year);
|
||||
int yearLength = handleGetYearLength(year);
|
||||
|
||||
if (yearLength > 380) {
|
||||
yearLength -= 30; // Subtract length of leap month.
|
||||
@ -1063,16 +614,38 @@ public class HebrewCalendar extends Calendar {
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether a given Hebrew year is a leap year
|
||||
*
|
||||
* The rule here is that if (year % 19) == 0, 3, 6, 8, 11, 14, or 17.
|
||||
* The formula below performs the same test, believe it or not.
|
||||
*/
|
||||
private static final boolean isLeapYear(int year) {
|
||||
return (year * 12 + 17) % 19 >= 12;
|
||||
}
|
||||
|
||||
private static int monthsInYear(int year) {
|
||||
return isLeapYear(year) ? 13 : 12;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Calendar framework
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
protected int handleGetLimit(int field, int limitType) {
|
||||
return LIMITS[field][limitType];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the length of the given month in the given year
|
||||
*/
|
||||
private static int monthLength(int year, int month)
|
||||
{
|
||||
protected int handleGetMonthLength(int extendedYear, int month) {
|
||||
|
||||
switch (month) {
|
||||
case HESHVAN:
|
||||
case KISLEV:
|
||||
// These two month lengths can vary
|
||||
return MONTH_LENGTH[month][yearType(year)];
|
||||
return MONTH_LENGTH[month][yearType(extendedYear)];
|
||||
|
||||
default:
|
||||
// The rest are a fixed length
|
||||
@ -1081,20 +654,113 @@ public class HebrewCalendar extends Calendar {
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether a given Hebrew year is a leap year
|
||||
*
|
||||
* The rule here is that if (year % 19) == 0, 3, 6, 8, 11, 14, or 17.
|
||||
* The formula below performs the same test, believe it or not.
|
||||
* Returns the number of days in the given Hebrew year
|
||||
*/
|
||||
private static boolean isLeapYear(int year)
|
||||
{
|
||||
return (year * 12 + 17) % 19 >= 12;
|
||||
protected int handleGetYearLength(int eyear) {
|
||||
return (int)(startOfYear(eyear+1) - startOfYear(eyear));
|
||||
}
|
||||
|
||||
|
||||
static private void debug(String str) {
|
||||
if (false) {
|
||||
System.out.println(str);
|
||||
//-------------------------------------------------------------------------
|
||||
// Functions for converting from milliseconds to field values
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Subclasses may override this method to compute several fields
|
||||
* specific to each calendar system. These are:
|
||||
*
|
||||
* <ul><li>ERA
|
||||
* <li>YEAR
|
||||
* <li>MONTH
|
||||
* <li>DAY_OF_MONTH
|
||||
* <li>DAY_OF_YEAR
|
||||
* <li>EXTENDED_YEAR</ul>
|
||||
*
|
||||
* Subclasses can refer to the DAY_OF_WEEK and DOW_LOCAL fields,
|
||||
* which will be set when this method is called. Subclasses can
|
||||
* also call the getGregorianXxx() methods to obtain Gregorian
|
||||
* calendar equivalents for the given Julian day.
|
||||
*
|
||||
* <p>In addition, subclasses should compute any subclass-specific
|
||||
* fields, that is, fields from BASE_FIELD_COUNT to
|
||||
* getFieldCount() - 1.
|
||||
*/
|
||||
protected void handleComputeFields(int julianDay) {
|
||||
long d = julianDay - 347997;
|
||||
long m = (d * DAY_PARTS) / MONTH_PARTS; // Months (approx)
|
||||
int year = (int)((19 * m + 234) / 235) + 1; // Years (approx)
|
||||
long ys = startOfYear(year); // 1st day of year
|
||||
int dayOfYear = (int)(d - ys);
|
||||
|
||||
// Because of the postponement rules, it's possible to guess wrong. Fix it.
|
||||
while (dayOfYear < 1) {
|
||||
year--;
|
||||
ys = startOfYear(year);
|
||||
dayOfYear = (int)(d - ys);
|
||||
}
|
||||
|
||||
// Now figure out which month we're in, and the date within that month
|
||||
int yearType = yearType(year);
|
||||
int monthStart[][] = isLeapYear(year) ? LEAP_MONTH_START : MONTH_START;
|
||||
|
||||
int month = 0;
|
||||
while (dayOfYear > monthStart[month][yearType]) {
|
||||
month++;
|
||||
}
|
||||
month--;
|
||||
int dayOfMonth = dayOfYear - monthStart[month][yearType];
|
||||
|
||||
internalSet(ERA, 0);
|
||||
internalSet(YEAR, year);
|
||||
internalSet(EXTENDED_YEAR, year);
|
||||
internalSet(MONTH, month);
|
||||
internalSet(DAY_OF_MONTH, dayOfMonth);
|
||||
internalSet(DAY_OF_YEAR, dayOfYear);
|
||||
}
|
||||
};
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Functions for converting from field values to milliseconds
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
protected int handleGetExtendedYear() {
|
||||
int year;
|
||||
if (newerField(EXTENDED_YEAR, YEAR) == EXTENDED_YEAR) {
|
||||
year = internalGet(EXTENDED_YEAR, 1); // Default to year 1
|
||||
} else {
|
||||
year = internalGet(YEAR, 1); // Default to year 1
|
||||
}
|
||||
return year;
|
||||
}
|
||||
|
||||
// Return JD of start of given month/year
|
||||
protected int handleComputeMonthStart(int eyear, int month) {
|
||||
|
||||
// Resolve out-of-range months. This is necessary in order to
|
||||
// obtain the correct year.
|
||||
if (month < 0) {
|
||||
while (month < 0) {
|
||||
month += monthsInYear(--eyear);
|
||||
}
|
||||
} else if (month > 0) {
|
||||
for (;;) {
|
||||
int monthsInYear = monthsInYear(eyear);
|
||||
if (month < monthsInYear) {
|
||||
break;
|
||||
}
|
||||
++eyear;
|
||||
month -= monthsInYear;
|
||||
}
|
||||
}
|
||||
|
||||
long day = startOfYear(eyear);
|
||||
|
||||
if (month != 0) {
|
||||
if (isLeapYear(eyear)) {
|
||||
day += LEAP_MONTH_START[month][yearType(eyear)];
|
||||
} else {
|
||||
day += MONTH_START[month][yearType(eyear)];
|
||||
}
|
||||
}
|
||||
|
||||
return (int) (day + 347997);
|
||||
}
|
||||
}
|
||||
|
@ -5,8 +5,8 @@
|
||||
*******************************************************************************
|
||||
*
|
||||
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/util/IslamicCalendar.java,v $
|
||||
* $Date: 2000/10/27 22:25:52 $
|
||||
* $Revision: 1.7 $
|
||||
* $Date: 2000/11/18 00:30:54 $
|
||||
* $Revision: 1.8 $
|
||||
*
|
||||
*****************************************************************************************
|
||||
*/
|
||||
@ -71,7 +71,7 @@ import com.ibm.util.CalendarAstronomer;
|
||||
* @see com.ibm.util.GregorianCalendar
|
||||
*
|
||||
* @author Laura Werner
|
||||
* @version 1.0
|
||||
* @author Alan Liu
|
||||
*/
|
||||
public class IslamicCalendar extends Calendar {
|
||||
|
||||
@ -118,13 +118,6 @@ public class IslamicCalendar extends Calendar {
|
||||
public static final int DHU_AL_HIJJAH = 11;
|
||||
|
||||
|
||||
// Useful millisecond constants
|
||||
private static final int SECOND_MS = 1000;
|
||||
private static final int MINUTE_MS = 60*SECOND_MS;
|
||||
private static final int HOUR_MS = 60*MINUTE_MS;
|
||||
private static final long DAY_MS = 24*HOUR_MS;
|
||||
private static final long WEEK_MS = 7*DAY_MS;
|
||||
|
||||
private static final long HIJRA_MILLIS = -42521587200000L; // 7/16/622 AD 00:00
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
@ -249,9 +242,10 @@ public class IslamicCalendar extends Calendar {
|
||||
if (civil != beCivil) {
|
||||
// The fields of the calendar will become invalid, because the calendar
|
||||
// rules are different
|
||||
computeTime();
|
||||
areFieldsSet = false;
|
||||
long m = getTimeInMillis();
|
||||
civil = beCivil;
|
||||
clear();
|
||||
setTimeInMillis(m);
|
||||
}
|
||||
}
|
||||
|
||||
@ -268,466 +262,35 @@ public class IslamicCalendar extends Calendar {
|
||||
// Minimum / Maximum access functions
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
// The minimum and maximum values for all of the fields, for validation
|
||||
private static final int MinMax[][] = {
|
||||
// Min Greatest Min Least Max Max
|
||||
{ 0, 0, 0, 0 }, // ERA
|
||||
{ 1, 1, 5000000, 5000000 }, // YEAR
|
||||
{ 0, 0, 11, 11 }, // MONTH
|
||||
{ 0, 0, 51, 52 }, // WEEK_OF_YEAR
|
||||
{ 0, 0, 5, 6 }, // WEEK_OF_MONTH
|
||||
{ 1, 1, 29, 30 }, // DAY_OF_MONTH
|
||||
{ 1, 1, 354, 355 }, // DAY_OF_YEAR
|
||||
{ 1, 1, 7, 7 }, // DAY_OF_WEEK
|
||||
{ -1, -1, 4, 5 }, // DAY_OF_WEEK_IN_MONTH
|
||||
{ 0, 0, 1, 1 }, // AM_PM
|
||||
{ 0, 0, 11, 11 }, // HOUR
|
||||
{ 0, 0, 23, 23 }, // HOUR_OF_DAY
|
||||
{ 0, 0, 59, 59 }, // MINUTE
|
||||
{ 0, 0, 59, 59 }, // SECOND
|
||||
{ 0, 0, 999, 999 }, // MILLISECOND
|
||||
{ -12*HOUR_MS, -12*HOUR_MS, 12*HOUR_MS, 12*HOUR_MS }, // ZONE_OFFSET
|
||||
{ 0, 0, 1*HOUR_MS, 1*HOUR_MS }, // DST_OFFSET
|
||||
private static final int LIMITS[][] = {
|
||||
// Minimum Greatest Least Maximum
|
||||
// Minimum Maximum
|
||||
{ 0, 0, 0, 0 }, // ERA
|
||||
{ 1, 1, 5000000, 5000000 }, // YEAR
|
||||
{ 0, 0, 11, 11 }, // MONTH
|
||||
{ 1, 1, 51, 52 }, // WEEK_OF_YEAR
|
||||
{ 0, 0, 5, 6 }, // WEEK_OF_MONTH
|
||||
{ 1, 1, 29, 30 }, // DAY_OF_MONTH
|
||||
{ 1, 1, 354, 355 }, // DAY_OF_YEAR
|
||||
{/* */}, // DAY_OF_WEEK
|
||||
{ -1, -1, 4, 5 }, // DAY_OF_WEEK_IN_MONTH
|
||||
{/* */}, // AM_PM
|
||||
{/* */}, // HOUR
|
||||
{/* */}, // HOUR_OF_DAY
|
||||
{/* */}, // MINUTE
|
||||
{/* */}, // SECOND
|
||||
{/* */}, // MILLISECOND
|
||||
{/* */}, // ZONE_OFFSET
|
||||
{/* */}, // DST_OFFSET
|
||||
{ -5000001, -5000001, 5000001, 5000001 }, // YEAR_WOY
|
||||
{/* */}, // DOW_LOCAL
|
||||
{ -5000000, -5000000, 5000000, 5000000 }, // EXTENDED_YEAR
|
||||
{/* */}, // JULIAN_DAY
|
||||
{/* */}, // MILLISECONDS_IN_DAY
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns minimum value for the given field.
|
||||
* For example, for {@link #DAY_OF_MONTH DAY_OF_MONTH} this method returns 1,
|
||||
*
|
||||
* @param field The field whose minimum value is desired.
|
||||
*
|
||||
* @see com.ibm.util.Calendar#getMinimum
|
||||
*/
|
||||
public int getMinimum(int field)
|
||||
{
|
||||
return MinMax[field][0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns highest minimum value for the given field. For the Islamic
|
||||
* calendar, this always returns the same result as {@link #getMinimum}.
|
||||
*
|
||||
* @param field The field whose greatest minimum value is desired.
|
||||
*
|
||||
* @see #getMinimum
|
||||
*/
|
||||
public int getGreatestMinimum(int field)
|
||||
{
|
||||
return MinMax[field][1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns maximum value for the given field
|
||||
* For the {@link #DAY_OF_MONTH DAY_OF_MONTH} field, this method returns 30.
|
||||
*
|
||||
* @param field The field whose maximum value is desired.
|
||||
*
|
||||
* @see #getLeastMaximum
|
||||
* @see #getActualMaximum
|
||||
*/
|
||||
public int getMaximum(int field)
|
||||
{
|
||||
return MinMax[field][3];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns lowest maximum value for the given field. For most fields,
|
||||
* this returns the same result as {@link #getMaximum getMaximum}. However,
|
||||
* for some fields this can be a lower number. For example,
|
||||
* the maximum {@link #DAY_OF_MONTH DAY_OF_MONTH} in the Islamic caleandar varies
|
||||
* from month to month, so this method returns 29 while <code>getMaximum</code>
|
||||
* returns 30.
|
||||
*
|
||||
* @param field The field whose least maximum value is desired.
|
||||
*
|
||||
* @see #getMaximum
|
||||
* @see #getActualMaximum
|
||||
*/
|
||||
public int getLeastMaximum(int field)
|
||||
{
|
||||
return MinMax[field][2];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the maximum value that a field could have, given the current date.
|
||||
* For example, for the {@link #DAY_OF_MONTH DAY_OF_MONTH} field the actual maximum varies
|
||||
* depending on the length of the month, which in turn varies according
|
||||
* to either the civil calendar cycle or the actual time of the next new moon.
|
||||
*
|
||||
* @param field The field whose maximum value is desired.
|
||||
*
|
||||
* @see #getMaximum
|
||||
* @see #getLeastMaximum
|
||||
*/
|
||||
public int getActualMaximum(int field) {
|
||||
if (!isSet(YEAR) || !isSet(MONTH)) {
|
||||
complete();
|
||||
}
|
||||
switch (field) {
|
||||
case DAY_OF_MONTH:
|
||||
return monthLength(fields[YEAR], fields[MONTH]);
|
||||
|
||||
case DAY_OF_YEAR:
|
||||
return yearLength(fields[YEAR]);
|
||||
|
||||
default:
|
||||
return super.getActualMaximum(field);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Functions for converting from field values to milliseconds....
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Converts time field values to UTC as milliseconds.
|
||||
*
|
||||
* @exception IllegalArgumentException if a field has an invalid value
|
||||
* and {@link #isLenient isLenient} returns <code>false</code>.
|
||||
*/
|
||||
protected void computeTime()
|
||||
{
|
||||
if (isTimeSet) return;
|
||||
|
||||
if (!isLenient() && !validateFields())
|
||||
throw new IllegalArgumentException();
|
||||
|
||||
if (isSet(ERA) && internalGet(ERA) != 0)
|
||||
throw new IllegalArgumentException();
|
||||
|
||||
// We need the time zone offset for some of the calculations below.
|
||||
// We use the TimeZone object, unless the user has explicitly set the
|
||||
// ZONE_OFFSET field.
|
||||
TimeZone zone = getTimeZone();
|
||||
int zoneOffset = zone.getRawOffset();
|
||||
|
||||
// The year is required. We don't have to check if it's unset,
|
||||
// because if it is, by definition it will be 0.
|
||||
|
||||
int year = internalGet(YEAR);
|
||||
|
||||
if (year <= 0 && !isLenient())
|
||||
throw new IllegalArgumentException();
|
||||
|
||||
long dayNumber = 0, date = 0;
|
||||
|
||||
// The following code is somewhat convoluted. The various nested
|
||||
// if's handle the different cases of what fields are present.
|
||||
if (isSet(MONTH) &&
|
||||
(isSet(DATE) ||
|
||||
(isSet(DAY_OF_WEEK) &&
|
||||
(isSet(WEEK_OF_MONTH) ||
|
||||
isSet(DAY_OF_WEEK_IN_MONTH))
|
||||
)
|
||||
))
|
||||
{
|
||||
// We have the month specified. Figure out when that month starts
|
||||
int month = internalGet(MONTH);
|
||||
dayNumber = monthStart(year, month);
|
||||
|
||||
if (isSet(DATE))
|
||||
{
|
||||
date = internalGet(DATE);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Compute from day of week plus week number or from the day of
|
||||
// week plus the day of week in month. The computations are
|
||||
// almost identical.
|
||||
|
||||
// Find the day of the week for the first of this month. This
|
||||
// is zero-based, with 0 being the locale-specific first day of
|
||||
// the week. Add 1 to get the 1st day of month. Subtract
|
||||
// getFirstDayOfWeek() to make 0-based.
|
||||
|
||||
int fdm = absoluteDayToDayOfWeek(dayNumber + 1) - getFirstDayOfWeek();
|
||||
if (fdm < 0) fdm += 7;
|
||||
|
||||
// Find the start of the first week. This will be a date from
|
||||
// 1..-6. It represents the locale-specific first day of the
|
||||
// week of the first day of the month, ignoring minimal days in
|
||||
// first week.
|
||||
date = 1 - fdm + internalGet(DAY_OF_WEEK) - getFirstDayOfWeek();
|
||||
|
||||
if (isSet(WEEK_OF_MONTH))
|
||||
{
|
||||
// Adjust for minimal days in first week.
|
||||
if ((7 - fdm) < getMinimalDaysInFirstWeek()) date += 7;
|
||||
|
||||
// Now adjust for the week number.
|
||||
date += 7 * (internalGet(WEEK_OF_MONTH) - 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Adjust into the month, if needed.
|
||||
if (date < 1) date += 7;
|
||||
|
||||
// We are basing this on the day-of-week-in-month. The only
|
||||
// trickiness occurs if the day-of-week-in-month is
|
||||
// negative.
|
||||
int dim = internalGet(DAY_OF_WEEK_IN_MONTH);
|
||||
if (dim >= 0) date += 7*(dim - 1);
|
||||
else
|
||||
{
|
||||
// Move date to the last of this day-of-week in this
|
||||
// month, then back up as needed. If dim==-1, we don't
|
||||
// back up at all. If dim==-2, we back up once, etc.
|
||||
// Don't back up past the first of the given day-of-week
|
||||
// in this month. Note that we handle -2, -3,
|
||||
// etc. correctly, even though values < -1 are
|
||||
// technically disallowed.
|
||||
date += ((monthLength(year, month) - date) / 7 + dim + 1) * 7;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (isSet(DAY_OF_YEAR)) {
|
||||
dayNumber = yearStart(year) + internalGet(DAY_OF_YEAR);
|
||||
}
|
||||
else if (isSet(DAY_OF_WEEK) && isSet(WEEK_OF_YEAR))
|
||||
{
|
||||
dayNumber = yearStart(year);
|
||||
|
||||
// Compute from day of week plus week of year
|
||||
|
||||
// Find the day of the week for the first of this year. This
|
||||
// is zero-based, with 0 being the locale-specific first day of
|
||||
// the week. Add 1 to get the 1st day of month. Subtract
|
||||
// getFirstDayOfWeek() to make 0-based.
|
||||
int fdy = absoluteDayToDayOfWeek(dayNumber + 1) - getFirstDayOfWeek();
|
||||
if (fdy < 0) fdy += 7;
|
||||
|
||||
// Find the start of the first week. This may be a valid date
|
||||
// from 1..7, or a date before the first, from 0..-6. It
|
||||
// represents the locale-specific first day of the week
|
||||
// of the first day of the year.
|
||||
|
||||
// First ignore the minimal days in first week.
|
||||
date = 1 - fdy + internalGet(DAY_OF_WEEK) - getFirstDayOfWeek();
|
||||
|
||||
// Adjust for minimal days in first week.
|
||||
if ((7 - fdy) < getMinimalDaysInFirstWeek()) date += 7;
|
||||
|
||||
// Now adjust for the week number.
|
||||
date += 7 * (internalGet(WEEK_OF_YEAR) - 1);
|
||||
|
||||
dayNumber += date;
|
||||
} else { // Not enough information
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
long millis = dayNumber * DAY_MS + HIJRA_MILLIS;
|
||||
|
||||
// Add in the days we calculated above
|
||||
millis += (date - 1) * DAY_MS;
|
||||
|
||||
// Now we can do the time portion of the conversion.
|
||||
|
||||
int millisInDay = 0;
|
||||
|
||||
// Hours
|
||||
if (isSet(HOUR_OF_DAY)) {
|
||||
// Don't normalize here; let overflow bump into the next period.
|
||||
// This is consistent with how we handle other fields.
|
||||
millisInDay += internalGet(HOUR_OF_DAY);
|
||||
|
||||
} else if (isSet(HOUR))
|
||||
{
|
||||
// Don't normalize here; let overflow bump into the next period.
|
||||
// This is consistent with how we handle other fields.
|
||||
millisInDay += internalGet(HOUR);
|
||||
|
||||
millisInDay += 12 * internalGet(AM_PM);
|
||||
}
|
||||
|
||||
// Minutes. We use the fact that unset == 0
|
||||
millisInDay *= 60;
|
||||
millisInDay += internalGet(MINUTE);
|
||||
|
||||
// Seconds. unset == 0
|
||||
millisInDay *= 60;
|
||||
millisInDay += internalGet(SECOND);
|
||||
|
||||
// Milliseconds. unset == 0
|
||||
millisInDay *= 1000;
|
||||
millisInDay += internalGet(MILLISECOND);
|
||||
|
||||
// Add millis and millisInDay together, to make millis contain the GMT time
|
||||
// computed so far, with no DST adjustments
|
||||
millis += millisInDay;
|
||||
|
||||
int dstOffset = 0;
|
||||
|
||||
//
|
||||
// Compute the time zone offset and DST offset.
|
||||
// Since the TimeZone API expects the Gregorian year, month, etc.,
|
||||
// We have to convert to local Gregorian time in order to
|
||||
// figure out the time zone calculations. This is a bit slow, but
|
||||
// it saves us from doing some *really* nasty calculations here.
|
||||
//
|
||||
if (getTimeZone().useDaylightTime())
|
||||
{
|
||||
synchronized(gregorian) {
|
||||
gregorian.setTimeZone(zone);
|
||||
gregorian.setTime(new Date(millis));
|
||||
dstOffset = gregorian.get(DST_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
// Store our final computed GMT time, with DST adjustments.
|
||||
time = millis - zoneOffset - dstOffset;
|
||||
isTimeSet = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the values of the set time fields.
|
||||
*/
|
||||
private boolean validateFields()
|
||||
{
|
||||
for (int field = 0; field < FIELD_COUNT; field++)
|
||||
{
|
||||
// Ignore DATE and DAY_OF_YEAR which are handled below
|
||||
if (isSet(field) &&
|
||||
!boundsCheck(internalGet(field), field))
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isSet(YEAR))
|
||||
{
|
||||
int year = internalGet(YEAR);
|
||||
if (year < 1)
|
||||
return false;
|
||||
}
|
||||
|
||||
// Handle DAY_OF_WEEK_IN_MONTH, which must not have the value zero.
|
||||
// We've checked against minimum and maximum above already.
|
||||
if (isSet(DAY_OF_WEEK_IN_MONTH) &&
|
||||
0 == internalGet(DAY_OF_WEEK_IN_MONTH)) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the value of the given time field.
|
||||
*/
|
||||
private boolean boundsCheck(int value, int field)
|
||||
{
|
||||
return value >= getMinimum(field) && value <= getMaximum(field);
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Functions for converting from milliseconds to field values
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Converts UTC as milliseconds to time field values.
|
||||
* The time is <em>not</em>
|
||||
* recomputed first; to recompute the time, then the fields, call the
|
||||
* {@link #complete} method.
|
||||
*/
|
||||
protected void computeFields()
|
||||
{
|
||||
if (areFieldsSet) return;
|
||||
|
||||
// The following algorithm only works for dates after the Hijra (16 July AD 622)
|
||||
if (time < HIJRA_MILLIS && !isLenient()) {
|
||||
throw new IllegalArgumentException("IslamicCalendar does not handle dates before 1 AH");
|
||||
}
|
||||
|
||||
//
|
||||
// Compute the time zone offset and DST offset.
|
||||
// Since the TimeZone API expects the Gregorian year, month, etc.,
|
||||
// We have to convert to local Gregorian time in order to
|
||||
// figure out the time zone calculations. This is a bit slow, but
|
||||
// it saves us from doing some *really* nasty calculations here.
|
||||
//
|
||||
TimeZone zone = getTimeZone();
|
||||
int rawOffset = zone.getRawOffset(); // Not including DST
|
||||
int dstOffset = 0;
|
||||
if (zone.useDaylightTime())
|
||||
{
|
||||
synchronized (gregorian) {
|
||||
gregorian.setTimeZone(zone);
|
||||
gregorian.setTime(new Date(time));
|
||||
dstOffset += gregorian.get(DST_OFFSET);
|
||||
}
|
||||
}
|
||||
long localMillis = time + rawOffset + dstOffset;
|
||||
|
||||
long days = (localMillis - HIJRA_MILLIS) / DAY_MS;
|
||||
int millisInDay = (int)(localMillis % DAY_MS);
|
||||
|
||||
if (civil) {
|
||||
// Use the civil calendar approximation, which is just arithmetic
|
||||
int year = (int)Math.floor( (30 * days + 10646) / 10631.0 );
|
||||
int month = (int)Math.ceil((days - 29 - yearStart(year)) / 29.5 );
|
||||
month = Math.min(month, 11);
|
||||
|
||||
int date = (int)(days - monthStart(year, month)) + 1;
|
||||
|
||||
fields[YEAR] = year;
|
||||
fields[MONTH] = month;
|
||||
fields[DATE] = date;
|
||||
} else {
|
||||
// Guess at the number of elapsed full months since the epoch
|
||||
int months = (int)Math.floor(days / CalendarAstronomer.SYNODIC_MONTH);
|
||||
|
||||
long start = (long)Math.floor(months * CalendarAstronomer.SYNODIC_MONTH - 1);
|
||||
|
||||
if ( days - start >= 28 && MoonAge(time) > 0) {
|
||||
// If we're near the end of the month, assume next month and search backwards
|
||||
months++;
|
||||
}
|
||||
|
||||
// Find out the last time that the new moon was actually visible at this longitude
|
||||
// This returns midnight the night that the moon was visible at sunset.
|
||||
while ((start = trueMonthStart(months)) > days) {
|
||||
// If it was after the date in question, back up a month and try again
|
||||
months--;
|
||||
}
|
||||
|
||||
fields[YEAR] = months / 12 + 1;
|
||||
fields[MONTH] = months % 12;
|
||||
fields[DATE] = (int)(days - start) + 1;
|
||||
|
||||
}
|
||||
fields[ERA] = 0;
|
||||
|
||||
// Calculate the day of the week.
|
||||
int dayOfWeek = absoluteDayToDayOfWeek(days);
|
||||
fields[DAY_OF_WEEK] = dayOfWeek;
|
||||
fields[WEEK_OF_MONTH] = weekNumber(fields[DATE], dayOfWeek);
|
||||
fields[DAY_OF_WEEK_IN_MONTH] = (fields[DATE]-1) / 7 + 1;
|
||||
|
||||
// Now figure out the day of the year.
|
||||
int dayOfYear = (int)(days - monthStart(fields[YEAR], fields[MONTH]) + 1);
|
||||
|
||||
fields[DAY_OF_YEAR] = dayOfYear;
|
||||
fields[WEEK_OF_YEAR] = weekNumber(dayOfYear, dayOfWeek);
|
||||
|
||||
// Fill in all time-related fields based on millisInDay.
|
||||
|
||||
fields[MILLISECOND] = millisInDay % 1000;
|
||||
millisInDay /= 1000;
|
||||
fields[SECOND] = millisInDay % 60;
|
||||
millisInDay /= 60;
|
||||
fields[MINUTE] = millisInDay % 60;
|
||||
millisInDay /= 60;
|
||||
fields[HOUR_OF_DAY] = millisInDay;
|
||||
fields[AM_PM] = millisInDay / 12;
|
||||
fields[HOUR] = millisInDay % 12;
|
||||
|
||||
fields[ZONE_OFFSET] = rawOffset;
|
||||
fields[DST_OFFSET] = dstOffset;
|
||||
|
||||
areFieldsSet = true;
|
||||
|
||||
// Careful here: We are manually setting the isSet flags to true, so we
|
||||
// must be sure that the above code actually does set all these fields.
|
||||
_TEMPORARY_markAllFieldsSet();
|
||||
protected int handleGetLimit(int field, int limitType) {
|
||||
return LIMITS[field][limitType];
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
@ -771,19 +334,6 @@ public class IslamicCalendar extends Calendar {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of days in the given Islamic year
|
||||
*/
|
||||
private final int yearLength(int year)
|
||||
{
|
||||
if (civil) {
|
||||
return 354 + (civilLeapYear(year) ? 1 : 0);
|
||||
} else {
|
||||
int month = 12*(year-1);
|
||||
return (int)(trueMonthStart(month + 12) - trueMonthStart(month));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the day # on which the given month starts. Days are counted
|
||||
* from the Hijri epoch, origin 0.
|
||||
@ -800,28 +350,6 @@ public class IslamicCalendar extends Calendar {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the length (in days) of the given month.
|
||||
*
|
||||
* @param year The hijri year
|
||||
* @param year The hijri month, 0-based
|
||||
*/
|
||||
private final int monthLength(int year, int month)
|
||||
{
|
||||
int length = 0;
|
||||
|
||||
if (civil) {
|
||||
length = 29 + (month+1) % 2;
|
||||
if (month == DHU_AL_HIJJAH && civilLeapYear(year)) {
|
||||
length++;
|
||||
}
|
||||
} else {
|
||||
month = 12*(year-1) + month;
|
||||
length = (int)( trueMonthStart(month+1) - trueMonthStart(month) );
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the day number on which a particular month of the true/lunar
|
||||
* Islamic calendar starts.
|
||||
@ -838,26 +366,26 @@ public class IslamicCalendar extends Calendar {
|
||||
{
|
||||
// Make a guess at when the month started, using the average length
|
||||
long origin = HIJRA_MILLIS
|
||||
+ (long)Math.floor(month * CalendarAstronomer.SYNODIC_MONTH - 1) * DAY_MS;
|
||||
+ (long)Math.floor(month * CalendarAstronomer.SYNODIC_MONTH - 1) * ONE_DAY;
|
||||
|
||||
double age = MoonAge(origin);
|
||||
double age = moonAge(origin);
|
||||
|
||||
if (MoonAge(origin) >= 0) {
|
||||
if (moonAge(origin) >= 0) {
|
||||
// The month has already started
|
||||
do {
|
||||
origin -= DAY_MS;
|
||||
age = MoonAge(origin);
|
||||
origin -= ONE_DAY;
|
||||
age = moonAge(origin);
|
||||
} while (age >= 0);
|
||||
}
|
||||
else {
|
||||
// Preceding month has not ended yet.
|
||||
do {
|
||||
origin += DAY_MS;
|
||||
age = MoonAge(origin);
|
||||
origin += ONE_DAY;
|
||||
age = moonAge(origin);
|
||||
} while (age < 0);
|
||||
}
|
||||
|
||||
start = (origin - HIJRA_MILLIS) / DAY_MS + 1;
|
||||
start = (origin - HIJRA_MILLIS) / ONE_DAY + 1;
|
||||
|
||||
cache.put(month, start);
|
||||
}
|
||||
@ -873,7 +401,7 @@ public class IslamicCalendar extends Calendar {
|
||||
* @param time The time at which the moon's age is desired,
|
||||
* in millis since 1/1/1970.
|
||||
*/
|
||||
static final double MoonAge(long time)
|
||||
static final double moonAge(long time)
|
||||
{
|
||||
double age = 0;
|
||||
|
||||
@ -893,9 +421,6 @@ public class IslamicCalendar extends Calendar {
|
||||
//-------------------------------------------------------------------------
|
||||
// Internal data....
|
||||
//
|
||||
|
||||
// We need a GregorianCalendar object for doing time zone calculations
|
||||
private static GregorianCalendar gregorian = new GregorianCalendar();
|
||||
|
||||
// And an Astronomer object for the moon age calculations
|
||||
private static CalendarAstronomer astro = new CalendarAstronomer();
|
||||
@ -911,9 +436,125 @@ public class IslamicCalendar extends Calendar {
|
||||
*/
|
||||
private boolean civil = true;
|
||||
|
||||
static private void debug(String str) {
|
||||
if (true) {
|
||||
System.out.println(str);
|
||||
//----------------------------------------------------------------------
|
||||
// Calendar framework
|
||||
//----------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Return the length (in days) of the given month.
|
||||
*
|
||||
* @param year The hijri year
|
||||
* @param year The hijri month, 0-based
|
||||
*/
|
||||
protected int handleGetMonthLength(int extendedYear, int month) {
|
||||
|
||||
int length = 0;
|
||||
|
||||
if (civil) {
|
||||
length = 29 + (month+1) % 2;
|
||||
if (month == DHU_AL_HIJJAH && civilLeapYear(extendedYear)) {
|
||||
length++;
|
||||
}
|
||||
} else {
|
||||
month = 12*(extendedYear-1) + month;
|
||||
length = (int)( trueMonthStart(month+1) - trueMonthStart(month) );
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of days in the given Islamic year
|
||||
*/
|
||||
protected int handleGetYearLength(int extendedYear) {
|
||||
if (civil) {
|
||||
return 354 + (civilLeapYear(extendedYear) ? 1 : 0);
|
||||
} else {
|
||||
int month = 12*(extendedYear-1);
|
||||
return (int)(trueMonthStart(month + 12) - trueMonthStart(month));
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Functions for converting from field values to milliseconds....
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
// Return JD of start of given month/year
|
||||
protected int handleComputeMonthStart(int eyear, int month) {
|
||||
return (int) monthStart(eyear, month) + 1948439;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Functions for converting from milliseconds to field values
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
protected int handleGetExtendedYear() {
|
||||
int year;
|
||||
if (newerField(EXTENDED_YEAR, YEAR) == EXTENDED_YEAR) {
|
||||
year = internalGet(EXTENDED_YEAR, 1); // Default to year 1
|
||||
} else {
|
||||
year = internalGet(YEAR, 1); // Default to year 1
|
||||
}
|
||||
return year;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override Calendar to compute several fields specific to the Islamic
|
||||
* calendar system. These are:
|
||||
*
|
||||
* <ul><li>ERA
|
||||
* <li>YEAR
|
||||
* <li>MONTH
|
||||
* <li>DAY_OF_MONTH
|
||||
* <li>DAY_OF_YEAR
|
||||
* <li>EXTENDED_YEAR</ul>
|
||||
*
|
||||
* The DAY_OF_WEEK and DOW_LOCAL fields are already set when this
|
||||
* method is called. The getGregorianXxx() methods return Gregorian
|
||||
* calendar equivalents for the given Julian day.
|
||||
*/
|
||||
protected void handleComputeFields(int julianDay) {
|
||||
int year, month, dayOfMonth, dayOfYear;
|
||||
long monthStart;
|
||||
long days = julianDay - 1948440;
|
||||
|
||||
if (civil) {
|
||||
// Use the civil calendar approximation, which is just arithmetic
|
||||
year = (int)Math.floor( (30 * days + 10646) / 10631.0 );
|
||||
month = (int)Math.ceil((days - 29 - yearStart(year)) / 29.5 );
|
||||
month = Math.min(month, 11);
|
||||
monthStart = monthStart(year, month);
|
||||
} else {
|
||||
// Guess at the number of elapsed full months since the epoch
|
||||
int months = (int)Math.floor(days / CalendarAstronomer.SYNODIC_MONTH);
|
||||
|
||||
monthStart = (long)Math.floor(months * CalendarAstronomer.SYNODIC_MONTH - 1);
|
||||
|
||||
if ( days - monthStart >= 28 && moonAge(internalGetTimeInMillis()) > 0) {
|
||||
// If we're near the end of the month, assume next month and search backwards
|
||||
months++;
|
||||
}
|
||||
|
||||
// Find out the last time that the new moon was actually visible at this longitude
|
||||
// This returns midnight the night that the moon was visible at sunset.
|
||||
while ((monthStart = trueMonthStart(months)) > days) {
|
||||
// If it was after the date in question, back up a month and try again
|
||||
months--;
|
||||
}
|
||||
|
||||
year = months / 12 + 1;
|
||||
month = months % 12;
|
||||
}
|
||||
|
||||
dayOfMonth = (int)(days - monthStart(year, month)) + 1;
|
||||
|
||||
// Now figure out the day of the year.
|
||||
dayOfYear = (int)(days - monthStart(year, 0) + 1);
|
||||
|
||||
internalSet(ERA, 0);
|
||||
internalSet(YEAR, year);
|
||||
internalSet(EXTENDED_YEAR, year);
|
||||
internalSet(MONTH, month);
|
||||
internalSet(DAY_OF_MONTH, dayOfMonth);
|
||||
internalSet(DAY_OF_YEAR, dayOfYear);
|
||||
}
|
||||
}
|
||||
|
@ -5,8 +5,8 @@
|
||||
*******************************************************************************
|
||||
*
|
||||
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/util/JapaneseCalendar.java,v $
|
||||
* $Date: 2000/10/17 18:26:45 $
|
||||
* $Revision: 1.5 $
|
||||
* $Date: 2000/11/18 00:30:54 $
|
||||
* $Revision: 1.6 $
|
||||
*
|
||||
*****************************************************************************************
|
||||
*/
|
||||
@ -43,6 +43,7 @@ import java.util.Locale;
|
||||
* @see com.ibm.util.GregorianCalendar
|
||||
*
|
||||
* @author Laura Werner
|
||||
* @author Alan Liu
|
||||
*/
|
||||
public class JapaneseCalendar extends GregorianCalendar {
|
||||
|
||||
@ -97,8 +98,8 @@ public class JapaneseCalendar extends GregorianCalendar {
|
||||
* @param date The date to which the new calendar is set.
|
||||
*/
|
||||
public JapaneseCalendar(Date date) {
|
||||
super(TimeZone.getDefault(), Locale.getDefault());
|
||||
this.setTime(date);
|
||||
this();
|
||||
setTime(date);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -142,7 +143,7 @@ public class JapaneseCalendar extends GregorianCalendar {
|
||||
*/
|
||||
public JapaneseCalendar(int year, int month, int date) {
|
||||
super(year, month, date);
|
||||
set(ERA, eras.length - 1);
|
||||
set(ERA, CURRENT_ERA);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -168,149 +169,60 @@ public class JapaneseCalendar extends GregorianCalendar {
|
||||
int minute, int second)
|
||||
{
|
||||
super(year, month, date, hour, minute, second);
|
||||
set(ERA, eras.length - 1);
|
||||
set(ERA, CURRENT_ERA);
|
||||
}
|
||||
|
||||
/**
|
||||
* Override of the <code>GregorianCalendar</code> method that computes the
|
||||
* fields such as YEAR, MONTH, and DATE from the date in milliseconds
|
||||
* since 1/1/1970 AD.
|
||||
* <p>
|
||||
* This method calls {@link GregorianCalendar#computeFields} to do most
|
||||
* of the work, then determines the imperial era corresponding to the
|
||||
* Gregorian date and adjusts the {@link #YEAR YEAR} and {@link #ERA ERA}
|
||||
* fields accordingly.
|
||||
*/
|
||||
protected void computeFields() {
|
||||
// Let GregorianCalendar do its thing.
|
||||
super.computeFields();
|
||||
|
||||
fromGregorian();
|
||||
|
||||
//
|
||||
// If the date was too early for this calendar, throw an exception,
|
||||
// but only after we've made all the fields consistent
|
||||
//
|
||||
if (fields[YEAR] < 1 && !isLenient()) {
|
||||
throw new IllegalArgumentException("Year out of range");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Override of the <code>GregorianCalendar</code> method that computes the
|
||||
* elapsed time in milliseconds since 1/1/1970 AD from the fields such
|
||||
* as YEAR, MONTH, and DATE.
|
||||
* <p>
|
||||
* This method adjusts the {@link #YEAR YEAR} and {@link #ERA ERA} from their
|
||||
* values in the Japanese calendar to the corresponding Gregorian values, calls
|
||||
* {@link GregorianCalendar#computeTime} to do the real millisecond
|
||||
* calculation, and then restores the Japanese <code>YEAR</code> and
|
||||
* <code>ERA</code>.
|
||||
*/
|
||||
protected void computeTime() {
|
||||
int year = fields[YEAR];
|
||||
int era = fields[ERA];
|
||||
|
||||
// If we're in strict mode, make sure that the year actually
|
||||
// falls within the era, i.e. isn't < 1 or > era length
|
||||
if (era < 0 || era >= eras.length) {
|
||||
throw new IllegalArgumentException("Era out of range");
|
||||
}
|
||||
if (!isLenient() && (year < 1
|
||||
|| era < eras.length && year > (eras[(era+1)*3] - eras[era*3] + 1))) {
|
||||
throw new IllegalArgumentException("Year out of range");
|
||||
}
|
||||
|
||||
// Adjust the year and era to the corresponding Gregorian values
|
||||
toGregorian();
|
||||
try {
|
||||
// Let GregorianCalendar do its thing.
|
||||
super.computeTime();
|
||||
}
|
||||
finally {
|
||||
// Set the year and era back to the original values
|
||||
fields[YEAR] = year;
|
||||
fields[ERA] = era;
|
||||
}
|
||||
}
|
||||
|
||||
public void add(int field, int amount) {
|
||||
toGregorian();
|
||||
try {
|
||||
super.add(field, amount);
|
||||
}
|
||||
finally {
|
||||
fromGregorian();
|
||||
}
|
||||
}
|
||||
|
||||
public void roll(int field, int amount) {
|
||||
toGregorian();
|
||||
try {
|
||||
super.roll(field, amount);
|
||||
}
|
||||
finally {
|
||||
fromGregorian();
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Methods for converting between Gregorian and Buddhist calendars
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Convert the YEAR and ERA fields from Japanese to Gregorian values
|
||||
* Return the (Buddhist) value of the YEAR field on input;
|
||||
*/
|
||||
private void toGregorian() {
|
||||
int year = fields[YEAR] + eras[fields[ERA] * 3] - 1;
|
||||
int era = AD;
|
||||
|
||||
if (year < 1) {
|
||||
year = 1 - year;
|
||||
era = BC;
|
||||
protected int handleGetExtendedYear() {
|
||||
int year;
|
||||
// TODO reimplement this to be faster?
|
||||
if (newerField(EXTENDED_YEAR, YEAR) == EXTENDED_YEAR &&
|
||||
newerField(EXTENDED_YEAR, ERA) == EXTENDED_YEAR) {
|
||||
year = internalGet(EXTENDED_YEAR, 1);
|
||||
} else {
|
||||
// Subtract one because year starts at 1
|
||||
year = internalGet(YEAR) + ERAS[internalGet(ERA) * 3] - 1;
|
||||
}
|
||||
fields[ERA] = era;
|
||||
fields[YEAR] = year;
|
||||
return year;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adjust the year and era from Gregorian to Japanese values
|
||||
*/
|
||||
private void fromGregorian() {
|
||||
// Figure out which emperor's reign we're in, and adjust the
|
||||
// year and era accordingly.
|
||||
int year = fields[ERA] == AD ? fields[YEAR] : 1 - fields[YEAR];
|
||||
|
||||
// Binary search...
|
||||
int low = 0, high = eras.length / 3;
|
||||
protected void handleComputeFields(int julianDay) {
|
||||
super.handleComputeFields(julianDay);
|
||||
int year = internalGet(EXTENDED_YEAR);
|
||||
|
||||
// Binary search
|
||||
int low = 0, high = ERAS.length / 3;
|
||||
|
||||
while (low < high - 1) {
|
||||
int i = (low + high + 1) / 2;
|
||||
|
||||
int diff = year - eras[i*3 + 0];
|
||||
int i = (low + high) / 2;
|
||||
int diff = year - ERAS[i*3];
|
||||
|
||||
// If years are the same, then compare the months, and if those
|
||||
// are the same, compare days of month. In the ERAS array
|
||||
// months are 1-based for easier maintenance.
|
||||
if (diff == 0) {
|
||||
// Years were the same; compare the months. In the eras
|
||||
// array months are 1-based for easier maintenance.
|
||||
diff = fields[MONTH] - (eras[i*3 + 1] - 1);
|
||||
diff = internalGet(MONTH) - (ERAS[i*3 + 1] - 1);
|
||||
if (diff == 0) {
|
||||
diff = internalGet(DAY_OF_MONTH) - ERAS[i*3 + 2];
|
||||
}
|
||||
}
|
||||
if (diff == 0) {
|
||||
diff = fields[DATE] - eras[i*3 + 2];
|
||||
}
|
||||
if (diff > 0) {
|
||||
if (diff >= 0) {
|
||||
low = i;
|
||||
} else {
|
||||
high = i;
|
||||
}
|
||||
}
|
||||
|
||||
// Now we've found the last era that starts before this date,
|
||||
// so adjust the year to count from the start of that era.
|
||||
fields[ERA] = low;
|
||||
fields[YEAR] = year - eras[low*3 + 0] + 1;
|
||||
// Now we've found the last era that starts before this date, so
|
||||
// adjust the year to count from the start of that era. Note that
|
||||
// all dates before the first era will fall into the first era by
|
||||
// the algorithm.
|
||||
internalSet(ERA, low);
|
||||
internalSet(YEAR, year - ERAS[low*3] + 1);
|
||||
}
|
||||
|
||||
private static final int[] eras = {
|
||||
|
||||
private static final int[] ERAS = {
|
||||
// Gregorian date of each emperor's ascension
|
||||
// Years are AD, months are 1-based.
|
||||
// Year Month Day
|
||||
@ -556,15 +468,61 @@ public class JapaneseCalendar extends GregorianCalendar {
|
||||
// Public constants for some of the recent eras that folks might use...
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
// Constant for the current era. This must be regularly updated.
|
||||
static public final int CURRENT_ERA = (ERAS.length / 3) - 1;
|
||||
|
||||
/** Constant for the era starting on Sept. 8, 1868 AD */
|
||||
static public final int MEIJI = eras.length - 4;
|
||||
static public final int MEIJI = CURRENT_ERA - 3;
|
||||
|
||||
/** Constant for the era starting on July 30, 1912 AD */
|
||||
static public final int TAISHO = eras.length - 3;
|
||||
static public final int TAISHO = CURRENT_ERA - 2;
|
||||
|
||||
/** Constant for the era starting on Dec. 25, 1926 AD */
|
||||
static public final int SHOWA = eras.length - 2;
|
||||
static public final int SHOWA = CURRENT_ERA - 1;
|
||||
|
||||
/** Constant for the era starting on Jan. 7, 1989 AD */
|
||||
static public final int HEISEI = eras.length - 1;
|
||||
};
|
||||
static public final int HEISEI = CURRENT_ERA;
|
||||
|
||||
/**
|
||||
* Partial limits table for limits that differ from GregorianCalendar's.
|
||||
* The YEAR max limits are filled in the first time they are needed.
|
||||
*/
|
||||
private static int LIMITS[][] = {
|
||||
// Minimum Greatest Least Maximum
|
||||
// Minimum Maximum
|
||||
{ 0, 0, CURRENT_ERA, CURRENT_ERA }, // ERA
|
||||
{ 1, 1, 0, 0 }, // YEAR
|
||||
};
|
||||
|
||||
private static boolean YEAR_LIMIT_KNOWN = false;
|
||||
|
||||
/**
|
||||
* Override GregorianCalendar. We should really handle YEAR_WOY and
|
||||
* EXTENDED_YEAR here too to implement the 1..5000000 range, but it's
|
||||
* not critical.
|
||||
*/
|
||||
protected int handleGetLimit(int field, int limitType) {
|
||||
switch (field) {
|
||||
case ERA:
|
||||
return LIMITS[field][limitType];
|
||||
case YEAR:
|
||||
if (!YEAR_LIMIT_KNOWN) {
|
||||
int min = ERAS[3] - ERAS[0];
|
||||
int max = min;
|
||||
for (int i=6; i<ERAS.length; i+=3) {
|
||||
int d = ERAS[i] - ERAS[i-3];
|
||||
if (d < min) {
|
||||
min = d;
|
||||
} else if (d > max) {
|
||||
max = d;
|
||||
}
|
||||
}
|
||||
LIMITS[field][LEAST_MAXIMUM] = min;
|
||||
LIMITS[field][MAXIMUM] = max;
|
||||
}
|
||||
return LIMITS[field][limitType];
|
||||
default:
|
||||
return super.handleGetLimit(field, limitType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,8 +5,8 @@
|
||||
*******************************************************************************
|
||||
*
|
||||
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/util/Attic/BuddhistCalendar.java,v $
|
||||
* $Date: 2000/10/17 18:26:44 $
|
||||
* $Revision: 1.5 $
|
||||
* $Date: 2000/11/18 00:30:54 $
|
||||
* $Revision: 1.6 $
|
||||
*
|
||||
*****************************************************************************************
|
||||
*/
|
||||
@ -35,6 +35,7 @@ import java.util.Locale;
|
||||
* @see com.ibm.util.GregorianCalendar
|
||||
*
|
||||
* @author Laura Werner
|
||||
* @author Alan Liu
|
||||
*/
|
||||
public class BuddhistCalendar extends GregorianCalendar {
|
||||
|
||||
@ -99,8 +100,8 @@ public class BuddhistCalendar extends GregorianCalendar {
|
||||
* @param date The date to which the new calendar is set.
|
||||
*/
|
||||
public BuddhistCalendar(Date date) {
|
||||
super(TimeZone.getDefault(), Locale.getDefault());
|
||||
this.setTime(date);
|
||||
this();
|
||||
setTime(date);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -148,124 +149,42 @@ public class BuddhistCalendar extends GregorianCalendar {
|
||||
// take care of that....
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Override of the <code>GregorianCalendar</code> method that computes the
|
||||
* fields such as <code>YEAR</code>, <code>MONTH</code>, and <code>DATE</code>
|
||||
* from the date in milliseconds since 1/1/1970 AD.
|
||||
*
|
||||
* This method calls {@link GregorianCalendar#computeFields} to do most
|
||||
* of the work,
|
||||
* then adjusts the {@link #YEAR YEAR} and {@link #ERA ERA} fields to use the
|
||||
* Buddhist Era rather than the Gregorian {@link #AD AD} or {@link #BC BC}.
|
||||
*/
|
||||
protected void computeFields() {
|
||||
// Let GregorianCalendar do its thing.
|
||||
super.computeFields();
|
||||
|
||||
// Now adjust the year and era to the proper Buddhist values
|
||||
fromGregorian();
|
||||
|
||||
//
|
||||
// If we're in strict mode and the year is less than 1, fail.
|
||||
// But do this after setting both the year and era to the right values
|
||||
// anyway, so that this object is in a consistent state.
|
||||
//
|
||||
if (!isLenient() && fields[YEAR] < 1) {
|
||||
throw new IllegalArgumentException("Time before start of Buddhist era");
|
||||
}
|
||||
}
|
||||
// Starts in -543 AD, ie 544 BC
|
||||
private static final int BUDDHIST_ERA_START = -543;
|
||||
|
||||
/**
|
||||
* Override of the <code>GregorianCalendar</code> method that computes the
|
||||
* elapsed time in milliseconds since 1/1/1970 AD from the fields such
|
||||
* as <code>YEAR</code>, <code>MONTH</code>, and <code>DATE</code>.
|
||||
*
|
||||
* This method adjusts the {@link #YEAR YEAR} and {@link #ERA ERA} from their
|
||||
* values in the Buddhist calendar to the corresponding Gregorian values, calls
|
||||
* {@link GregorianCalendar#computeTime} to do the real millisecond
|
||||
* calculation, and then restores the Buddhist <code>YEAR</code> and
|
||||
* <code>ERA</code>.
|
||||
*/
|
||||
protected void computeTime() {
|
||||
int year = fields[YEAR];
|
||||
int era = fields[ERA];
|
||||
|
||||
if (!isLenient()) {
|
||||
if (era != BE) {
|
||||
throw new IllegalArgumentException("Illegal value for ERA");
|
||||
}
|
||||
if (year < 1) {
|
||||
throw new IllegalArgumentException("YEAR must be greater than 0");
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
toGregorian();
|
||||
super.computeTime();
|
||||
}
|
||||
finally {
|
||||
// Set the year and era back to the Buddhist values, even if
|
||||
// GregorianCalendar fails because other fields are invalid.
|
||||
fields[YEAR] = year;
|
||||
fields[ERA] = era;
|
||||
}
|
||||
}
|
||||
|
||||
public void add(int field, int amount) {
|
||||
toGregorian();
|
||||
try {
|
||||
super.add(field, amount);
|
||||
}
|
||||
finally {
|
||||
fromGregorian();
|
||||
}
|
||||
}
|
||||
|
||||
public void roll(int field, int amount) {
|
||||
toGregorian();
|
||||
try {
|
||||
super.roll(field, amount);
|
||||
}
|
||||
finally {
|
||||
fromGregorian();
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Methods for converting between Gregorian and Buddhist calendars
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
private static int BUDDHIST_ERA_START = -543; // Starts in -543 AD, ie 544 BC
|
||||
|
||||
/**
|
||||
* Convert the YEAR and ERA fields from Buddhist to Gregorian values
|
||||
* Return the (Buddhist) value of the YEAR field on input;
|
||||
*/
|
||||
private void toGregorian() {
|
||||
int year = fields[YEAR];
|
||||
|
||||
if (year > 0) {
|
||||
fields[YEAR] = year + BUDDHIST_ERA_START;
|
||||
fields[ERA] = AD;
|
||||
protected int handleGetExtendedYear() {
|
||||
int year;
|
||||
if (newerField(EXTENDED_YEAR, YEAR) == EXTENDED_YEAR) {
|
||||
year = internalGet(EXTENDED_YEAR, 1);
|
||||
} else {
|
||||
fields[YEAR] = 1 - year - BUDDHIST_ERA_START;
|
||||
fields[ERA] = BC;
|
||||
// Ignore the era, as there is only one
|
||||
year = internalGet(YEAR, 1);
|
||||
}
|
||||
return year;
|
||||
}
|
||||
|
||||
|
||||
// Return JD of start of given month/year
|
||||
protected int handleComputeMonthStart(int eyear, int month) {
|
||||
return super.handleComputeMonthStart(eyear + BUDDHIST_ERA_START, month);
|
||||
}
|
||||
|
||||
protected void handleComputeFields(int julianDay) {
|
||||
super.handleComputeFields(julianDay);
|
||||
int y = internalGet(EXTENDED_YEAR) - BUDDHIST_ERA_START;
|
||||
internalSet(EXTENDED_YEAR, y);
|
||||
internalSet(ERA, 0);
|
||||
internalSet(YEAR, y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adjust the year and era from Gregorian to Buddhist values
|
||||
* Override GregorianCalendar. There is only one Buddhist ERA. We
|
||||
* should really handle YEAR, YEAR_WOY, and EXTENDED_YEAR here too to
|
||||
* implement the 1..5000000 range, but it's not critical.
|
||||
*/
|
||||
private void fromGregorian() {
|
||||
// Now adjust the year and era to the proper Buddhist values
|
||||
int year = fields[YEAR];
|
||||
|
||||
if (fields[ERA] == BC) {
|
||||
fields[YEAR] = 1 - year - BUDDHIST_ERA_START;
|
||||
} else {
|
||||
fields[YEAR] = year - BUDDHIST_ERA_START;
|
||||
protected int handleGetLimit(int field, int limitType) {
|
||||
if (field == ERA) {
|
||||
return BE;
|
||||
}
|
||||
fields[ERA] = BE;
|
||||
return super.handleGetLimit(field, limitType);
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
@ -5,8 +5,8 @@
|
||||
*******************************************************************************
|
||||
*
|
||||
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/util/Attic/HebrewCalendar.java,v $
|
||||
* $Date: 2000/10/27 22:25:52 $
|
||||
* $Revision: 1.5 $
|
||||
* $Date: 2000/11/18 00:30:54 $
|
||||
* $Revision: 1.6 $
|
||||
*
|
||||
*****************************************************************************************
|
||||
*/
|
||||
@ -64,6 +64,7 @@ import java.util.Locale;
|
||||
* @see com.ibm.util.GregorianCalendar
|
||||
*
|
||||
* @author Laura Werner
|
||||
* @author Alan Liu
|
||||
*/
|
||||
public class HebrewCalendar extends Calendar {
|
||||
|
||||
@ -124,35 +125,31 @@ public class HebrewCalendar extends Calendar {
|
||||
*/
|
||||
private static final long EPOCH_MILLIS = -180799862400000L; // 1/1/1 HY
|
||||
|
||||
// Useful millisecond constants
|
||||
private static final int SECOND_MS = 1000;
|
||||
private static final int MINUTE_MS = 60*SECOND_MS;
|
||||
private static final int HOUR_MS = 60*MINUTE_MS;
|
||||
private static final long DAY_MS = 24*HOUR_MS;
|
||||
private static final long WEEK_MS = 7*DAY_MS;
|
||||
|
||||
/**
|
||||
* The minimum and maximum values for all of the fields, for validation
|
||||
*/
|
||||
private static final int MinMax[][] = {
|
||||
// Min Greatest Min Least Max Max
|
||||
{ 0, 0, 0, 0 }, // ERA
|
||||
{ 1, 1, 5000000, 5000000 }, // YEAR
|
||||
{ 0, 0, 12, 12 }, // MONTH
|
||||
{ 0, 0, 51, 56 }, // WEEK_OF_YEAR
|
||||
{ 0, 0, 5, 6 }, // WEEK_OF_MONTH
|
||||
{ 1, 1, 29, 30 }, // DAY_OF_MONTH
|
||||
{ 1, 1, 353, 385 }, // DAY_OF_YEAR
|
||||
{ 1, 1, 7, 7 }, // DAY_OF_WEEK
|
||||
{ -1, -1, 4, 6 }, // DAY_OF_WEEK_IN_MONTH
|
||||
{ 0, 0, 1, 1 }, // AM_PM
|
||||
{ 0, 0, 11, 11 }, // HOUR
|
||||
{ 0, 0, 23, 23 }, // HOUR_OF_DAY
|
||||
{ 0, 0, 59, 59 }, // MINUTE
|
||||
{ 0, 0, 59, 59 }, // SECOND
|
||||
{ 0, 0, 999, 999 }, // MILLISECOND
|
||||
{ -12*HOUR_MS, -12*HOUR_MS, 12*HOUR_MS, 12*HOUR_MS }, // ZONE_OFFSET
|
||||
{ 0, 0, 1*HOUR_MS, 1*HOUR_MS },
|
||||
private static final int LIMITS[][] = {
|
||||
// Minimum Greatest Least Maximum
|
||||
// Minimum Maximum
|
||||
{ 0, 0, 0, 0 }, // ERA
|
||||
{ 1, 1, 5000000, 5000000 }, // YEAR
|
||||
{ 0, 0, 12, 12 }, // MONTH
|
||||
{ 1, 1, 51, 56 }, // WEEK_OF_YEAR
|
||||
{ 0, 0, 5, 6 }, // WEEK_OF_MONTH
|
||||
{ 1, 1, 29, 30 }, // DAY_OF_MONTH
|
||||
{ 1, 1, 353, 385 }, // DAY_OF_YEAR
|
||||
{/* */}, // DAY_OF_WEEK
|
||||
{ -1, -1, 4, 6 }, // DAY_OF_WEEK_IN_MONTH
|
||||
{/* */}, // AM_PM
|
||||
{/* */}, // HOUR
|
||||
{/* */}, // HOUR_OF_DAY
|
||||
{/* */}, // MINUTE
|
||||
{/* */}, // SECOND
|
||||
{/* */}, // MILLISECOND
|
||||
{/* */}, // ZONE_OFFSET
|
||||
{/* */}, // DST_OFFSET
|
||||
{ -5000001, -5000001, 5000001, 5000001 }, // YEAR_WOY
|
||||
{/* */}, // DOW_LOCAL
|
||||
{ -5000000, -5000000, 5000000, 5000000 }, // EXTENDED_YEAR
|
||||
{/* */}, // JULIAN_DAY
|
||||
{/* */}, // MILLISECONDS_IN_DAY
|
||||
};
|
||||
|
||||
/**
|
||||
@ -184,7 +181,7 @@ public class HebrewCalendar extends Calendar {
|
||||
* Although this can be calculated from the MONTH_LENGTH table,
|
||||
* keeping it around separately makes some calculations a lot faster
|
||||
*/
|
||||
private static final int NUM_DAYS[][] = {
|
||||
private static final int MONTH_START[][] = {
|
||||
// Deficient Normal Complete
|
||||
{ 0, 0, 0 }, // (placeholder)
|
||||
{ 30, 30, 30 }, // Tishri
|
||||
@ -205,7 +202,7 @@ public class HebrewCalendar extends Calendar {
|
||||
/**
|
||||
* The cumulative # of days to the end of each month in a leap year
|
||||
*/
|
||||
private static final int LEAP_NUM_DAYS[][] = {
|
||||
private static final int LEAP_MONTH_START[][] = {
|
||||
// Deficient Normal Complete
|
||||
{ 0, 0, 0 }, // (placeholder)
|
||||
{ 30, 30, 30 }, // Tishri
|
||||
@ -227,14 +224,6 @@ public class HebrewCalendar extends Calendar {
|
||||
// Data Members...
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Since TimeZone rules are all defined in terms of GregorianCalendar,
|
||||
* we need a GregorianCalendar object for doing time zone calculations
|
||||
* There's no point in lazy-allocating this since it's needed for
|
||||
* almost anything this class does.
|
||||
*/
|
||||
private static GregorianCalendar gregorian = new GregorianCalendar();
|
||||
|
||||
private static CalendarCache cache = new CalendarCache();
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
@ -340,99 +329,6 @@ public class HebrewCalendar extends Calendar {
|
||||
this.set(SECOND, second);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Minimum / Maximum access functions
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Returns the minimum value for the given field.
|
||||
* e.g. for DAY_OF_MONTH, 1
|
||||
*
|
||||
* @param field The field whose minimum value is desired.
|
||||
*
|
||||
* @see com.ibm.util.Calendar#getMinimum
|
||||
*/
|
||||
public int getMinimum(int field)
|
||||
{
|
||||
return MinMax[field][0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the highest minimum value for the given field. For the Hebrew
|
||||
* calendar, this always returns the same result as <code>getMinimum</code>.
|
||||
*
|
||||
* @param field The field whose greatest minimum value is desired.
|
||||
*
|
||||
* @see #getMinimum
|
||||
*/
|
||||
public int getGreatestMinimum(int field)
|
||||
{
|
||||
return MinMax[field][1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the maximum value for the given field.
|
||||
* e.g. for {@link #DAY_OF_MONTH DAY_OF_MONTH}, 30
|
||||
*
|
||||
* @param field The field whose maximum value is desired.
|
||||
*
|
||||
* @see #getLeastMaximum
|
||||
* @see #getActualMaximum
|
||||
*/
|
||||
public int getMaximum(int field)
|
||||
{
|
||||
return MinMax[field][3];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the lowest maximum value for the given field. For most fields,
|
||||
* this returns the same result as {@link #getMaximum getMaximum}. However,
|
||||
* for some fields this can be a lower number. For example,
|
||||
* the maximum {@link #DAY_OF_MONTH DAY_OF_MONTH} in the Hebrew caleandar varies
|
||||
* from month to month, so this method returns 29 while <code>getMaximum</code>
|
||||
* returns 30.
|
||||
*
|
||||
* @param field The field whose least maximum value is desired.
|
||||
*
|
||||
* @see #getMaximum
|
||||
* @see #getActualMaximum
|
||||
*/
|
||||
public int getLeastMaximum(int field)
|
||||
{
|
||||
return MinMax[field][2];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the maximum value that a field could have, given the current date.
|
||||
* For example, with the date "Kislev 3, 5757" and the {@link #DAY_OF_MONTH DAY_OF_MONTH} field,
|
||||
* the actual maximum would be 29; for "Kislev 3, 5758" it would be 30,
|
||||
* since the length of the month Kislev varies from year to year.
|
||||
*
|
||||
* @param field The field whose actual maximum value is desired.
|
||||
*
|
||||
* @see #getMaximum
|
||||
* @see #getLeastMaximum
|
||||
*/
|
||||
public int getActualMaximum(int field)
|
||||
{
|
||||
if (!isSet(YEAR) || !isSet(MONTH)) {
|
||||
complete();
|
||||
}
|
||||
switch (field) {
|
||||
case MONTH:
|
||||
return isLeapYear(fields[YEAR]) ? 13 : 12;
|
||||
|
||||
case DAY_OF_MONTH:
|
||||
return monthLength(fields[YEAR], fields[MONTH]);
|
||||
|
||||
case DAY_OF_YEAR:
|
||||
return yearLength(fields[YEAR]);
|
||||
|
||||
default:
|
||||
return super.getActualMaximum(field);
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Rolling and adding functions overridden from Calendar
|
||||
//
|
||||
@ -469,34 +365,70 @@ public class HebrewCalendar extends Calendar {
|
||||
public void add(int field, int amount)
|
||||
{
|
||||
switch (field) {
|
||||
case MONTH:
|
||||
case MONTH:
|
||||
{
|
||||
//
|
||||
// MONTH is tricky, because the number of months per year varies
|
||||
// It's easiest to just convert to an absolute # of months
|
||||
// since the epoch, do the addition, and convert back.
|
||||
//
|
||||
int month = (235 * get(YEAR) - 234) / 19 + get(MONTH);
|
||||
month += amount;
|
||||
//~ // This seems like a good idea but it isn't working :( - Alan
|
||||
|
||||
// Now convert back to year and month values
|
||||
int year = (19 * month + 234) / 235;
|
||||
month -= (235 * year - 234) / 19;
|
||||
|
||||
// In a non-leap year, months after the (missing) leap month
|
||||
// must be bumped up by one.
|
||||
// TODO: but only if we started before the leap month
|
||||
if (month >= ADAR_1 && !isLeapYear(year)) {
|
||||
month++;
|
||||
//~ // MONTH is tricky, because the number of months per year varies
|
||||
//~ // It's easiest to just convert to an absolute # of months
|
||||
//~ // since the epoch, do the addition, and convert back.
|
||||
//~ int month = (235 * get(YEAR) - 234) / 19 + get(MONTH);
|
||||
//~ month += amount;
|
||||
|
||||
//~ // Now convert back to year and month values
|
||||
//~ int year = (19 * month + 234) / 235;
|
||||
//~ month -= (235 * year - 234) / 19;
|
||||
//~
|
||||
//~ // In a non-leap year, months after the (missing) leap month
|
||||
//~ // must be bumped up by one.
|
||||
//~ // TODO: but only if we started before the leap month
|
||||
//~ if (month >= ADAR_1 && !isLeapYear(year)) {
|
||||
//~ month++;
|
||||
//~ }
|
||||
//~ set(YEAR, year);
|
||||
//~ set(MONTH, month);
|
||||
//~ pinField(DAY_OF_MONTH);
|
||||
|
||||
// We can't just do a set(MONTH, get(MONTH) + amount). The
|
||||
// reason is ADAR_1. Suppose amount is +2 and we land in
|
||||
// ADAR_1 -- then we have to bump to ADAR_2 aka ADAR. But
|
||||
// if amount is -2 and we land in ADAR_1, then we have to
|
||||
// bump the other way -- down to SHEVAT. - Alan 11/00
|
||||
int month = get(MONTH);
|
||||
int year = get(YEAR);
|
||||
//public static final int SHEVAT = 4;
|
||||
//public static final int ADAR_1 = 5;
|
||||
//public static final int ADAR = 6;
|
||||
if (amount > 0) {
|
||||
while (amount-- > 0) {
|
||||
++month;
|
||||
if (month == ADAR_1 && !isLeapYear(year)) {
|
||||
++month;
|
||||
}
|
||||
if (month > ELUL) { // Last month of year
|
||||
month = 0;
|
||||
++year;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
while (amount++ < 0) {
|
||||
--month;
|
||||
if (month == ADAR_1 && !isLeapYear(year)) {
|
||||
--month;
|
||||
}
|
||||
if (month < 0) {
|
||||
month = ELUL; // Last month of year
|
||||
--year;
|
||||
}
|
||||
}
|
||||
}
|
||||
this.set(YEAR, year);
|
||||
this.set(MONTH, month);
|
||||
|
||||
set(MONTH, month);
|
||||
set(YEAR, year);
|
||||
pinField(DAY_OF_MONTH);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
|
||||
default:
|
||||
super.add(field, amount);
|
||||
break;
|
||||
}
|
||||
@ -536,18 +468,17 @@ public class HebrewCalendar extends Calendar {
|
||||
public void roll(int field, int amount)
|
||||
{
|
||||
switch (field) {
|
||||
case MONTH:
|
||||
case MONTH:
|
||||
{
|
||||
int month = get(MONTH);
|
||||
int year = get(YEAR);
|
||||
|
||||
boolean leapYear = isLeapYear(year);
|
||||
int yearLength = leapYear ? 13 : 12;
|
||||
int yearLength = monthsInYear(year);
|
||||
int newMonth = month + (amount % yearLength);
|
||||
//
|
||||
// If it's not a leap year and we're rolling past the missing month
|
||||
// of ADAR_1, we need to roll an extra month to make up for it.
|
||||
// TODO: fix cases like Av + 12 -> Tammuz
|
||||
//
|
||||
if (!leapYear) {
|
||||
if (amount > 0 && month < ADAR_1 && newMonth >= ADAR_1) {
|
||||
@ -560,385 +491,13 @@ public class HebrewCalendar extends Calendar {
|
||||
pinField(DAY_OF_MONTH);
|
||||
return;
|
||||
}
|
||||
default:
|
||||
default:
|
||||
super.roll(field, amount);
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Functions for converting from field values to milliseconds and back...
|
||||
//
|
||||
// These are overrides of abstract methods on com.ibm.util.Calendar
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Converts time field values to UTC as milliseconds.
|
||||
*
|
||||
* @exception IllegalArgumentException if an unknown field is given.
|
||||
*/
|
||||
protected void computeTime()
|
||||
{
|
||||
if (isTimeSet) return;
|
||||
|
||||
if (!isLenient() && !validateFields())
|
||||
throw new IllegalArgumentException("Invalid field values for HebrewCalendar");
|
||||
|
||||
if (isSet(ERA) && internalGet(ERA) != 0)
|
||||
throw new IllegalArgumentException("ERA out of range in HebrewCalendar");
|
||||
|
||||
// The year is required. We don't have to check if it's unset,
|
||||
// because if it is, by definition it will be 0.
|
||||
|
||||
int year = internalGet(YEAR);
|
||||
long dayNumber = 0, date = 0;
|
||||
|
||||
if (year <= 0) {
|
||||
throw new IllegalArgumentException("YEAR out of range in HebrewCalendar");
|
||||
}
|
||||
|
||||
// The following code is somewhat convoluted. The various nested
|
||||
// if's handle the different cases of what fields are present.
|
||||
if (isSet(MONTH) &&
|
||||
(isSet(DATE) ||
|
||||
(isSet(DAY_OF_WEEK) &&
|
||||
(isSet(WEEK_OF_MONTH) || isSet(DAY_OF_WEEK_IN_MONTH))
|
||||
)))
|
||||
{
|
||||
// We have the month specified. Make it 1-based for the algorithm.
|
||||
int month = internalGet(MONTH);
|
||||
|
||||
// normalize month
|
||||
// TODO: I think this is wrong, since months/year can vary
|
||||
if (month < 0) {
|
||||
year += month / 13 - 1;
|
||||
month = 13 + month % 13;
|
||||
} else if (month > 12) {
|
||||
year += month / 13;
|
||||
month = month % 13;
|
||||
}
|
||||
|
||||
dayNumber = startOfYear(year);
|
||||
if (isLeapYear(year)) {
|
||||
dayNumber += LEAP_NUM_DAYS[month][yearType(year)];
|
||||
} else {
|
||||
dayNumber += NUM_DAYS[month][yearType(year)];
|
||||
}
|
||||
|
||||
if (isSet(DATE))
|
||||
{
|
||||
date = internalGet(DATE);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Compute from day of week plus week number or from the day of
|
||||
// week plus the day of week in month. The computations are
|
||||
// almost identical.
|
||||
|
||||
// Find the day of the week for the first of this month. This
|
||||
// is zero-based, with 0 being the locale-specific first day of
|
||||
// the week. Add 1 to get the 1st day of month. Subtract
|
||||
// getFirstDayOfWeek() to make 0-based.
|
||||
int fdm = absoluteDayToDayOfWeek(dayNumber + 1) - getFirstDayOfWeek();
|
||||
if (fdm < 0) fdm += 7;
|
||||
|
||||
// Find the start of the first week. This will be a date from
|
||||
// 1..-6. It represents the locale-specific first day of the
|
||||
// week of the first day of the month, ignoring minimal days in
|
||||
// first week.
|
||||
date = 1 - fdm + internalGet(DAY_OF_WEEK) - getFirstDayOfWeek();
|
||||
|
||||
if (isSet(WEEK_OF_MONTH))
|
||||
{
|
||||
// Adjust for minimal days in first week.
|
||||
if ((7 - fdm) < getMinimalDaysInFirstWeek()) date += 7;
|
||||
|
||||
// Now adjust for the week number.
|
||||
date += 7 * (internalGet(WEEK_OF_MONTH) - 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Adjust into the month, if needed.
|
||||
if (date < 1) date += 7;
|
||||
|
||||
// We are basing this on the day-of-week-in-month. The only
|
||||
// trickiness occurs if the day-of-week-in-month is
|
||||
// negative.
|
||||
int dim = internalGet(DAY_OF_WEEK_IN_MONTH);
|
||||
if (dim >= 0) {
|
||||
date += 7*(dim - 1);
|
||||
} else {
|
||||
// Move date to the last of this day-of-week in this
|
||||
// month, then back up as needed. If dim==-1, we don't
|
||||
// back up at all. If dim==-2, we back up once, etc.
|
||||
// Don't back up past the first of the given day-of-week
|
||||
// in this month. Note that we handle -2, -3,
|
||||
// etc. correctly, even though values < -1 are
|
||||
// technically disallowed.
|
||||
date += ((monthLength(year, fields[MONTH]) - date) / 7 + dim + 1) * 7;
|
||||
}
|
||||
}
|
||||
}
|
||||
dayNumber += date;
|
||||
}
|
||||
else if (isSet(DAY_OF_YEAR)) {
|
||||
dayNumber = startOfYear(year) + internalGet(DAY_OF_YEAR);
|
||||
}
|
||||
else if (isSet(DAY_OF_WEEK) && isSet(WEEK_OF_YEAR))
|
||||
{
|
||||
dayNumber = startOfYear(year);
|
||||
|
||||
// Compute from day of week plus week of year
|
||||
|
||||
// Find the day of the week for the first of this year. This
|
||||
// is zero-based, with 0 being the locale-specific first day of
|
||||
// the week. Add 1 to get the 1st day of month. Subtract
|
||||
// getFirstDayOfWeek() to make 0-based.
|
||||
int fdy = absoluteDayToDayOfWeek(dayNumber + 1) - getFirstDayOfWeek();
|
||||
if (fdy < 0) fdy += 7;
|
||||
|
||||
// Find the start of the first week. This may be a valid date
|
||||
// from 1..7, or a date before the first, from 0..-6. It
|
||||
// represents the locale-specific first day of the week
|
||||
// of the first day of the year.
|
||||
|
||||
// First ignore the minimal days in first week.
|
||||
date = 1 - fdy + internalGet(DAY_OF_WEEK) - getFirstDayOfWeek();
|
||||
|
||||
// Adjust for minimal days in first week.
|
||||
if ((7 - fdy) < getMinimalDaysInFirstWeek()) date += 7;
|
||||
|
||||
// Now adjust for the week number.
|
||||
date += 7 * (internalGet(WEEK_OF_YEAR) - 1);
|
||||
|
||||
dayNumber += date;
|
||||
}
|
||||
else { // Not enough information
|
||||
throw new IllegalArgumentException("Not enough fields set to calculate time");
|
||||
}
|
||||
|
||||
long millis = dayNumber * DAY_MS + EPOCH_MILLIS;
|
||||
|
||||
// Now we can do the time portion of the conversion.
|
||||
int millisInDay = 0;
|
||||
|
||||
// Hours
|
||||
if (isSet(HOUR_OF_DAY))
|
||||
// Don't normalize here; let overflow bump into the next period.
|
||||
// This is consistent with how we handle other fields.
|
||||
millisInDay += internalGet(HOUR_OF_DAY);
|
||||
|
||||
else if (isSet(HOUR))
|
||||
{
|
||||
// Don't normalize here; let overflow bump into the next period.
|
||||
// This is consistent with how we handle other fields.
|
||||
millisInDay += internalGet(HOUR);
|
||||
millisInDay += 12 * internalGet(AM_PM);
|
||||
}
|
||||
|
||||
// Minutes. We use the fact that unset == 0
|
||||
millisInDay *= 60;
|
||||
millisInDay += internalGet(MINUTE);
|
||||
|
||||
// Seconds. unset == 0
|
||||
millisInDay *= 60;
|
||||
millisInDay += internalGet(SECOND);
|
||||
|
||||
// Milliseconds. unset == 0
|
||||
millisInDay *= 1000;
|
||||
millisInDay += internalGet(MILLISECOND);
|
||||
|
||||
// Now add date and millisInDay together, to make millis contain local wall
|
||||
// millis, with no zone or DST adjustments
|
||||
millis += millisInDay;
|
||||
|
||||
//
|
||||
// Compute the time zone offset and DST offset.
|
||||
// Since the TimeZone API expects the Gregorian year, month, etc.,
|
||||
// We have to convert to local Gregorian time in order to
|
||||
// figure out the time zone calculations. This is a bit slow, but
|
||||
// it saves us from doing some *really* nasty calculations here.
|
||||
//
|
||||
TimeZone zone = getTimeZone();
|
||||
int dstOffset = 0;
|
||||
|
||||
if (zone.useDaylightTime()) {
|
||||
synchronized(gregorian) {
|
||||
gregorian.setTimeZone(zone);
|
||||
gregorian.setTime(new Date(millis)); // "millis" is local wall clock time
|
||||
dstOffset = gregorian.get(DST_OFFSET);
|
||||
}
|
||||
}
|
||||
// Store our final computed GMT time, with timezone adjustments.
|
||||
time = millis - dstOffset - zone.getRawOffset();
|
||||
isTimeSet = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the value of the given time field.
|
||||
*/
|
||||
private boolean boundsCheck(int value, int field)
|
||||
{
|
||||
return value >= getMinimum(field) && value <= getMaximum(field);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Validates the values of the set time fields.
|
||||
*/
|
||||
private boolean validateFields()
|
||||
{
|
||||
for (int field = 0; field < FIELD_COUNT; field++)
|
||||
{
|
||||
// Ignore DATE and DAY_OF_YEAR which are handled below
|
||||
if (field != DATE &&
|
||||
field != DAY_OF_YEAR &&
|
||||
isSet(field) &&
|
||||
!boundsCheck(internalGet(field), field))
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Values differ in Least-Maximum and Maximum should be handled
|
||||
// specially.
|
||||
if (isSet(DATE))
|
||||
{
|
||||
int date = internalGet(DATE);
|
||||
return (date >= getMinimum(DATE) &&
|
||||
date <= monthLength(fields[YEAR], fields[MONTH]));
|
||||
}
|
||||
|
||||
if (isSet(DAY_OF_YEAR))
|
||||
{
|
||||
int days = internalGet(DAY_OF_YEAR);
|
||||
|
||||
if (days < 1 || days > yearLength(internalGet(YEAR)))
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isSet(YEAR))
|
||||
{
|
||||
int year = internalGet(YEAR);
|
||||
if (year < 1)
|
||||
return false;
|
||||
}
|
||||
|
||||
// Handle DAY_OF_WEEK_IN_MONTH, which must not have the value zero.
|
||||
// We've checked against minimum and maximum above already.
|
||||
if (isSet(DAY_OF_WEEK_IN_MONTH) &&
|
||||
0 == internalGet(DAY_OF_WEEK_IN_MONTH)) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the time as milliseconds since 1/1/1970 to the Calendar fields
|
||||
* such as YEAR, MONTH and DAY.
|
||||
*/
|
||||
protected void computeFields()
|
||||
{
|
||||
if (areFieldsSet) return;
|
||||
|
||||
// The following algorithm only works for dates from the start of the Hebrew
|
||||
// calendar onward.
|
||||
if (time < EPOCH_MILLIS && !isLenient()) {
|
||||
throw new IllegalArgumentException("HebrewCalendar does not handle dates before 1/1/1 AM");
|
||||
}
|
||||
|
||||
//
|
||||
// Compute the time zone offset and DST offset.
|
||||
// Since the TimeZone API expects the Gregorian year, month, etc.,
|
||||
// We have to convert to local Gregorian time in order to
|
||||
// figure out the time zone calculations. This is a bit slow, but
|
||||
// it saves us from doing some *really* nasty calculations here.
|
||||
//
|
||||
TimeZone zone = getTimeZone();
|
||||
int rawOffset = zone.getRawOffset();
|
||||
int dstOffset = 0; // Extra DST offset
|
||||
|
||||
if (zone.useDaylightTime()) {
|
||||
synchronized(gregorian) {
|
||||
gregorian.setTimeZone(zone);
|
||||
gregorian.setTime(new Date(time));
|
||||
dstOffset = gregorian.get(DST_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
long localMillis = time + rawOffset + dstOffset;
|
||||
|
||||
// We need to find out which Hebrew year the given time is in.
|
||||
// Once we know that, we find the time when the year started,
|
||||
// and everything else is straightforward
|
||||
|
||||
long epochMillis = localMillis - EPOCH_MILLIS; // Millis since epoch
|
||||
long d = epochMillis / DAY_MS; // Days
|
||||
long m = (d * DAY_PARTS) / MONTH_PARTS; // Months (approx)
|
||||
|
||||
int year = (int)((19 * m + 234) / 235) + 1; // Years (approx)
|
||||
long ys = startOfYear(year); // 1st day of year
|
||||
int dayOfYear = (int)(d - ys);
|
||||
|
||||
// Because of the postponement rules, it's possible to guess wrong. Fix it.
|
||||
while (dayOfYear < 1) {
|
||||
year--;
|
||||
ys = startOfYear(year);
|
||||
dayOfYear = (int)(d - ys);
|
||||
}
|
||||
|
||||
int dayOfWeek = absoluteDayToDayOfWeek((long)d);
|
||||
|
||||
// Now figure out which month we're in, and the date within that month
|
||||
int yearType = yearType(year);
|
||||
int numDays[][] = isLeapYear(year) ? LEAP_NUM_DAYS : NUM_DAYS;
|
||||
|
||||
int month = 0;
|
||||
while (dayOfYear > numDays[month][yearType]) {
|
||||
month++;
|
||||
}
|
||||
month--;
|
||||
int date = dayOfYear - numDays[month][yearType];
|
||||
|
||||
fields[ERA] = 0;
|
||||
fields[YEAR] = year;
|
||||
fields[MONTH] = month;
|
||||
fields[DATE] = date;
|
||||
|
||||
fields[DAY_OF_YEAR] = dayOfYear;
|
||||
fields[DAY_OF_WEEK] = dayOfWeek;
|
||||
|
||||
fields[WEEK_OF_YEAR] = weekNumber(dayOfYear, dayOfWeek);
|
||||
fields[WEEK_OF_MONTH] = weekNumber(date, dayOfWeek);
|
||||
|
||||
fields[DAY_OF_WEEK_IN_MONTH] = (date-1) / 7 + 1;
|
||||
|
||||
//long days = (long) (localMillis / DAY_MS);
|
||||
//int millisInDay = (int) (localMillis - (days * DAY_MS));
|
||||
//if (millisInDay < 0) millisInDay += DAY_MS;
|
||||
|
||||
int millisInDay = (int)(localMillis % DAY_MS);
|
||||
|
||||
// Fill in all time-related fields based on millisInDay.
|
||||
fields[MILLISECOND] = millisInDay % 1000;
|
||||
millisInDay /= 1000;
|
||||
fields[SECOND] = millisInDay % 60;
|
||||
millisInDay /= 60;
|
||||
fields[MINUTE] = millisInDay % 60;
|
||||
millisInDay /= 60;
|
||||
fields[HOUR_OF_DAY] = millisInDay;
|
||||
fields[AM_PM] = millisInDay / 12;
|
||||
fields[HOUR] = millisInDay % 12;
|
||||
|
||||
fields[ZONE_OFFSET] = rawOffset;
|
||||
fields[DST_OFFSET] = dstOffset;
|
||||
|
||||
areFieldsSet = true;
|
||||
|
||||
// Careful here: We are manually setting the isSet flags to true, so we
|
||||
// must be sure that the above code actually does set all these fields.
|
||||
_TEMPORARY_markAllFieldsSet();
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Functions for converting from milliseconds to field values
|
||||
// Support methods
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
// Hebrew date calculations are performed in terms of days, hours, and
|
||||
@ -1011,7 +570,7 @@ public class HebrewCalendar extends Calendar {
|
||||
cache.put(year, day);
|
||||
}
|
||||
return day;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the day of the week for a given day
|
||||
@ -1025,23 +584,15 @@ public class HebrewCalendar extends Calendar {
|
||||
return (int)(day % 7) + 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of days in the given Hebrew year
|
||||
*/
|
||||
private static int yearLength(int year)
|
||||
{
|
||||
return (int)(startOfYear(year+1) - startOfYear(year));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the the type of a given year.
|
||||
* 0 "Deficient" year with 353 or 383 days
|
||||
* 1 "Normal" year with 354 or 384 days
|
||||
* 2 "Complete" year with 355 or 385 days
|
||||
*/
|
||||
private static int yearType(int year)
|
||||
private final int yearType(int year)
|
||||
{
|
||||
int yearLength = yearLength(year);
|
||||
int yearLength = handleGetYearLength(year);
|
||||
|
||||
if (yearLength > 380) {
|
||||
yearLength -= 30; // Subtract length of leap month.
|
||||
@ -1063,16 +614,38 @@ public class HebrewCalendar extends Calendar {
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether a given Hebrew year is a leap year
|
||||
*
|
||||
* The rule here is that if (year % 19) == 0, 3, 6, 8, 11, 14, or 17.
|
||||
* The formula below performs the same test, believe it or not.
|
||||
*/
|
||||
private static final boolean isLeapYear(int year) {
|
||||
return (year * 12 + 17) % 19 >= 12;
|
||||
}
|
||||
|
||||
private static int monthsInYear(int year) {
|
||||
return isLeapYear(year) ? 13 : 12;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Calendar framework
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
protected int handleGetLimit(int field, int limitType) {
|
||||
return LIMITS[field][limitType];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the length of the given month in the given year
|
||||
*/
|
||||
private static int monthLength(int year, int month)
|
||||
{
|
||||
protected int handleGetMonthLength(int extendedYear, int month) {
|
||||
|
||||
switch (month) {
|
||||
case HESHVAN:
|
||||
case KISLEV:
|
||||
// These two month lengths can vary
|
||||
return MONTH_LENGTH[month][yearType(year)];
|
||||
return MONTH_LENGTH[month][yearType(extendedYear)];
|
||||
|
||||
default:
|
||||
// The rest are a fixed length
|
||||
@ -1081,20 +654,113 @@ public class HebrewCalendar extends Calendar {
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether a given Hebrew year is a leap year
|
||||
*
|
||||
* The rule here is that if (year % 19) == 0, 3, 6, 8, 11, 14, or 17.
|
||||
* The formula below performs the same test, believe it or not.
|
||||
* Returns the number of days in the given Hebrew year
|
||||
*/
|
||||
private static boolean isLeapYear(int year)
|
||||
{
|
||||
return (year * 12 + 17) % 19 >= 12;
|
||||
protected int handleGetYearLength(int eyear) {
|
||||
return (int)(startOfYear(eyear+1) - startOfYear(eyear));
|
||||
}
|
||||
|
||||
|
||||
static private void debug(String str) {
|
||||
if (false) {
|
||||
System.out.println(str);
|
||||
//-------------------------------------------------------------------------
|
||||
// Functions for converting from milliseconds to field values
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Subclasses may override this method to compute several fields
|
||||
* specific to each calendar system. These are:
|
||||
*
|
||||
* <ul><li>ERA
|
||||
* <li>YEAR
|
||||
* <li>MONTH
|
||||
* <li>DAY_OF_MONTH
|
||||
* <li>DAY_OF_YEAR
|
||||
* <li>EXTENDED_YEAR</ul>
|
||||
*
|
||||
* Subclasses can refer to the DAY_OF_WEEK and DOW_LOCAL fields,
|
||||
* which will be set when this method is called. Subclasses can
|
||||
* also call the getGregorianXxx() methods to obtain Gregorian
|
||||
* calendar equivalents for the given Julian day.
|
||||
*
|
||||
* <p>In addition, subclasses should compute any subclass-specific
|
||||
* fields, that is, fields from BASE_FIELD_COUNT to
|
||||
* getFieldCount() - 1.
|
||||
*/
|
||||
protected void handleComputeFields(int julianDay) {
|
||||
long d = julianDay - 347997;
|
||||
long m = (d * DAY_PARTS) / MONTH_PARTS; // Months (approx)
|
||||
int year = (int)((19 * m + 234) / 235) + 1; // Years (approx)
|
||||
long ys = startOfYear(year); // 1st day of year
|
||||
int dayOfYear = (int)(d - ys);
|
||||
|
||||
// Because of the postponement rules, it's possible to guess wrong. Fix it.
|
||||
while (dayOfYear < 1) {
|
||||
year--;
|
||||
ys = startOfYear(year);
|
||||
dayOfYear = (int)(d - ys);
|
||||
}
|
||||
|
||||
// Now figure out which month we're in, and the date within that month
|
||||
int yearType = yearType(year);
|
||||
int monthStart[][] = isLeapYear(year) ? LEAP_MONTH_START : MONTH_START;
|
||||
|
||||
int month = 0;
|
||||
while (dayOfYear > monthStart[month][yearType]) {
|
||||
month++;
|
||||
}
|
||||
month--;
|
||||
int dayOfMonth = dayOfYear - monthStart[month][yearType];
|
||||
|
||||
internalSet(ERA, 0);
|
||||
internalSet(YEAR, year);
|
||||
internalSet(EXTENDED_YEAR, year);
|
||||
internalSet(MONTH, month);
|
||||
internalSet(DAY_OF_MONTH, dayOfMonth);
|
||||
internalSet(DAY_OF_YEAR, dayOfYear);
|
||||
}
|
||||
};
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Functions for converting from field values to milliseconds
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
protected int handleGetExtendedYear() {
|
||||
int year;
|
||||
if (newerField(EXTENDED_YEAR, YEAR) == EXTENDED_YEAR) {
|
||||
year = internalGet(EXTENDED_YEAR, 1); // Default to year 1
|
||||
} else {
|
||||
year = internalGet(YEAR, 1); // Default to year 1
|
||||
}
|
||||
return year;
|
||||
}
|
||||
|
||||
// Return JD of start of given month/year
|
||||
protected int handleComputeMonthStart(int eyear, int month) {
|
||||
|
||||
// Resolve out-of-range months. This is necessary in order to
|
||||
// obtain the correct year.
|
||||
if (month < 0) {
|
||||
while (month < 0) {
|
||||
month += monthsInYear(--eyear);
|
||||
}
|
||||
} else if (month > 0) {
|
||||
for (;;) {
|
||||
int monthsInYear = monthsInYear(eyear);
|
||||
if (month < monthsInYear) {
|
||||
break;
|
||||
}
|
||||
++eyear;
|
||||
month -= monthsInYear;
|
||||
}
|
||||
}
|
||||
|
||||
long day = startOfYear(eyear);
|
||||
|
||||
if (month != 0) {
|
||||
if (isLeapYear(eyear)) {
|
||||
day += LEAP_MONTH_START[month][yearType(eyear)];
|
||||
} else {
|
||||
day += MONTH_START[month][yearType(eyear)];
|
||||
}
|
||||
}
|
||||
|
||||
return (int) (day + 347997);
|
||||
}
|
||||
}
|
||||
|
@ -5,8 +5,8 @@
|
||||
*******************************************************************************
|
||||
*
|
||||
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/util/Attic/IslamicCalendar.java,v $
|
||||
* $Date: 2000/10/27 22:25:52 $
|
||||
* $Revision: 1.7 $
|
||||
* $Date: 2000/11/18 00:30:54 $
|
||||
* $Revision: 1.8 $
|
||||
*
|
||||
*****************************************************************************************
|
||||
*/
|
||||
@ -71,7 +71,7 @@ import com.ibm.util.CalendarAstronomer;
|
||||
* @see com.ibm.util.GregorianCalendar
|
||||
*
|
||||
* @author Laura Werner
|
||||
* @version 1.0
|
||||
* @author Alan Liu
|
||||
*/
|
||||
public class IslamicCalendar extends Calendar {
|
||||
|
||||
@ -118,13 +118,6 @@ public class IslamicCalendar extends Calendar {
|
||||
public static final int DHU_AL_HIJJAH = 11;
|
||||
|
||||
|
||||
// Useful millisecond constants
|
||||
private static final int SECOND_MS = 1000;
|
||||
private static final int MINUTE_MS = 60*SECOND_MS;
|
||||
private static final int HOUR_MS = 60*MINUTE_MS;
|
||||
private static final long DAY_MS = 24*HOUR_MS;
|
||||
private static final long WEEK_MS = 7*DAY_MS;
|
||||
|
||||
private static final long HIJRA_MILLIS = -42521587200000L; // 7/16/622 AD 00:00
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
@ -249,9 +242,10 @@ public class IslamicCalendar extends Calendar {
|
||||
if (civil != beCivil) {
|
||||
// The fields of the calendar will become invalid, because the calendar
|
||||
// rules are different
|
||||
computeTime();
|
||||
areFieldsSet = false;
|
||||
long m = getTimeInMillis();
|
||||
civil = beCivil;
|
||||
clear();
|
||||
setTimeInMillis(m);
|
||||
}
|
||||
}
|
||||
|
||||
@ -268,466 +262,35 @@ public class IslamicCalendar extends Calendar {
|
||||
// Minimum / Maximum access functions
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
// The minimum and maximum values for all of the fields, for validation
|
||||
private static final int MinMax[][] = {
|
||||
// Min Greatest Min Least Max Max
|
||||
{ 0, 0, 0, 0 }, // ERA
|
||||
{ 1, 1, 5000000, 5000000 }, // YEAR
|
||||
{ 0, 0, 11, 11 }, // MONTH
|
||||
{ 0, 0, 51, 52 }, // WEEK_OF_YEAR
|
||||
{ 0, 0, 5, 6 }, // WEEK_OF_MONTH
|
||||
{ 1, 1, 29, 30 }, // DAY_OF_MONTH
|
||||
{ 1, 1, 354, 355 }, // DAY_OF_YEAR
|
||||
{ 1, 1, 7, 7 }, // DAY_OF_WEEK
|
||||
{ -1, -1, 4, 5 }, // DAY_OF_WEEK_IN_MONTH
|
||||
{ 0, 0, 1, 1 }, // AM_PM
|
||||
{ 0, 0, 11, 11 }, // HOUR
|
||||
{ 0, 0, 23, 23 }, // HOUR_OF_DAY
|
||||
{ 0, 0, 59, 59 }, // MINUTE
|
||||
{ 0, 0, 59, 59 }, // SECOND
|
||||
{ 0, 0, 999, 999 }, // MILLISECOND
|
||||
{ -12*HOUR_MS, -12*HOUR_MS, 12*HOUR_MS, 12*HOUR_MS }, // ZONE_OFFSET
|
||||
{ 0, 0, 1*HOUR_MS, 1*HOUR_MS }, // DST_OFFSET
|
||||
private static final int LIMITS[][] = {
|
||||
// Minimum Greatest Least Maximum
|
||||
// Minimum Maximum
|
||||
{ 0, 0, 0, 0 }, // ERA
|
||||
{ 1, 1, 5000000, 5000000 }, // YEAR
|
||||
{ 0, 0, 11, 11 }, // MONTH
|
||||
{ 1, 1, 51, 52 }, // WEEK_OF_YEAR
|
||||
{ 0, 0, 5, 6 }, // WEEK_OF_MONTH
|
||||
{ 1, 1, 29, 30 }, // DAY_OF_MONTH
|
||||
{ 1, 1, 354, 355 }, // DAY_OF_YEAR
|
||||
{/* */}, // DAY_OF_WEEK
|
||||
{ -1, -1, 4, 5 }, // DAY_OF_WEEK_IN_MONTH
|
||||
{/* */}, // AM_PM
|
||||
{/* */}, // HOUR
|
||||
{/* */}, // HOUR_OF_DAY
|
||||
{/* */}, // MINUTE
|
||||
{/* */}, // SECOND
|
||||
{/* */}, // MILLISECOND
|
||||
{/* */}, // ZONE_OFFSET
|
||||
{/* */}, // DST_OFFSET
|
||||
{ -5000001, -5000001, 5000001, 5000001 }, // YEAR_WOY
|
||||
{/* */}, // DOW_LOCAL
|
||||
{ -5000000, -5000000, 5000000, 5000000 }, // EXTENDED_YEAR
|
||||
{/* */}, // JULIAN_DAY
|
||||
{/* */}, // MILLISECONDS_IN_DAY
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns minimum value for the given field.
|
||||
* For example, for {@link #DAY_OF_MONTH DAY_OF_MONTH} this method returns 1,
|
||||
*
|
||||
* @param field The field whose minimum value is desired.
|
||||
*
|
||||
* @see com.ibm.util.Calendar#getMinimum
|
||||
*/
|
||||
public int getMinimum(int field)
|
||||
{
|
||||
return MinMax[field][0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns highest minimum value for the given field. For the Islamic
|
||||
* calendar, this always returns the same result as {@link #getMinimum}.
|
||||
*
|
||||
* @param field The field whose greatest minimum value is desired.
|
||||
*
|
||||
* @see #getMinimum
|
||||
*/
|
||||
public int getGreatestMinimum(int field)
|
||||
{
|
||||
return MinMax[field][1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns maximum value for the given field
|
||||
* For the {@link #DAY_OF_MONTH DAY_OF_MONTH} field, this method returns 30.
|
||||
*
|
||||
* @param field The field whose maximum value is desired.
|
||||
*
|
||||
* @see #getLeastMaximum
|
||||
* @see #getActualMaximum
|
||||
*/
|
||||
public int getMaximum(int field)
|
||||
{
|
||||
return MinMax[field][3];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns lowest maximum value for the given field. For most fields,
|
||||
* this returns the same result as {@link #getMaximum getMaximum}. However,
|
||||
* for some fields this can be a lower number. For example,
|
||||
* the maximum {@link #DAY_OF_MONTH DAY_OF_MONTH} in the Islamic caleandar varies
|
||||
* from month to month, so this method returns 29 while <code>getMaximum</code>
|
||||
* returns 30.
|
||||
*
|
||||
* @param field The field whose least maximum value is desired.
|
||||
*
|
||||
* @see #getMaximum
|
||||
* @see #getActualMaximum
|
||||
*/
|
||||
public int getLeastMaximum(int field)
|
||||
{
|
||||
return MinMax[field][2];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the maximum value that a field could have, given the current date.
|
||||
* For example, for the {@link #DAY_OF_MONTH DAY_OF_MONTH} field the actual maximum varies
|
||||
* depending on the length of the month, which in turn varies according
|
||||
* to either the civil calendar cycle or the actual time of the next new moon.
|
||||
*
|
||||
* @param field The field whose maximum value is desired.
|
||||
*
|
||||
* @see #getMaximum
|
||||
* @see #getLeastMaximum
|
||||
*/
|
||||
public int getActualMaximum(int field) {
|
||||
if (!isSet(YEAR) || !isSet(MONTH)) {
|
||||
complete();
|
||||
}
|
||||
switch (field) {
|
||||
case DAY_OF_MONTH:
|
||||
return monthLength(fields[YEAR], fields[MONTH]);
|
||||
|
||||
case DAY_OF_YEAR:
|
||||
return yearLength(fields[YEAR]);
|
||||
|
||||
default:
|
||||
return super.getActualMaximum(field);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Functions for converting from field values to milliseconds....
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Converts time field values to UTC as milliseconds.
|
||||
*
|
||||
* @exception IllegalArgumentException if a field has an invalid value
|
||||
* and {@link #isLenient isLenient} returns <code>false</code>.
|
||||
*/
|
||||
protected void computeTime()
|
||||
{
|
||||
if (isTimeSet) return;
|
||||
|
||||
if (!isLenient() && !validateFields())
|
||||
throw new IllegalArgumentException();
|
||||
|
||||
if (isSet(ERA) && internalGet(ERA) != 0)
|
||||
throw new IllegalArgumentException();
|
||||
|
||||
// We need the time zone offset for some of the calculations below.
|
||||
// We use the TimeZone object, unless the user has explicitly set the
|
||||
// ZONE_OFFSET field.
|
||||
TimeZone zone = getTimeZone();
|
||||
int zoneOffset = zone.getRawOffset();
|
||||
|
||||
// The year is required. We don't have to check if it's unset,
|
||||
// because if it is, by definition it will be 0.
|
||||
|
||||
int year = internalGet(YEAR);
|
||||
|
||||
if (year <= 0 && !isLenient())
|
||||
throw new IllegalArgumentException();
|
||||
|
||||
long dayNumber = 0, date = 0;
|
||||
|
||||
// The following code is somewhat convoluted. The various nested
|
||||
// if's handle the different cases of what fields are present.
|
||||
if (isSet(MONTH) &&
|
||||
(isSet(DATE) ||
|
||||
(isSet(DAY_OF_WEEK) &&
|
||||
(isSet(WEEK_OF_MONTH) ||
|
||||
isSet(DAY_OF_WEEK_IN_MONTH))
|
||||
)
|
||||
))
|
||||
{
|
||||
// We have the month specified. Figure out when that month starts
|
||||
int month = internalGet(MONTH);
|
||||
dayNumber = monthStart(year, month);
|
||||
|
||||
if (isSet(DATE))
|
||||
{
|
||||
date = internalGet(DATE);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Compute from day of week plus week number or from the day of
|
||||
// week plus the day of week in month. The computations are
|
||||
// almost identical.
|
||||
|
||||
// Find the day of the week for the first of this month. This
|
||||
// is zero-based, with 0 being the locale-specific first day of
|
||||
// the week. Add 1 to get the 1st day of month. Subtract
|
||||
// getFirstDayOfWeek() to make 0-based.
|
||||
|
||||
int fdm = absoluteDayToDayOfWeek(dayNumber + 1) - getFirstDayOfWeek();
|
||||
if (fdm < 0) fdm += 7;
|
||||
|
||||
// Find the start of the first week. This will be a date from
|
||||
// 1..-6. It represents the locale-specific first day of the
|
||||
// week of the first day of the month, ignoring minimal days in
|
||||
// first week.
|
||||
date = 1 - fdm + internalGet(DAY_OF_WEEK) - getFirstDayOfWeek();
|
||||
|
||||
if (isSet(WEEK_OF_MONTH))
|
||||
{
|
||||
// Adjust for minimal days in first week.
|
||||
if ((7 - fdm) < getMinimalDaysInFirstWeek()) date += 7;
|
||||
|
||||
// Now adjust for the week number.
|
||||
date += 7 * (internalGet(WEEK_OF_MONTH) - 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Adjust into the month, if needed.
|
||||
if (date < 1) date += 7;
|
||||
|
||||
// We are basing this on the day-of-week-in-month. The only
|
||||
// trickiness occurs if the day-of-week-in-month is
|
||||
// negative.
|
||||
int dim = internalGet(DAY_OF_WEEK_IN_MONTH);
|
||||
if (dim >= 0) date += 7*(dim - 1);
|
||||
else
|
||||
{
|
||||
// Move date to the last of this day-of-week in this
|
||||
// month, then back up as needed. If dim==-1, we don't
|
||||
// back up at all. If dim==-2, we back up once, etc.
|
||||
// Don't back up past the first of the given day-of-week
|
||||
// in this month. Note that we handle -2, -3,
|
||||
// etc. correctly, even though values < -1 are
|
||||
// technically disallowed.
|
||||
date += ((monthLength(year, month) - date) / 7 + dim + 1) * 7;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (isSet(DAY_OF_YEAR)) {
|
||||
dayNumber = yearStart(year) + internalGet(DAY_OF_YEAR);
|
||||
}
|
||||
else if (isSet(DAY_OF_WEEK) && isSet(WEEK_OF_YEAR))
|
||||
{
|
||||
dayNumber = yearStart(year);
|
||||
|
||||
// Compute from day of week plus week of year
|
||||
|
||||
// Find the day of the week for the first of this year. This
|
||||
// is zero-based, with 0 being the locale-specific first day of
|
||||
// the week. Add 1 to get the 1st day of month. Subtract
|
||||
// getFirstDayOfWeek() to make 0-based.
|
||||
int fdy = absoluteDayToDayOfWeek(dayNumber + 1) - getFirstDayOfWeek();
|
||||
if (fdy < 0) fdy += 7;
|
||||
|
||||
// Find the start of the first week. This may be a valid date
|
||||
// from 1..7, or a date before the first, from 0..-6. It
|
||||
// represents the locale-specific first day of the week
|
||||
// of the first day of the year.
|
||||
|
||||
// First ignore the minimal days in first week.
|
||||
date = 1 - fdy + internalGet(DAY_OF_WEEK) - getFirstDayOfWeek();
|
||||
|
||||
// Adjust for minimal days in first week.
|
||||
if ((7 - fdy) < getMinimalDaysInFirstWeek()) date += 7;
|
||||
|
||||
// Now adjust for the week number.
|
||||
date += 7 * (internalGet(WEEK_OF_YEAR) - 1);
|
||||
|
||||
dayNumber += date;
|
||||
} else { // Not enough information
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
long millis = dayNumber * DAY_MS + HIJRA_MILLIS;
|
||||
|
||||
// Add in the days we calculated above
|
||||
millis += (date - 1) * DAY_MS;
|
||||
|
||||
// Now we can do the time portion of the conversion.
|
||||
|
||||
int millisInDay = 0;
|
||||
|
||||
// Hours
|
||||
if (isSet(HOUR_OF_DAY)) {
|
||||
// Don't normalize here; let overflow bump into the next period.
|
||||
// This is consistent with how we handle other fields.
|
||||
millisInDay += internalGet(HOUR_OF_DAY);
|
||||
|
||||
} else if (isSet(HOUR))
|
||||
{
|
||||
// Don't normalize here; let overflow bump into the next period.
|
||||
// This is consistent with how we handle other fields.
|
||||
millisInDay += internalGet(HOUR);
|
||||
|
||||
millisInDay += 12 * internalGet(AM_PM);
|
||||
}
|
||||
|
||||
// Minutes. We use the fact that unset == 0
|
||||
millisInDay *= 60;
|
||||
millisInDay += internalGet(MINUTE);
|
||||
|
||||
// Seconds. unset == 0
|
||||
millisInDay *= 60;
|
||||
millisInDay += internalGet(SECOND);
|
||||
|
||||
// Milliseconds. unset == 0
|
||||
millisInDay *= 1000;
|
||||
millisInDay += internalGet(MILLISECOND);
|
||||
|
||||
// Add millis and millisInDay together, to make millis contain the GMT time
|
||||
// computed so far, with no DST adjustments
|
||||
millis += millisInDay;
|
||||
|
||||
int dstOffset = 0;
|
||||
|
||||
//
|
||||
// Compute the time zone offset and DST offset.
|
||||
// Since the TimeZone API expects the Gregorian year, month, etc.,
|
||||
// We have to convert to local Gregorian time in order to
|
||||
// figure out the time zone calculations. This is a bit slow, but
|
||||
// it saves us from doing some *really* nasty calculations here.
|
||||
//
|
||||
if (getTimeZone().useDaylightTime())
|
||||
{
|
||||
synchronized(gregorian) {
|
||||
gregorian.setTimeZone(zone);
|
||||
gregorian.setTime(new Date(millis));
|
||||
dstOffset = gregorian.get(DST_OFFSET);
|
||||
}
|
||||
}
|
||||
|
||||
// Store our final computed GMT time, with DST adjustments.
|
||||
time = millis - zoneOffset - dstOffset;
|
||||
isTimeSet = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the values of the set time fields.
|
||||
*/
|
||||
private boolean validateFields()
|
||||
{
|
||||
for (int field = 0; field < FIELD_COUNT; field++)
|
||||
{
|
||||
// Ignore DATE and DAY_OF_YEAR which are handled below
|
||||
if (isSet(field) &&
|
||||
!boundsCheck(internalGet(field), field))
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isSet(YEAR))
|
||||
{
|
||||
int year = internalGet(YEAR);
|
||||
if (year < 1)
|
||||
return false;
|
||||
}
|
||||
|
||||
// Handle DAY_OF_WEEK_IN_MONTH, which must not have the value zero.
|
||||
// We've checked against minimum and maximum above already.
|
||||
if (isSet(DAY_OF_WEEK_IN_MONTH) &&
|
||||
0 == internalGet(DAY_OF_WEEK_IN_MONTH)) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the value of the given time field.
|
||||
*/
|
||||
private boolean boundsCheck(int value, int field)
|
||||
{
|
||||
return value >= getMinimum(field) && value <= getMaximum(field);
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Functions for converting from milliseconds to field values
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Converts UTC as milliseconds to time field values.
|
||||
* The time is <em>not</em>
|
||||
* recomputed first; to recompute the time, then the fields, call the
|
||||
* {@link #complete} method.
|
||||
*/
|
||||
protected void computeFields()
|
||||
{
|
||||
if (areFieldsSet) return;
|
||||
|
||||
// The following algorithm only works for dates after the Hijra (16 July AD 622)
|
||||
if (time < HIJRA_MILLIS && !isLenient()) {
|
||||
throw new IllegalArgumentException("IslamicCalendar does not handle dates before 1 AH");
|
||||
}
|
||||
|
||||
//
|
||||
// Compute the time zone offset and DST offset.
|
||||
// Since the TimeZone API expects the Gregorian year, month, etc.,
|
||||
// We have to convert to local Gregorian time in order to
|
||||
// figure out the time zone calculations. This is a bit slow, but
|
||||
// it saves us from doing some *really* nasty calculations here.
|
||||
//
|
||||
TimeZone zone = getTimeZone();
|
||||
int rawOffset = zone.getRawOffset(); // Not including DST
|
||||
int dstOffset = 0;
|
||||
if (zone.useDaylightTime())
|
||||
{
|
||||
synchronized (gregorian) {
|
||||
gregorian.setTimeZone(zone);
|
||||
gregorian.setTime(new Date(time));
|
||||
dstOffset += gregorian.get(DST_OFFSET);
|
||||
}
|
||||
}
|
||||
long localMillis = time + rawOffset + dstOffset;
|
||||
|
||||
long days = (localMillis - HIJRA_MILLIS) / DAY_MS;
|
||||
int millisInDay = (int)(localMillis % DAY_MS);
|
||||
|
||||
if (civil) {
|
||||
// Use the civil calendar approximation, which is just arithmetic
|
||||
int year = (int)Math.floor( (30 * days + 10646) / 10631.0 );
|
||||
int month = (int)Math.ceil((days - 29 - yearStart(year)) / 29.5 );
|
||||
month = Math.min(month, 11);
|
||||
|
||||
int date = (int)(days - monthStart(year, month)) + 1;
|
||||
|
||||
fields[YEAR] = year;
|
||||
fields[MONTH] = month;
|
||||
fields[DATE] = date;
|
||||
} else {
|
||||
// Guess at the number of elapsed full months since the epoch
|
||||
int months = (int)Math.floor(days / CalendarAstronomer.SYNODIC_MONTH);
|
||||
|
||||
long start = (long)Math.floor(months * CalendarAstronomer.SYNODIC_MONTH - 1);
|
||||
|
||||
if ( days - start >= 28 && MoonAge(time) > 0) {
|
||||
// If we're near the end of the month, assume next month and search backwards
|
||||
months++;
|
||||
}
|
||||
|
||||
// Find out the last time that the new moon was actually visible at this longitude
|
||||
// This returns midnight the night that the moon was visible at sunset.
|
||||
while ((start = trueMonthStart(months)) > days) {
|
||||
// If it was after the date in question, back up a month and try again
|
||||
months--;
|
||||
}
|
||||
|
||||
fields[YEAR] = months / 12 + 1;
|
||||
fields[MONTH] = months % 12;
|
||||
fields[DATE] = (int)(days - start) + 1;
|
||||
|
||||
}
|
||||
fields[ERA] = 0;
|
||||
|
||||
// Calculate the day of the week.
|
||||
int dayOfWeek = absoluteDayToDayOfWeek(days);
|
||||
fields[DAY_OF_WEEK] = dayOfWeek;
|
||||
fields[WEEK_OF_MONTH] = weekNumber(fields[DATE], dayOfWeek);
|
||||
fields[DAY_OF_WEEK_IN_MONTH] = (fields[DATE]-1) / 7 + 1;
|
||||
|
||||
// Now figure out the day of the year.
|
||||
int dayOfYear = (int)(days - monthStart(fields[YEAR], fields[MONTH]) + 1);
|
||||
|
||||
fields[DAY_OF_YEAR] = dayOfYear;
|
||||
fields[WEEK_OF_YEAR] = weekNumber(dayOfYear, dayOfWeek);
|
||||
|
||||
// Fill in all time-related fields based on millisInDay.
|
||||
|
||||
fields[MILLISECOND] = millisInDay % 1000;
|
||||
millisInDay /= 1000;
|
||||
fields[SECOND] = millisInDay % 60;
|
||||
millisInDay /= 60;
|
||||
fields[MINUTE] = millisInDay % 60;
|
||||
millisInDay /= 60;
|
||||
fields[HOUR_OF_DAY] = millisInDay;
|
||||
fields[AM_PM] = millisInDay / 12;
|
||||
fields[HOUR] = millisInDay % 12;
|
||||
|
||||
fields[ZONE_OFFSET] = rawOffset;
|
||||
fields[DST_OFFSET] = dstOffset;
|
||||
|
||||
areFieldsSet = true;
|
||||
|
||||
// Careful here: We are manually setting the isSet flags to true, so we
|
||||
// must be sure that the above code actually does set all these fields.
|
||||
_TEMPORARY_markAllFieldsSet();
|
||||
protected int handleGetLimit(int field, int limitType) {
|
||||
return LIMITS[field][limitType];
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
@ -771,19 +334,6 @@ public class IslamicCalendar extends Calendar {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of days in the given Islamic year
|
||||
*/
|
||||
private final int yearLength(int year)
|
||||
{
|
||||
if (civil) {
|
||||
return 354 + (civilLeapYear(year) ? 1 : 0);
|
||||
} else {
|
||||
int month = 12*(year-1);
|
||||
return (int)(trueMonthStart(month + 12) - trueMonthStart(month));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the day # on which the given month starts. Days are counted
|
||||
* from the Hijri epoch, origin 0.
|
||||
@ -800,28 +350,6 @@ public class IslamicCalendar extends Calendar {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the length (in days) of the given month.
|
||||
*
|
||||
* @param year The hijri year
|
||||
* @param year The hijri month, 0-based
|
||||
*/
|
||||
private final int monthLength(int year, int month)
|
||||
{
|
||||
int length = 0;
|
||||
|
||||
if (civil) {
|
||||
length = 29 + (month+1) % 2;
|
||||
if (month == DHU_AL_HIJJAH && civilLeapYear(year)) {
|
||||
length++;
|
||||
}
|
||||
} else {
|
||||
month = 12*(year-1) + month;
|
||||
length = (int)( trueMonthStart(month+1) - trueMonthStart(month) );
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the day number on which a particular month of the true/lunar
|
||||
* Islamic calendar starts.
|
||||
@ -838,26 +366,26 @@ public class IslamicCalendar extends Calendar {
|
||||
{
|
||||
// Make a guess at when the month started, using the average length
|
||||
long origin = HIJRA_MILLIS
|
||||
+ (long)Math.floor(month * CalendarAstronomer.SYNODIC_MONTH - 1) * DAY_MS;
|
||||
+ (long)Math.floor(month * CalendarAstronomer.SYNODIC_MONTH - 1) * ONE_DAY;
|
||||
|
||||
double age = MoonAge(origin);
|
||||
double age = moonAge(origin);
|
||||
|
||||
if (MoonAge(origin) >= 0) {
|
||||
if (moonAge(origin) >= 0) {
|
||||
// The month has already started
|
||||
do {
|
||||
origin -= DAY_MS;
|
||||
age = MoonAge(origin);
|
||||
origin -= ONE_DAY;
|
||||
age = moonAge(origin);
|
||||
} while (age >= 0);
|
||||
}
|
||||
else {
|
||||
// Preceding month has not ended yet.
|
||||
do {
|
||||
origin += DAY_MS;
|
||||
age = MoonAge(origin);
|
||||
origin += ONE_DAY;
|
||||
age = moonAge(origin);
|
||||
} while (age < 0);
|
||||
}
|
||||
|
||||
start = (origin - HIJRA_MILLIS) / DAY_MS + 1;
|
||||
start = (origin - HIJRA_MILLIS) / ONE_DAY + 1;
|
||||
|
||||
cache.put(month, start);
|
||||
}
|
||||
@ -873,7 +401,7 @@ public class IslamicCalendar extends Calendar {
|
||||
* @param time The time at which the moon's age is desired,
|
||||
* in millis since 1/1/1970.
|
||||
*/
|
||||
static final double MoonAge(long time)
|
||||
static final double moonAge(long time)
|
||||
{
|
||||
double age = 0;
|
||||
|
||||
@ -893,9 +421,6 @@ public class IslamicCalendar extends Calendar {
|
||||
//-------------------------------------------------------------------------
|
||||
// Internal data....
|
||||
//
|
||||
|
||||
// We need a GregorianCalendar object for doing time zone calculations
|
||||
private static GregorianCalendar gregorian = new GregorianCalendar();
|
||||
|
||||
// And an Astronomer object for the moon age calculations
|
||||
private static CalendarAstronomer astro = new CalendarAstronomer();
|
||||
@ -911,9 +436,125 @@ public class IslamicCalendar extends Calendar {
|
||||
*/
|
||||
private boolean civil = true;
|
||||
|
||||
static private void debug(String str) {
|
||||
if (true) {
|
||||
System.out.println(str);
|
||||
//----------------------------------------------------------------------
|
||||
// Calendar framework
|
||||
//----------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Return the length (in days) of the given month.
|
||||
*
|
||||
* @param year The hijri year
|
||||
* @param year The hijri month, 0-based
|
||||
*/
|
||||
protected int handleGetMonthLength(int extendedYear, int month) {
|
||||
|
||||
int length = 0;
|
||||
|
||||
if (civil) {
|
||||
length = 29 + (month+1) % 2;
|
||||
if (month == DHU_AL_HIJJAH && civilLeapYear(extendedYear)) {
|
||||
length++;
|
||||
}
|
||||
} else {
|
||||
month = 12*(extendedYear-1) + month;
|
||||
length = (int)( trueMonthStart(month+1) - trueMonthStart(month) );
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of days in the given Islamic year
|
||||
*/
|
||||
protected int handleGetYearLength(int extendedYear) {
|
||||
if (civil) {
|
||||
return 354 + (civilLeapYear(extendedYear) ? 1 : 0);
|
||||
} else {
|
||||
int month = 12*(extendedYear-1);
|
||||
return (int)(trueMonthStart(month + 12) - trueMonthStart(month));
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Functions for converting from field values to milliseconds....
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
// Return JD of start of given month/year
|
||||
protected int handleComputeMonthStart(int eyear, int month) {
|
||||
return (int) monthStart(eyear, month) + 1948439;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Functions for converting from milliseconds to field values
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
protected int handleGetExtendedYear() {
|
||||
int year;
|
||||
if (newerField(EXTENDED_YEAR, YEAR) == EXTENDED_YEAR) {
|
||||
year = internalGet(EXTENDED_YEAR, 1); // Default to year 1
|
||||
} else {
|
||||
year = internalGet(YEAR, 1); // Default to year 1
|
||||
}
|
||||
return year;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override Calendar to compute several fields specific to the Islamic
|
||||
* calendar system. These are:
|
||||
*
|
||||
* <ul><li>ERA
|
||||
* <li>YEAR
|
||||
* <li>MONTH
|
||||
* <li>DAY_OF_MONTH
|
||||
* <li>DAY_OF_YEAR
|
||||
* <li>EXTENDED_YEAR</ul>
|
||||
*
|
||||
* The DAY_OF_WEEK and DOW_LOCAL fields are already set when this
|
||||
* method is called. The getGregorianXxx() methods return Gregorian
|
||||
* calendar equivalents for the given Julian day.
|
||||
*/
|
||||
protected void handleComputeFields(int julianDay) {
|
||||
int year, month, dayOfMonth, dayOfYear;
|
||||
long monthStart;
|
||||
long days = julianDay - 1948440;
|
||||
|
||||
if (civil) {
|
||||
// Use the civil calendar approximation, which is just arithmetic
|
||||
year = (int)Math.floor( (30 * days + 10646) / 10631.0 );
|
||||
month = (int)Math.ceil((days - 29 - yearStart(year)) / 29.5 );
|
||||
month = Math.min(month, 11);
|
||||
monthStart = monthStart(year, month);
|
||||
} else {
|
||||
// Guess at the number of elapsed full months since the epoch
|
||||
int months = (int)Math.floor(days / CalendarAstronomer.SYNODIC_MONTH);
|
||||
|
||||
monthStart = (long)Math.floor(months * CalendarAstronomer.SYNODIC_MONTH - 1);
|
||||
|
||||
if ( days - monthStart >= 28 && moonAge(internalGetTimeInMillis()) > 0) {
|
||||
// If we're near the end of the month, assume next month and search backwards
|
||||
months++;
|
||||
}
|
||||
|
||||
// Find out the last time that the new moon was actually visible at this longitude
|
||||
// This returns midnight the night that the moon was visible at sunset.
|
||||
while ((monthStart = trueMonthStart(months)) > days) {
|
||||
// If it was after the date in question, back up a month and try again
|
||||
months--;
|
||||
}
|
||||
|
||||
year = months / 12 + 1;
|
||||
month = months % 12;
|
||||
}
|
||||
|
||||
dayOfMonth = (int)(days - monthStart(year, month)) + 1;
|
||||
|
||||
// Now figure out the day of the year.
|
||||
dayOfYear = (int)(days - monthStart(year, 0) + 1);
|
||||
|
||||
internalSet(ERA, 0);
|
||||
internalSet(YEAR, year);
|
||||
internalSet(EXTENDED_YEAR, year);
|
||||
internalSet(MONTH, month);
|
||||
internalSet(DAY_OF_MONTH, dayOfMonth);
|
||||
internalSet(DAY_OF_YEAR, dayOfYear);
|
||||
}
|
||||
}
|
||||
|
@ -5,8 +5,8 @@
|
||||
*******************************************************************************
|
||||
*
|
||||
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/util/Attic/JapaneseCalendar.java,v $
|
||||
* $Date: 2000/10/17 18:26:45 $
|
||||
* $Revision: 1.5 $
|
||||
* $Date: 2000/11/18 00:30:54 $
|
||||
* $Revision: 1.6 $
|
||||
*
|
||||
*****************************************************************************************
|
||||
*/
|
||||
@ -43,6 +43,7 @@ import java.util.Locale;
|
||||
* @see com.ibm.util.GregorianCalendar
|
||||
*
|
||||
* @author Laura Werner
|
||||
* @author Alan Liu
|
||||
*/
|
||||
public class JapaneseCalendar extends GregorianCalendar {
|
||||
|
||||
@ -97,8 +98,8 @@ public class JapaneseCalendar extends GregorianCalendar {
|
||||
* @param date The date to which the new calendar is set.
|
||||
*/
|
||||
public JapaneseCalendar(Date date) {
|
||||
super(TimeZone.getDefault(), Locale.getDefault());
|
||||
this.setTime(date);
|
||||
this();
|
||||
setTime(date);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -142,7 +143,7 @@ public class JapaneseCalendar extends GregorianCalendar {
|
||||
*/
|
||||
public JapaneseCalendar(int year, int month, int date) {
|
||||
super(year, month, date);
|
||||
set(ERA, eras.length - 1);
|
||||
set(ERA, CURRENT_ERA);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -168,149 +169,60 @@ public class JapaneseCalendar extends GregorianCalendar {
|
||||
int minute, int second)
|
||||
{
|
||||
super(year, month, date, hour, minute, second);
|
||||
set(ERA, eras.length - 1);
|
||||
set(ERA, CURRENT_ERA);
|
||||
}
|
||||
|
||||
/**
|
||||
* Override of the <code>GregorianCalendar</code> method that computes the
|
||||
* fields such as YEAR, MONTH, and DATE from the date in milliseconds
|
||||
* since 1/1/1970 AD.
|
||||
* <p>
|
||||
* This method calls {@link GregorianCalendar#computeFields} to do most
|
||||
* of the work, then determines the imperial era corresponding to the
|
||||
* Gregorian date and adjusts the {@link #YEAR YEAR} and {@link #ERA ERA}
|
||||
* fields accordingly.
|
||||
*/
|
||||
protected void computeFields() {
|
||||
// Let GregorianCalendar do its thing.
|
||||
super.computeFields();
|
||||
|
||||
fromGregorian();
|
||||
|
||||
//
|
||||
// If the date was too early for this calendar, throw an exception,
|
||||
// but only after we've made all the fields consistent
|
||||
//
|
||||
if (fields[YEAR] < 1 && !isLenient()) {
|
||||
throw new IllegalArgumentException("Year out of range");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Override of the <code>GregorianCalendar</code> method that computes the
|
||||
* elapsed time in milliseconds since 1/1/1970 AD from the fields such
|
||||
* as YEAR, MONTH, and DATE.
|
||||
* <p>
|
||||
* This method adjusts the {@link #YEAR YEAR} and {@link #ERA ERA} from their
|
||||
* values in the Japanese calendar to the corresponding Gregorian values, calls
|
||||
* {@link GregorianCalendar#computeTime} to do the real millisecond
|
||||
* calculation, and then restores the Japanese <code>YEAR</code> and
|
||||
* <code>ERA</code>.
|
||||
*/
|
||||
protected void computeTime() {
|
||||
int year = fields[YEAR];
|
||||
int era = fields[ERA];
|
||||
|
||||
// If we're in strict mode, make sure that the year actually
|
||||
// falls within the era, i.e. isn't < 1 or > era length
|
||||
if (era < 0 || era >= eras.length) {
|
||||
throw new IllegalArgumentException("Era out of range");
|
||||
}
|
||||
if (!isLenient() && (year < 1
|
||||
|| era < eras.length && year > (eras[(era+1)*3] - eras[era*3] + 1))) {
|
||||
throw new IllegalArgumentException("Year out of range");
|
||||
}
|
||||
|
||||
// Adjust the year and era to the corresponding Gregorian values
|
||||
toGregorian();
|
||||
try {
|
||||
// Let GregorianCalendar do its thing.
|
||||
super.computeTime();
|
||||
}
|
||||
finally {
|
||||
// Set the year and era back to the original values
|
||||
fields[YEAR] = year;
|
||||
fields[ERA] = era;
|
||||
}
|
||||
}
|
||||
|
||||
public void add(int field, int amount) {
|
||||
toGregorian();
|
||||
try {
|
||||
super.add(field, amount);
|
||||
}
|
||||
finally {
|
||||
fromGregorian();
|
||||
}
|
||||
}
|
||||
|
||||
public void roll(int field, int amount) {
|
||||
toGregorian();
|
||||
try {
|
||||
super.roll(field, amount);
|
||||
}
|
||||
finally {
|
||||
fromGregorian();
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Methods for converting between Gregorian and Buddhist calendars
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Convert the YEAR and ERA fields from Japanese to Gregorian values
|
||||
* Return the (Buddhist) value of the YEAR field on input;
|
||||
*/
|
||||
private void toGregorian() {
|
||||
int year = fields[YEAR] + eras[fields[ERA] * 3] - 1;
|
||||
int era = AD;
|
||||
|
||||
if (year < 1) {
|
||||
year = 1 - year;
|
||||
era = BC;
|
||||
protected int handleGetExtendedYear() {
|
||||
int year;
|
||||
// TODO reimplement this to be faster?
|
||||
if (newerField(EXTENDED_YEAR, YEAR) == EXTENDED_YEAR &&
|
||||
newerField(EXTENDED_YEAR, ERA) == EXTENDED_YEAR) {
|
||||
year = internalGet(EXTENDED_YEAR, 1);
|
||||
} else {
|
||||
// Subtract one because year starts at 1
|
||||
year = internalGet(YEAR) + ERAS[internalGet(ERA) * 3] - 1;
|
||||
}
|
||||
fields[ERA] = era;
|
||||
fields[YEAR] = year;
|
||||
return year;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adjust the year and era from Gregorian to Japanese values
|
||||
*/
|
||||
private void fromGregorian() {
|
||||
// Figure out which emperor's reign we're in, and adjust the
|
||||
// year and era accordingly.
|
||||
int year = fields[ERA] == AD ? fields[YEAR] : 1 - fields[YEAR];
|
||||
|
||||
// Binary search...
|
||||
int low = 0, high = eras.length / 3;
|
||||
protected void handleComputeFields(int julianDay) {
|
||||
super.handleComputeFields(julianDay);
|
||||
int year = internalGet(EXTENDED_YEAR);
|
||||
|
||||
// Binary search
|
||||
int low = 0, high = ERAS.length / 3;
|
||||
|
||||
while (low < high - 1) {
|
||||
int i = (low + high + 1) / 2;
|
||||
|
||||
int diff = year - eras[i*3 + 0];
|
||||
int i = (low + high) / 2;
|
||||
int diff = year - ERAS[i*3];
|
||||
|
||||
// If years are the same, then compare the months, and if those
|
||||
// are the same, compare days of month. In the ERAS array
|
||||
// months are 1-based for easier maintenance.
|
||||
if (diff == 0) {
|
||||
// Years were the same; compare the months. In the eras
|
||||
// array months are 1-based for easier maintenance.
|
||||
diff = fields[MONTH] - (eras[i*3 + 1] - 1);
|
||||
diff = internalGet(MONTH) - (ERAS[i*3 + 1] - 1);
|
||||
if (diff == 0) {
|
||||
diff = internalGet(DAY_OF_MONTH) - ERAS[i*3 + 2];
|
||||
}
|
||||
}
|
||||
if (diff == 0) {
|
||||
diff = fields[DATE] - eras[i*3 + 2];
|
||||
}
|
||||
if (diff > 0) {
|
||||
if (diff >= 0) {
|
||||
low = i;
|
||||
} else {
|
||||
high = i;
|
||||
}
|
||||
}
|
||||
|
||||
// Now we've found the last era that starts before this date,
|
||||
// so adjust the year to count from the start of that era.
|
||||
fields[ERA] = low;
|
||||
fields[YEAR] = year - eras[low*3 + 0] + 1;
|
||||
// Now we've found the last era that starts before this date, so
|
||||
// adjust the year to count from the start of that era. Note that
|
||||
// all dates before the first era will fall into the first era by
|
||||
// the algorithm.
|
||||
internalSet(ERA, low);
|
||||
internalSet(YEAR, year - ERAS[low*3] + 1);
|
||||
}
|
||||
|
||||
private static final int[] eras = {
|
||||
|
||||
private static final int[] ERAS = {
|
||||
// Gregorian date of each emperor's ascension
|
||||
// Years are AD, months are 1-based.
|
||||
// Year Month Day
|
||||
@ -556,15 +468,61 @@ public class JapaneseCalendar extends GregorianCalendar {
|
||||
// Public constants for some of the recent eras that folks might use...
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
// Constant for the current era. This must be regularly updated.
|
||||
static public final int CURRENT_ERA = (ERAS.length / 3) - 1;
|
||||
|
||||
/** Constant for the era starting on Sept. 8, 1868 AD */
|
||||
static public final int MEIJI = eras.length - 4;
|
||||
static public final int MEIJI = CURRENT_ERA - 3;
|
||||
|
||||
/** Constant for the era starting on July 30, 1912 AD */
|
||||
static public final int TAISHO = eras.length - 3;
|
||||
static public final int TAISHO = CURRENT_ERA - 2;
|
||||
|
||||
/** Constant for the era starting on Dec. 25, 1926 AD */
|
||||
static public final int SHOWA = eras.length - 2;
|
||||
static public final int SHOWA = CURRENT_ERA - 1;
|
||||
|
||||
/** Constant for the era starting on Jan. 7, 1989 AD */
|
||||
static public final int HEISEI = eras.length - 1;
|
||||
};
|
||||
static public final int HEISEI = CURRENT_ERA;
|
||||
|
||||
/**
|
||||
* Partial limits table for limits that differ from GregorianCalendar's.
|
||||
* The YEAR max limits are filled in the first time they are needed.
|
||||
*/
|
||||
private static int LIMITS[][] = {
|
||||
// Minimum Greatest Least Maximum
|
||||
// Minimum Maximum
|
||||
{ 0, 0, CURRENT_ERA, CURRENT_ERA }, // ERA
|
||||
{ 1, 1, 0, 0 }, // YEAR
|
||||
};
|
||||
|
||||
private static boolean YEAR_LIMIT_KNOWN = false;
|
||||
|
||||
/**
|
||||
* Override GregorianCalendar. We should really handle YEAR_WOY and
|
||||
* EXTENDED_YEAR here too to implement the 1..5000000 range, but it's
|
||||
* not critical.
|
||||
*/
|
||||
protected int handleGetLimit(int field, int limitType) {
|
||||
switch (field) {
|
||||
case ERA:
|
||||
return LIMITS[field][limitType];
|
||||
case YEAR:
|
||||
if (!YEAR_LIMIT_KNOWN) {
|
||||
int min = ERAS[3] - ERAS[0];
|
||||
int max = min;
|
||||
for (int i=6; i<ERAS.length; i+=3) {
|
||||
int d = ERAS[i] - ERAS[i-3];
|
||||
if (d < min) {
|
||||
min = d;
|
||||
} else if (d > max) {
|
||||
max = d;
|
||||
}
|
||||
}
|
||||
LIMITS[field][LEAST_MAXIMUM] = min;
|
||||
LIMITS[field][MAXIMUM] = max;
|
||||
}
|
||||
return LIMITS[field][limitType];
|
||||
default:
|
||||
return super.handleGetLimit(field, limitType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user