📄 bigdate.java
字号:
/**
* Get day number in the year for this BigDate.
*
* @return day number Jan 01 = 1, 1 to 366
*/
public final int getDDD()
{
return ( ordinal == NULL_ORDINAL )
? 0
: ( ordinal - jan01OfYear( yyyy ) + 1 );
}
/**
* Get java.util.Date object corresponding to this BigDate,
*
* @param timeZone We consider this BigDate to have an implied time of 0:00 in this timeZone.
* @return Date or null Result is undefined if BigDate is outside the range handled by Date.
* @see #getLocalTimeStamp
* @see #getUTCTimeStamp
* @see #getTimeStamp
* @see #getUTCDate
* @see #getLocalDate
*/
public final java.util.Date getDate( TimeZone timeZone )
{
return ordinal == NULL_ORDINAL
? null
: new java.util.Date( getTimeStamp( timeZone ) );
}
/**
* Get day of week for this BigDate. It is zero-based starting with Sunday.
*
* @return day of week 0=Sunday 1=Monday 2=Tuesday 3=Wednesday 4=Thursday 5=Friday 6=Saturday WARNING: not
* compatible with 1=Calendar.SUNDAY
* @see #getCalendarDayOfWeek
* @see #getISODayOfWeek
* @see #dayOfWeek
*/
public final int getDayOfWeek()
{
return dayOfWeek( ordinal );
}
/**
* Get day of week 1 to 7 for this BigDate according to the ISO standard IS-8601. It is one-based starting with
* Monday.
*
* @return day of week 1=Monday to 7=Sunday, 0 for null date. WARNING: not compatible with 1=Calendar.SUNDAY
* @see #getCalendarDayOfWeek
* @see #getDayOfWeek
* @see #isoDayOfWeek
*/
public final int getISODayOfWeek()
{
return isoDayOfWeek( ordinal );
}
/**
* Get week number 1 to 53 of the year this date falls in, according to the rules of ISO standard IS-8601. A week
* that lies partly in one year and partly in another is assigned a number in the year in which most of its days
* lie. This means that week 1 of any year is the week that contains 4 January, or equivalently week 1 of any year
* is the week that contains the first Thursday in January. Most years have 52 weeks, but years that start on a
* Thursday and leap years that start on a Wednesday have 53 weeks. Jan 1 may well be in week 53 of the previous
* year! Only defined for dates on or after 1600 Jan 01. You can find out how many ISO weeks there are per year with
* new BigDate( year, 12, 31).getISOWeekNumber();
*
* @return week number 1..53, 0 for null or invalid date.
* @see <a href="http://www.pip.dknet.dk/~pip10160/calendar.faq3.txt">Calendar FAQ</a>
*/
public final int getISOWeekNumber()
{
if ( ordinal < Jan_01_Leap100RuleYear )
{
return 0;
}
int jan04Ordinal = jan01OfYear( yyyy ) + 3;
int jan04DayOfWeek =
( jan04Ordinal + MondayIsZeroAdjustment - MIN_ORDINAL )
% 7;// 0=Monday
// 6=Sunday
int week1StartOrdinal = jan04Ordinal - jan04DayOfWeek;
if ( ordinal < week1StartOrdinal )
{// we are part of the previous year. Don't worry about year 0.
jan04Ordinal = jan01OfYear( yyyy - 1 ) + 3;
jan04DayOfWeek =
( jan04Ordinal + MondayIsZeroAdjustment - MIN_ORDINAL )
% 7;// 0=Monday
// 6=Sunday
week1StartOrdinal = jan04Ordinal - jan04DayOfWeek;
}
else if ( mm == 12 )
{// see if we are part of next year. Don't worry about year 0.
jan04Ordinal = jan01OfYear( yyyy + 1 ) + 3;
jan04DayOfWeek =
( jan04Ordinal + MondayIsZeroAdjustment - MIN_ORDINAL )
% 7;// 0=Monday
// 6=Sunday
int week1StartNextOrdinal = jan04Ordinal - jan04DayOfWeek;
if ( ordinal >= week1StartNextOrdinal )
{
week1StartOrdinal = week1StartNextOrdinal;
}
}
return ( ( ordinal - week1StartOrdinal ) / 7 ) + 1;
}// end getISOWeekNumber
/**
* Get java.util.Date object corresponding to this BigDate. We consider this BigDate to have an implied time of 0:00
* local time.
*
* @return Date or null Result is undefined if BigDate is outside the range handled by Date.
* @see #getLocalTimeStamp
*/
public final java.util.Date getLocalDate()
{
return ordinal == NULL_ORDINAL
? null
: new java.util.Date( getLocalTimeStamp() );
}
/**
* Get milliseconds since 1970 Jan 01 00:00 GMT for this BigDate. Does not account for leap seconds primarily
* because we do not know them in advance. N.B. returns long, not int as in many Unix implementations. This the long
* that a Sun Date constructor wants. We consider this BigDate to have an implied time of 0:00 local time.
*
* @return milliseconds since 1979 Jan 01 00:00 GMT, or NULL_TIMESTAMP. This is NOT a JDBC Timestamp! You can use
* this timestamp with java.util.Date.setTime, java.sql.TimeStamp.setTime or java.sql.Date.setTime or in the
* constructors. To interconvert, just cast.
* @see #getLocalDate
*/
public final long getLocalTimeStamp()
{
return getTimeStamp( TimeZone.getDefault() );
}
/**
* Get month of year for this BigDate.
*
* @return month 1 to 12, 0 for null date.
*/
public final int getMM()
{
return mm;
}
/**
* get days since 1970 Jan 01 for this BigDate. 1970/01/01 = day 0.
*
* @return days since 1970 Jan 01. This is NOT what you want for the java.util.Date constructor.
* @see #getLocalTimeStamp
* @see #getUTCTimeStamp
* @see #getTimeStamp
*/
public final int getOrdinal()
{
return ordinal;
}
/**
* @return Julian day number of noon of the date BigDate represents. See notes on the Julian Proleptic calendar
* under the constructor.
*/
public final double getProlepticJulianDay()
{
if ( ordinal == NULL_ORDINAL )
{
return Double.MIN_VALUE;
}
return ordinal + 2440588L/* diff in base epochs of calendars */
/* no need for noon adjust */;
}// getProlepticJulianDay
/**
* Get season of year for this BigDate.
*
* @return 0=spring (Mar, Apr, May) 1=summer (Jun, Jul, Aug) 2=fall (Sep, Oct, Dec) 3=winter Dec, Jan, Feb
*/
public final int getSeason()
{
// 1 > 3, 2 > 3, 3 > 0, 4 > 0, 5 > 0, 6 > 1, 7 > 1, 8 > 1, 9 > 2, 10 >
// 2, 11 > 2, 12 > 3
return ( ( mm + ( 12 - 3 ) ) % 12 ) / 3;
}
/**
* Get milliseconds since 1970 Jan 01 00:00 GMT for this BigDate, as at the start of day 0:00. Does not account for
* leap seconds primarily because we do not know them in advance. N.B. returns long, not int as in many Unix
* implementations. This the long that a Sun Date constructor wants.
*
* @param timeZone We consider this BigDate to have an implied time of 0:00 in this timeZone.
* @return milliseconds since 1979 Jan 01 00:00 GMT, or NULL_TIMESTAMP. This is NOT a JDBC Timestamp! You can use
* this timestamp with java.util.Date.setTime, java.sql.TimeStamp.setTime or java.sql.Date.setTime or in the
* constructors. To interconvert, just cast.
* @see #getDate
* @see #getUTCTimeStamp
* @see #getLocalTimeStamp
*/
public final long getTimeStamp( TimeZone timeZone )
{
if ( ordinal == NULL_ORDINAL )
{
return NULL_TIMESTAMP;
}
/*
* Gets the time zone offset, for current date, modified in case of
* daylight savings. This is the offset to add *to* UTC to get local
* time. Places east of Greenwich have a positive offset. :era the era
* of the given date, AD = 1 BC = 0 :year the year in the given date.
* Docs are unclear if 1900=0 or 1900. Does not use year in calcs
* anyway. :month the month in the given date. Month is 0-based. e.g., 0
* for January. :day the day-in-month of the given date. :dayOfWeek the
* day-of-week of the given date. 1=Calendar.SUNDAY. :milliseconds the
* millis in day in <em>standard</em> local time. :return the offset
* in millis to add *to* GMT to get local time.
*/
int offsetInMillis =
timeZone.getOffset( ordinal >= Jan_01_0001 ? 1 : 0,
yyyy,
mm - 1,
dd,
getCalendarDayOfWeek(),
0 );
// 86,400,000 = 1000 * 60 * 60 * 24 = milliseconds per day
return ordinal * 86400000L - offsetInMillis;
}
/**
* Get java.util.Date object corresponding to this BigDate. We consider this BigDate to have an implied time of 0:00
* UTC (Greenwich GMT).
*
* @return Date or null Result is undefined if BigDate is outside the range handled by Date.
* @see #getUTCTimeStamp
*/
public final java.util.Date getUTCDate()
{
return ordinal == NULL_ORDINAL
? null
: new java.util.Date( getUTCTimeStamp() );
}
/**
* Get milliseconds since 1970 Jan 01 00:00 GMT for this BigDate. Does not account for leap seconds primarily
* because we do not know them in advance. N.B. returns long, not int as in many Unix implementations. This the long
* that a Sun Date constructor wants. We consider this BigDate to have an implied time of 0:00 UTC (Greenwich GMT).
*
* @return milliseconds since 1970 Jan 01 00:00 GMT, or NULL_TIMESTAMP. This is NOT a JDBC Timestamp! You can use
* this timestamp with java.util.Date.setTime, java.sql.TimeStamp.setTime or java.sql.Date.setTime or in the
* constructors. To interconvert, just cast.
* @see #getUTCDate
*/
public final long getUTCTimeStamp()
{
// 86,400,000 = 1000 * 60 * 60 * 24 = milliseconds per day
return ordinal == NULL_ORDINAL
? NULL_TIMESTAMP
: ( ordinal * 86400000L );
}
/**
* Get week number 1 to 53 of the year this date falls in. This does NOT follow the rules of ISO standard IS-8601
* section 5.5. Week 1 is the first week with any days in the current year. Weeks start on Sunday. Jan 1 and Dec 31
* are always considered part of the current year. Only defined for dates on or after 1600 Jan 01.
*
* @return week number 1..53, 0 for null or invalid date.
* @see #getISOWeekNumber
*/
public final int getWeekNumber()
{
if ( ordinal < Jan_01_Leap100RuleYear )
{
return 0;
}
int jan01 = jan01OfYear( yyyy );
int jan01DayOfWeek = dayOfWeek( jan01 );// 0=Sunday 1=Monday
// 2=Tuesday 3=Wednesday
// 4=Thursday 5=Friday
// 6=Saturday
int sundayOnOrBeforeJan01Ordinal = jan01 - jan01DayOfWeek;
return ( ( ordinal - sundayOnOrBeforeJan01Ordinal ) / 7 ) + 1;
}// end getWeekNumber
/**
* Get year for this BigDate.
*
* @return year -999,999 to 999,999. 0 for null date. negative is BC, positive AD.
*/
public final int getYYYY()
{
return yyyy;
}
/**
* hashCode for use in Hashtable lookup
*
* @return the ordinal which is perfectly unique for the date.
*/
public final int hashCode()
{
return ordinal;
}
/**
* Find the BigDate with date closest to this one with the given day of week.
*
* @param dayOfWeek 0=Sunday 1=Monday 2=Tuesday 3=Wednesday 4=Thursday 5=Friday 6=Saturday WARNING: not compatible
* with 1=Calendar.SUNDAY.
* @return BigDate object with nearest date to this one with the given day of week.
*/
public BigDate nearestXXXDay( int dayOfWeek )
{
// positive or negative difference -6..0..+6
int diff = dayOfWeek - this.dayOfWeek();
if ( diff >= 4 )
{
diff -= 7;
}
else if ( diff <= -4 )
{
diff += 7;
}
return new BigDate( this.ordinal + diff );
}
/**
* Set the ordinal field, and compute the equivalent internal Gregorian yyyy mm dd fields. alias setOrdinal.
*
* @param ordinal days since 1970 Jan 1.
*/
public final void set( int ordinal )
{
if ( this.ordinal == ordinal )
{
return;
}
this.ordinal = ordinal;
toGregorian();
}// end set
/**
* Set the yyyy mm dd Gregorian fields, and compute the internal ordinal equivalent. yyyy mm dd are checked for
* validity.
*
* @param yyyy -999,999 (BC) to +999,999 (AD)
* @param mm month 1 to 12 (not 0 to 11 as in Sun's Date), no lead 0.
* @param dd day 1 to 31, no lead 0.
*/
public final void set( int yyyy, int mm, int dd )
{
set( yyyy, mm, dd, CHECK );
}
/**
* Set the Gregorian fields, and compute the ordinal equivalent with the same modifiers CHECK, NORMALIZE,
* BYPASSCHECK as the constructor. BEW
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -