下面列出了java.util.Calendar#DATE 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Returns the number of millis of a datefield, if this is a constant value
*
* @param unit A Calendar field which is a valid unit for a fragment
* @return number of millis
* @throws IllegalArgumentException if date can't be represented in millisenconds
* @since 2.4
*/
private static long getMillisPerUnit(int unit) {
long result = Long.MAX_VALUE;
switch (unit) {
case Calendar.DAY_OF_YEAR:
case Calendar.DATE:
result = MILLIS_PER_DAY;
break;
case Calendar.HOUR_OF_DAY:
result = MILLIS_PER_HOUR;
break;
case Calendar.MINUTE:
result = MILLIS_PER_MINUTE;
break;
case Calendar.SECOND:
result = MILLIS_PER_SECOND;
break;
case Calendar.MILLISECOND:
result = 1;
break;
default: throw new IllegalArgumentException("The unit " + unit + " cannot be represented is milleseconds");
}
return result;
}
int getDefault(int field, Calendar now){
switch (field){
case Calendar.MONTH:
return Calendar.JANUARY;
case Calendar.DAY_OF_WEEK:
return now.getFirstDayOfWeek();
case Calendar.DATE:
return 1;
case Calendar.HOUR_OF_DAY:
return 12;
case Calendar.HOUR:
case Calendar.MINUTE:
case Calendar.SECOND:
return 0;
default:
return 0;
}
}
/**
* Returns the number of millis of a datefield, if this is a constant value
*
* @param unit A Calendar field which is a valid unit for a fragment
* @return number of millis
* @throws IllegalArgumentException if date can't be represented in millisenconds
* @since 2.4
*/
private static long getMillisPerUnit(int unit) {
long result = Long.MAX_VALUE;
switch (unit) {
case Calendar.DAY_OF_YEAR:
case Calendar.DATE:
result = MILLIS_PER_DAY;
break;
case Calendar.HOUR_OF_DAY:
result = MILLIS_PER_HOUR;
break;
case Calendar.MINUTE:
result = MILLIS_PER_MINUTE;
break;
case Calendar.SECOND:
result = MILLIS_PER_SECOND;
break;
case Calendar.MILLISECOND:
result = 1;
break;
default: throw new IllegalArgumentException("The unit " + unit + " cannot be represented is milleseconds");
}
return result;
}
/**
* Returns the number of millis of a datefield, if this is a constant value
*
* @param unit A Calendar field which is a valid unit for a fragment
* @return number of millis
* @throws IllegalArgumentException if date can't be represented in millisenconds
* @since 2.4
*/
private static long getMillisPerUnit(int unit) {
long result = Long.MAX_VALUE;
switch (unit) {
case Calendar.DAY_OF_YEAR:
case Calendar.DATE:
result = MILLIS_PER_DAY;
break;
case Calendar.HOUR_OF_DAY:
result = MILLIS_PER_HOUR;
break;
case Calendar.MINUTE:
result = MILLIS_PER_MINUTE;
break;
case Calendar.SECOND:
result = MILLIS_PER_SECOND;
break;
case Calendar.MILLISECOND:
result = 1;
break;
default: throw new IllegalArgumentException("The unit " + unit + " cannot be represented is milleseconds");
}
return result;
}
/**
* Tests DateUtils.round()-method with Calendar.DATE
* Includes rounding the extremes of one day
* Includes rounding to January 1
*
* @throws Exception
* @since 3.0
*/
@Test
public void testRoundDate() throws Exception {
final int calendarField = Calendar.DATE;
Date roundedUpDate, roundedDownDate, lastRoundedDownDate;
Date minDate, maxDate;
roundedUpDate = dateTimeParser.parse("June 2, 2008 0:00:00.000");
roundedDownDate = targetDateDate;
lastRoundedDownDate = dateTimeParser.parse("June 1, 2008 11:59:59.999");
baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField);
//round to January 1
minDate = dateTimeParser.parse("December 31, 2007 12:00:00.000");
maxDate = dateTimeParser.parse("January 1, 2008 11:59:59.999");
roundToJanuaryFirst(minDate, maxDate, calendarField);
}
/**
* Tests DateUtils.round()-method with Calendar.DATE
* Includes rounding the extremes of one day
* Includes rounding to January 1
*
* @throws Exception
* @since 3.0
*/
@Test
public void testRoundDate() throws Exception {
final int calendarField = Calendar.DATE;
Date roundedUpDate, roundedDownDate, lastRoundedDownDate;
Date minDate, maxDate;
roundedUpDate = dateTimeParser.parse("June 2, 2008 0:00:00.000");
roundedDownDate = targetDateDate;
lastRoundedDownDate = dateTimeParser.parse("June 1, 2008 11:59:59.999");
baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField);
//round to January 1
minDate = dateTimeParser.parse("December 31, 2007 12:00:00.000");
maxDate = dateTimeParser.parse("January 1, 2008 11:59:59.999");
roundToJanuaryFirst(minDate, maxDate, calendarField);
}
private static Comparable<?> getAtomicIncreaseValue(String paramTokenStr, AtomIncreaseType type) {
String[] increase = TStringUtil.split(paramTokenStr.trim(), INCREASE_TYPE_SPLITOR);
// 如果长度为1,那么默认为数字/string类型
if (increase.length == 1) {
return Integer.valueOf(increase[0]);
} else if (increase.length == 2) {
switch (type) {
case NUMBER:
case STRING:
return Integer.valueOf(increase[0]);
case DATE:
return new DateEnumerationParameter(Integer.valueOf(increase[0]), Calendar.DATE);
case MONTH:
return new DateEnumerationParameter(Integer.valueOf(increase[0]), Calendar.MONTH);
case YEAR:
return new DateEnumerationParameter(Integer.valueOf(increase[0]), Calendar.YEAR);
case HOUR:
return new DateEnumerationParameter(Integer.valueOf(increase[0]), Calendar.HOUR_OF_DAY);
default:
throw new IllegalArgumentException("不支持的自增类型:" + type);
}
} else {
throw new IllegalArgumentException("自增配置定义错误:" + paramTokenStr);
}
}
/**
* Tests DateUtils.round()-method with Calendar.DATE
* Includes rounding the extremes of one day
* Includes rounding to January 1
*
* @throws Exception
* @since 3.0
*/
@Test
public void testRoundDate() throws Exception {
final int calendarField = Calendar.DATE;
Date roundedUpDate, roundedDownDate, lastRoundedDownDate;
Date minDate, maxDate;
roundedUpDate = dateTimeParser.parse("June 2, 2008 0:00:00.000");
roundedDownDate = targetDateDate;
lastRoundedDownDate = dateTimeParser.parse("June 1, 2008 11:59:59.999");
baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField);
//round to January 1
minDate = dateTimeParser.parse("December 31, 2007 12:00:00.000");
maxDate = dateTimeParser.parse("January 1, 2008 11:59:59.999");
roundToJanuaryFirst(minDate, maxDate, calendarField);
}
/**
* Returns the number of millis of a datefield, if this is a constant value
*
* @param unit A Calendar field which is a valid unit for a fragment
* @return number of millis
* @throws IllegalArgumentException if date can't be represented in millisenconds
* @since 2.4
*/
private static long getMillisPerUnit(int unit) {
long result = Long.MAX_VALUE;
switch (unit) {
case Calendar.DAY_OF_YEAR:
case Calendar.DATE:
result = MILLIS_PER_DAY;
break;
case Calendar.HOUR_OF_DAY:
result = MILLIS_PER_HOUR;
break;
case Calendar.MINUTE:
result = MILLIS_PER_MINUTE;
break;
case Calendar.SECOND:
result = MILLIS_PER_SECOND;
break;
case Calendar.MILLISECOND:
result = 1;
break;
default: throw new IllegalArgumentException("The unit " + unit + " cannot be represented is milleseconds");
}
return result;
}
/** This used to be 'deltim' in MMS.
*/
public static double deltaHours(int calUnit, int increments) {
if (calUnit == Calendar.DATE) {
return 24 * increments;
} else if (calUnit == Calendar.HOUR) {
return increments;
} else if (calUnit == Calendar.MINUTE) {
return increments / 60;
} else if (calUnit == Calendar.SECOND) {
return increments / 3600;
}
return -1;
}
/**
* Makes dates even, in the sense of that years always begin in January,
* months always begin on the 1st and days always at midnight.
*
* @param dates The list of dates.
* @return The new list of dates.
*/
private List<Date> makeDatesEven(List<Date> dates, Calendar calendar) {
// if the dates contain more dates than just the lower and upper bounds, make the dates in between even.
if (dates.size() > 2) {
List<Date> evenDates = new ArrayList<>();
// for each interval, modify the date slightly by a few millis, to make sure they are different days.
// this is because Axis stores each value and won't update the tick labels, if the value is already known.
// this happens if you display days and then add a date many years in the future the tick label will still be displayed as day.
for (int i = 0; i < dates.size(); i++) {
calendar.setTime(dates.get(i));
switch (actualInterval.interval) {
case Calendar.YEAR:
// if it's not the first or last date (lower and upper bound), make the year begin with first month and let the months begin with first day.
if (i != 0 && i != dates.size() - 1) {
calendar.set(Calendar.MONTH, 0);
calendar.set(Calendar.DATE, 1);
}
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 6);
break;
case Calendar.MONTH:
// if it's not the first or last date (lower and upper bound), make the months begin with first day.
if (i != 0 && i != dates.size() - 1) {
calendar.set(Calendar.DATE, 1);
}
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 5);
break;
case Calendar.WEEK_OF_YEAR:
// make weeks begin with first day of week?
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 4);
break;
case Calendar.DATE:
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 3);
break;
case Calendar.HOUR:
if (i != 0 && i != dates.size() - 1) {
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
}
calendar.set(Calendar.MILLISECOND, 2);
break;
case Calendar.MINUTE:
if (i != 0 && i != dates.size() - 1) {
calendar.set(Calendar.SECOND, 0);
}
calendar.set(Calendar.MILLISECOND, 1);
break;
case Calendar.SECOND:
calendar.set(Calendar.MILLISECOND, 0);
break;
default:
break;
}
evenDates.add(calendar.getTime());
}
return evenDates;
} else {
return dates;
}
}
/**
* Calendar-version for fragment-calculation in any unit
*
* @param calendar the calendar to work with, not null
* @param fragment the Calendar field part of calendar to calculate
* @param unit Calendar field defining the unit
* @return number of units within the fragment of the calendar
* @throws IllegalArgumentException if the date is <code>null</code> or
* fragment is not supported
* @since 2.4
*/
private static long getFragment(Calendar calendar, int fragment, int unit) {
if(calendar == null) {
throw new IllegalArgumentException("The date must not be null");
}
long millisPerUnit = getMillisPerUnit(unit);
long result = 0;
// Fragments bigger than a day require a breakdown to days
switch (fragment) {
case Calendar.YEAR:
result += (calendar.get(Calendar.DAY_OF_YEAR) * MILLIS_PER_DAY) / millisPerUnit;
break;
case Calendar.MONTH:
result += (calendar.get(Calendar.DAY_OF_MONTH) * MILLIS_PER_DAY) / millisPerUnit;
break;
}
switch (fragment) {
// Number of days already calculated for these cases
case Calendar.YEAR:
case Calendar.MONTH:
// The rest of the valid cases
case Calendar.DAY_OF_YEAR:
case Calendar.DATE:
result += (calendar.get(Calendar.HOUR_OF_DAY) * MILLIS_PER_HOUR) / millisPerUnit;
//$FALL-THROUGH$
case Calendar.HOUR_OF_DAY:
result += (calendar.get(Calendar.MINUTE) * MILLIS_PER_MINUTE) / millisPerUnit;
//$FALL-THROUGH$
case Calendar.MINUTE:
result += (calendar.get(Calendar.SECOND) * MILLIS_PER_SECOND) / millisPerUnit;
//$FALL-THROUGH$
case Calendar.SECOND:
result += (calendar.get(Calendar.MILLISECOND) * 1) / millisPerUnit;
break;
case Calendar.MILLISECOND: break;//never useful
default: throw new IllegalArgumentException("The fragment " + fragment + " is not supported");
}
return result;
}
/**
* Formats the given object as a DateFormat if Date is enabled or as a
* DecimalFormat. This is based on an internal format pattern given the
* object in parameter. When formatting a date, if minOrMaxDate is true as
* well as autoFormat, then the SimpleDateFormat us used to format the
* object.
*
* @param obj
* the object
* @param minOrMaxDate
* true if it is the min or max date on the scale.
* @return the formatted string
*/
public String format(Object obj, boolean minOrMaxDate) {
if (isDateEnabled()) {
if (autoFormat || formatPattern == null || formatPattern.equals("")
|| formatPattern.equals(default_decimal_format)
|| formatPattern.equals(DEFAULT_ENGINEERING_FORMAT)) {
double length = Math.abs(max - min);
if (length <= 5000 || timeUnit == Calendar.MILLISECOND) { // less
// than
// five
// second
internalSetFormatPattern("ss.SSS");//$NON-NLS-1$
} else if (length <= 1800000d || timeUnit == Calendar.SECOND) { // less
// than
// 30
// min
internalSetFormatPattern("HH:mm:ss");//$NON-NLS-1$
} else if (length <= 86400000d || timeUnit == Calendar.MINUTE) { // less
// than
// a
// day
internalSetFormatPattern("HH:mm");//$NON-NLS-1$
} else if (length <= 604800000d || timeUnit == Calendar.HOUR_OF_DAY) { // less
// than
// a
// week
internalSetFormatPattern("MM-dd\nHH:mm");//$NON-NLS-1$
} else if (length <= 2592000000d || timeUnit == Calendar.DATE) { // less
// than
// a
// month
internalSetFormatPattern("MM-dd");//$NON-NLS-1$
// } else if (length <= 31536000000d ||timeUnit ==
// Calendar.MONTH) { //less than a year
// formatPattern = "yyyy-MM-dd";//$NON-NLS-1$
} else { // more than a month
internalSetFormatPattern("yyyy-MM-dd"); //$NON-NLS-1$
}
autoFormat = true;
}
if (minOrMaxDate && autoFormat) {
if (Math.abs(max - min) < 5000)
return new SimpleDateFormat("yyyy-MM-dd\nHH:mm:ss.SSS").format(obj); //$NON-NLS-1$
return getFormat(DEFAULT_DATE_FORMAT, true).format(obj);
}
return getFormat(formatPattern, true).format(obj);
}
if (formatPattern == null || formatPattern.equals("")) {
formatPattern = default_decimal_format;
autoFormat = true;
}
return getFormat(formatPattern, false).format(obj);
}
/**
* Modifies the specified Calendar by "rounding" down to the specified unit
*
* @exception IllegalArgumentException if unit isn't recognized.
* @see #CALENDAR_UNITS
*/
public static void round(Calendar c, String unit) {
Integer uu = CALENDAR_UNITS.get(unit);
if (null == uu) {
throw new IllegalArgumentException("Rounding Unit not recognized: "
+ unit);
}
int u = uu.intValue();
switch (u) {
case Calendar.YEAR:
c.clear(Calendar.MONTH);
/* fall through */
case Calendar.MONTH:
c.clear(Calendar.DAY_OF_MONTH);
c.clear(Calendar.DAY_OF_WEEK);
c.clear(Calendar.DAY_OF_WEEK_IN_MONTH);
c.clear(Calendar.DAY_OF_YEAR);
c.clear(Calendar.WEEK_OF_MONTH);
c.clear(Calendar.WEEK_OF_YEAR);
/* fall through */
case Calendar.DATE:
c.clear(Calendar.HOUR_OF_DAY);
c.clear(Calendar.HOUR);
c.clear(Calendar.AM_PM);
/* fall through */
case Calendar.HOUR_OF_DAY:
c.clear(Calendar.MINUTE);
/* fall through */
case Calendar.MINUTE:
c.clear(Calendar.SECOND);
/* fall through */
case Calendar.SECOND:
c.clear(Calendar.MILLISECOND);
break;
default:
throw new IllegalStateException
("No logic for rounding value ("+u+") " + unit);
}
}
public static Footer Today() {
return new Footer(TODAY, Calendar.YEAR, Calendar.MONTH, Calendar.DATE);
}
/**
* Calendar-version for fragment-calculation in any unit
*
* @param calendar the calendar to work with, not null
* @param fragment the Calendar field part of calendar to calculate
* @param unit Calendar field defining the unit
* @return number of units within the fragment of the calendar
* @throws IllegalArgumentException if the date is <code>null</code> or
* fragment is not supported
* @since 2.4
*/
private static long getFragment(Calendar calendar, int fragment, int unit) {
if(calendar == null) {
throw new IllegalArgumentException("The date must not be null");
}
long millisPerUnit = getMillisPerUnit(unit);
long result = 0;
// Fragments bigger than a day require a breakdown to days
switch (fragment) {
case Calendar.YEAR:
result += (calendar.get(Calendar.DAY_OF_YEAR) * MILLIS_PER_DAY) / millisPerUnit;
break;
case Calendar.MONTH:
result += (calendar.get(Calendar.DAY_OF_MONTH) * MILLIS_PER_DAY) / millisPerUnit;
break;
}
switch (fragment) {
// Number of days already calculated for these cases
case Calendar.YEAR:
case Calendar.MONTH:
// The rest of the valid cases
case Calendar.DAY_OF_YEAR:
case Calendar.DATE:
result += (calendar.get(Calendar.HOUR_OF_DAY) * MILLIS_PER_HOUR) / millisPerUnit;
//$FALL-THROUGH$
case Calendar.HOUR_OF_DAY:
result += (calendar.get(Calendar.MINUTE) * MILLIS_PER_MINUTE) / millisPerUnit;
//$FALL-THROUGH$
case Calendar.MINUTE:
result += (calendar.get(Calendar.SECOND) * MILLIS_PER_SECOND) / millisPerUnit;
//$FALL-THROUGH$
case Calendar.SECOND:
result += (calendar.get(Calendar.MILLISECOND) * 1) / millisPerUnit;
break;
case Calendar.MILLISECOND: break;//never useful
default: throw new IllegalArgumentException("The fragment " + fragment + " is not supported");
}
return result;
}
/**
* Gets a Calendar fragment for any unit.
*
* @param calendar the calendar to work with, not null
* @param fragment the Calendar field part of calendar to calculate
* @param unit the {@code Calendar} field defining the unit
* @return number of units within the fragment of the calendar
* @throws IllegalArgumentException if the date is <code>null</code> or
* fragment is not supported
* @since 2.4
*/
private static long getFragment(Calendar calendar, int fragment, int unit) {
if(calendar == null) {
throw new IllegalArgumentException("The date must not be null");
}
long millisPerUnit = getMillisPerUnit(unit);
long result = 0;
// Fragments bigger than a day require a breakdown to days
switch (fragment) {
case Calendar.YEAR:
result += (calendar.get(Calendar.DAY_OF_YEAR) * MILLIS_PER_DAY) / millisPerUnit;
break;
case Calendar.MONTH:
result += (calendar.get(Calendar.DAY_OF_MONTH) * MILLIS_PER_DAY) / millisPerUnit;
break;
}
switch (fragment) {
// Number of days already calculated for these cases
case Calendar.YEAR:
case Calendar.MONTH:
// The rest of the valid cases
case Calendar.DAY_OF_YEAR:
case Calendar.DATE:
result += (calendar.get(Calendar.HOUR_OF_DAY) * MILLIS_PER_HOUR) / millisPerUnit;
//$FALL-THROUGH$
case Calendar.HOUR_OF_DAY:
result += (calendar.get(Calendar.MINUTE) * MILLIS_PER_MINUTE) / millisPerUnit;
//$FALL-THROUGH$
case Calendar.MINUTE:
result += (calendar.get(Calendar.SECOND) * MILLIS_PER_SECOND) / millisPerUnit;
//$FALL-THROUGH$
case Calendar.SECOND:
result += (calendar.get(Calendar.MILLISECOND) * 1) / millisPerUnit;
break;
case Calendar.MILLISECOND: break;//never useful
default: throw new IllegalArgumentException("The fragment " + fragment + " is not supported");
}
return result;
}
/**
* Rolls the date, month or year forward by the amount passed in. It modifies both the Gregorian and Jewish dates accordingly.
* If manipulation beyond the fields supported here is required, use the {@link Calendar} class {@link Calendar#add(int, int)}
* or {@link Calendar#roll(int, int)} methods in the following manner.
*
* <pre>
* <code>
* Calendar cal = jewishDate.getTime(); // get a java.util.Calendar representation of the JewishDate
* cal.add(Calendar.MONTH, 3); // add 3 Gregorian months
* jewishDate.setDate(cal); // set the updated calendar back to this class
* </code>
* </pre>
*
* @param field the calendar field to be forwarded. The must be {@link Calendar#DATE}, {@link Calendar#MONTH} or {@link Calendar#YEAR}
* @param amount the positive amount to move forward
* @throws IllegalArgumentException if the field is anything besides {@link Calendar#DATE}, {@link Calendar#MONTH} or {@link Calendar#YEAR}
* or if the amount is less than 1
*
* @see #back()
* @see Calendar#add(int, int)
* @see Calendar#roll(int, int)
*/
public void forward(int field, int amount) {
if (field != Calendar.DATE && field != Calendar.MONTH && field != Calendar.YEAR) {
throw new IllegalArgumentException("Unsupported field was passed to Forward. Only Calendar.DATE, Calendar.MONTH or Calendar.YEAR are supported.");
}
if (amount < 1) {
throw new IllegalArgumentException("JewishDate.forward() does not support amounts less than 1. See JewishDate.back()");
}
if (field == Calendar.DATE) {
// Change Gregorian date
for (int i = 0; i < amount; i++) {
if (gregorianDayOfMonth == getLastDayOfGregorianMonth(gregorianMonth, gregorianYear)) {
// if last day of year
if (gregorianMonth == 12) {
gregorianYear++;
gregorianMonth = 1;
gregorianDayOfMonth = 1;
} else {
gregorianMonth++;
gregorianDayOfMonth = 1;
}
} else { // if not last day of month
gregorianDayOfMonth++;
}
// Change the Jewish Date
if (jewishDay == getDaysInJewishMonth()) {
// if it last day of elul (i.e. last day of Jewish year)
if (jewishMonth == ELUL) {
jewishYear++;
jewishMonth++;
jewishDay = 1;
} else if (jewishMonth == getLastMonthOfJewishYear(jewishYear)) {
// if it is the last day of Adar, or Adar II as case may be
jewishMonth = NISSAN;
jewishDay = 1;
} else {
jewishMonth++;
jewishDay = 1;
}
} else { // if not last date of month
jewishDay++;
}
if (dayOfWeek == 7) { // if last day of week, loop back to Sunday
dayOfWeek = 1;
} else {
dayOfWeek++;
}
gregorianAbsDate++; // increment the absolute date
}
} else if (field == Calendar.MONTH) {
forwardJewishMonth(amount);
} else if (field == Calendar.YEAR) {
setJewishYear(getJewishYear() + amount);
}
}
/**
* Test DateUtils.truncate()-method with Calendar.DATE
*
* @throws Exception
* @since 3.0
*/
public void testTruncateDate() throws Exception {
final int calendarField = Calendar.DATE;
Date lastTruncateDate = dateTimeParser.parse("June 1, 2008 23:59:59.999");
baseTruncateTest(targetDateDate, lastTruncateDate, calendarField);
}
/**
* 默认使用Date作为日期类型的基本自增单位
*
* @param atomicIncreateNumber
*/
public DateEnumerationParameter(int atomicIncreateNumber){
this.atomicIncreatementNumber = atomicIncreateNumber;
this.calendarFieldType = Calendar.DATE;
}