下面列出了java.util.Calendar#HOUR_OF_DAY 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Tests DateUtils.round()-method with Calendar.HOUR_OF_DAY
* Includes rounding the extremes of one hour
* Includes rounding to January 1
*
* @throws Exception
* @since 3.0
*/
@Test
public void testRoundHourOfDay() throws Exception {
final int calendarField = Calendar.HOUR_OF_DAY;
Date roundedUpDate, roundedDownDate, lastRoundedDownDate;
Date minDate, maxDate;
roundedUpDate = dateTimeParser.parse("June 1, 2008 9:00:00.000");
roundedDownDate = targetHourOfDayDate;
lastRoundedDownDate = dateTimeParser.parse("June 1, 2008 8:29:59.999");
baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField);
//round to January 1
minDate = dateTimeParser.parse("December 31, 2007 23:30:00.000");
maxDate = dateTimeParser.parse("January 1, 2008 0:29:59.999");
roundToJanuaryFirst(minDate, maxDate, calendarField);
}
private static Comparable<?> getAtomicIncreaseValue(String paramTokenStr, AtomIncreaseType type) {
String[] increase = TStringUtil.split(paramTokenStr.trim(), INCREASE_TYPE_SPLITOR);
// 如果长度为1,那么默认为数字类型
if (increase.length == 1) {
return Integer.valueOf(increase[0]);
} else if (increase.length == 2) {
switch (type) {
case NUMBER:
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.HOUR_OF_DAY
* Includes rounding the extremes of one hour
* Includes rounding to January 1
*
* @throws Exception
* @since 3.0
*/
@Test
public void testRoundHourOfDay() throws Exception {
final int calendarField = Calendar.HOUR_OF_DAY;
Date roundedUpDate, roundedDownDate, lastRoundedDownDate;
Date minDate, maxDate;
roundedUpDate = dateTimeParser.parse("June 1, 2008 9:00:00.000");
roundedDownDate = targetHourOfDayDate;
lastRoundedDownDate = dateTimeParser.parse("June 1, 2008 8:29:59.999");
baseRoundTest(roundedUpDate, roundedDownDate, lastRoundedDownDate, calendarField);
//round to January 1
minDate = dateTimeParser.parse("December 31, 2007 23:30:00.000");
maxDate = dateTimeParser.parse("January 1, 2008 0:29: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;
}
/**
* 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;
}
private void handleMouseUp() {
dialPanel.getComposite()
.setCursor(getDisplay().getSystemCursor(SWT.CURSOR_ARROW));
if (timeAmPm != null) {
timeAmPm.setEnabled(true);
}
if (setH) {
int field = is24Hour ? Calendar.HOUR_OF_DAY : Calendar.HOUR;
cdt.fireSelectionChanged(field);
} else if (setM) {
cdt.fireSelectionChanged(Calendar.MINUTE);
} else if (setS) {
cdt.fireSelectionChanged(Calendar.SECOND);
}
setH = setM = setS = false;
}
private static int translate(final IntervalUnit unit) {
switch (unit) {
case DAY:
return Calendar.DAY_OF_YEAR;
case HOUR:
return Calendar.HOUR_OF_DAY;
case MINUTE:
return Calendar.MINUTE;
case MONTH:
return Calendar.MONTH;
case SECOND:
return Calendar.SECOND;
case MILLISECOND:
return Calendar.MILLISECOND;
case WEEK:
return Calendar.WEEK_OF_YEAR;
case YEAR:
return Calendar.YEAR;
default:
throw new IllegalArgumentException("Unknown IntervalUnit");
}
}
@Override
public void generateKLine(int range, int field, long time) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(time);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long endTick = calendar.getTimeInMillis();
String endTime = df.format(calendar.getTime());
//往前推range个时间单位
calendar.add(field, -range);
String fromTime = df.format(calendar.getTime());
long startTick = calendar.getTimeInMillis();
System.out.println("time range from " + fromTime + " to " + endTime);
List<ExchangeTrade> exchangeTrades = service.findTradeByTimeRange(this.symbol, startTick, endTick);
KLine kLine = new KLine();
kLine.setTime(endTick);
String rangeUnit = "";
if (field == Calendar.MINUTE) {
rangeUnit = "min";
} else if (field == Calendar.HOUR_OF_DAY) {
rangeUnit = "hour";
} else if (field == Calendar.DAY_OF_WEEK) {
rangeUnit = "week";
} else if (field == Calendar.DAY_OF_YEAR) {
rangeUnit = "day";
} else if (field == Calendar.MONTH) {
rangeUnit = "month";
}
kLine.setPeriod(range + rangeUnit);
// 处理K线信息
for (ExchangeTrade exchangeTrade : exchangeTrades) {
processTrade(kLine, exchangeTrade);
}
service.saveKLine(symbol, kLine);
}
protected DateTime toDateTime(BaseDateTimeType value, Integer calendarConstant) {
Calendar calendar = this.getCalendar(value);
TimeZone tz = calendar.getTimeZone() == null ? TimeZone.getDefault() : calendar.getTimeZone();
ZoneOffset zoneOffset = tz.toZoneId().getRules().getStandardOffset(calendar.toInstant());
switch (calendarConstant) {
case Calendar.YEAR: return new DateTime(
TemporalHelper.zoneToOffset(zoneOffset),
calendar.get(Calendar.YEAR)
);
case Calendar.MONTH: return new DateTime(
TemporalHelper.zoneToOffset(zoneOffset),
calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1
);
case Calendar.DAY_OF_MONTH: return new DateTime(
TemporalHelper.zoneToOffset(zoneOffset),
calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH)
);
case Calendar.HOUR_OF_DAY: return new DateTime(
TemporalHelper.zoneToOffset(zoneOffset),
calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY)
);
case Calendar.MINUTE: return new DateTime(
TemporalHelper.zoneToOffset(zoneOffset),
calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE)
);
case Calendar.SECOND: return new DateTime(
TemporalHelper.zoneToOffset(zoneOffset),
calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND)
);
case Calendar.MILLISECOND: return new DateTime(
TemporalHelper.zoneToOffset(zoneOffset),
calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND), calendar.get(Calendar.MILLISECOND)
);
default: throw new InvalidPrecision(String.format("Invalid temporal precision %s", calendarConstant));
}
}
/**
* Operation is based on java util calendar in order to support daylight
* saving times.
*/
public TimeSlice previous() {
long newStart = start;
long newEnd = end;
Calendar temp = Calendar.getInstance();
temp.setTimeInMillis(start);
int field = 0;
switch (period) {
case HOUR:
field = Calendar.HOUR_OF_DAY;
break;
case DAY:
field = Calendar.DAY_OF_MONTH;
break;
case WEEK:
field = Calendar.WEEK_OF_YEAR;
break;
case MONTH:
field = Calendar.MONTH;
break;
default:
break;
}
temp.add(field, -1);
newStart = temp.getTimeInMillis();
temp.setTimeInMillis(end);
temp.add(field, -1);
newEnd = temp.getTimeInMillis();
return new TimeSlice(newStart, newEnd, period);
}
public DateSubField(CubeSearcher searcher, SubField subfield) {
super(searcher);
m_subfield = subfield;
m_calendar = new GregorianCalendar(Utils.UTC_TIMEZONE);
switch(subfield) {
case MINUTE: m_calendarField = Calendar.MINUTE; break;
case HOUR: m_calendarField = Calendar.HOUR_OF_DAY; break;
case DAY: m_calendarField = Calendar.DAY_OF_MONTH; break;
case MONTH: m_calendarField = Calendar.MONTH; break;
case YEAR: m_calendarField = Calendar.YEAR; break;
default: Utils.require(false, "Undefined subfield: " + subfield);
}
}
public Integer get(final int field) {
if (field == Calendar.YEAR) {
if (!unspecifiedFields.contains(Calendar.YEAR)) {
return c.get(field);
}
return UNSPECIFIED_VALUE;
} else if (field == Calendar.MONTH) {
if (!unspecifiedFields.contains(Calendar.MONTH)) {
return c.get(field);
}
return UNSPECIFIED_VALUE;
} else if (field >= Calendar.WEEK_OF_YEAR && field <= Calendar.DAY_OF_WEEK_IN_MONTH) {
if (!unspecifiedFields.contains(Calendar.DATE)) {
return c.get(field);
}
return UNSPECIFIED_VALUE;
} else if (field >= Calendar.HOUR && field <= Calendar.HOUR_OF_DAY) {
if (!unspecifiedFields.contains(Calendar.HOUR_OF_DAY)) {
return c.get(field);
}
return UNSPECIFIED_VALUE;
} else if (field == Calendar.MINUTE) {
if (!unspecifiedFields.contains(Calendar.MINUTE)) {
return c.get(Calendar.MINUTE);
}
return UNSPECIFIED_VALUE;
} else {
return c.get(field);
}
//return UNSPECIFIED_VALUE;
}
private int slowSubCells(UnitNRShape lv) {
int field = FIELD_BY_LEVEL[lv.getLevel()+1];
//short-circuit optimization (GregorianCalendar assumptions)
if (field == -1 || field == Calendar.YEAR || field >= Calendar.HOUR_OF_DAY)//TODO make configurable
return super.getNumSubCells(lv);
Calendar cal = toCalendar(lv);//somewhat heavyweight op; ideally should be stored on UnitNRShape somehow
return cal.getActualMaximum(field) - cal.getActualMinimum(field) + 1;
}
@Override
public String mutate(String currentValue, boolean nullable) {
// If null, generate a new value that is not null, if not null generate null with probability
if(currentValue == null)
return generateRandom(false);
else if(nullable && random.nextDouble() < EvoSQLConfiguration.MUTATE_NULL_PROBABIBLITY)
return null;
Calendar cal = Calendar.getInstance();
try {
cal.setTimeInMillis(format.parse(currentValue).getTime());
} catch (ParseException e) {
throw new RuntimeException(e);
}
for (int i : genetic.Instrumenter.TIME_CONSTANTS) {
if (random.nextDouble() <= probability) {
int val;
switch (i) {
case Calendar.HOUR_OF_DAY: val = cal.get(Calendar.HOUR_OF_DAY); break;
case Calendar.MINUTE: val = cal.get(Calendar.MINUTE); break;
case Calendar.SECOND: val = cal.get(Calendar.SECOND); break;
case Calendar.MILLISECOND: val = cal.get(Calendar.MILLISECOND); break;
default: throw new RuntimeException("Impossible value " + i);
}
val = (int)(val + (random.nextSignedDouble() * 10));
switch (i) {
case Calendar.HOUR_OF_DAY: cal.set(Calendar.HOUR_OF_DAY, val); break;
case Calendar.MINUTE: cal.set(Calendar.MINUTE, val); break;
case Calendar.SECOND: cal.set(Calendar.SECOND, val); break;
case Calendar.MILLISECOND: cal.set(Calendar.MILLISECOND, val); break;
}
}
}
return format.format(cal.getTime());
}
@Override
public String mutate(String currentValue, boolean nullable) {
// If null, generate a new value that is not null, if not null generate null with probability
if(currentValue == null)
return generateRandom(false);
else if(nullable && random.nextDouble() < EvoSQLConfiguration.MUTATE_NULL_PROBABIBLITY)
return null;
Calendar cal = Calendar.getInstance();
try {
cal.setTimeInMillis(format.parse(currentValue).getTime());
} catch (ParseException e) {
throw new RuntimeException(e);
}
for (int i : genetic.Instrumenter.DATETIME_CONSTANTS) {
if (random.nextDouble() <= probability) {
int val;
switch (i) {
case Calendar.YEAR: val = cal.get(Calendar.YEAR); break;
case Calendar.MONTH: val = cal.get(Calendar.MONTH); break;
case Calendar.DAY_OF_MONTH: val = cal.get(Calendar.DAY_OF_MONTH); break;
case Calendar.HOUR_OF_DAY: val = cal.get(Calendar.HOUR_OF_DAY); break;
case Calendar.MINUTE: val = cal.get(Calendar.MINUTE); break;
case Calendar.SECOND: val = cal.get(Calendar.SECOND); break;
case Calendar.MILLISECOND: val = cal.get(Calendar.MILLISECOND); break;
default: throw new RuntimeException("Impossible value " + i);
}
val = (int)(val + (random.nextSignedDouble() * 10));
switch (i) {
case Calendar.YEAR: cal.set(Calendar.YEAR, val); break;
case Calendar.MONTH: cal.set(Calendar.MONTH, val); break;
case Calendar.DAY_OF_MONTH: cal.set(Calendar.DAY_OF_MONTH, val); break;
case Calendar.HOUR_OF_DAY: cal.set(Calendar.HOUR_OF_DAY, val); break;
case Calendar.MINUTE: cal.set(Calendar.MINUTE, val); break;
case Calendar.SECOND: cal.set(Calendar.SECOND, val); break;
case Calendar.MILLISECOND: cal.set(Calendar.MILLISECOND, val); break;
}
}
}
return format.format(cal.getTime());
}
/**
* Gets the grid step.
*
* @param lengthInPixels
* scale length in pixels
* @param min
* minimum value
* @param max
* maximum value
* @return rounded value.
*/
private double getGridStep(int lengthInPixels, final double minR, final double maxR) {
if((int) scale.getMajorGridStep() != 0) {
return scale.getMajorGridStep();
}
double min = minR, max = maxR;
if (lengthInPixels <= 0) {
lengthInPixels = 1;
}
boolean minBigger = false;
if (min >= max) {
if(max == min)
max ++;
else{
minBigger = true;
double swap = min;
min = max;
max= swap;
}
// throw new IllegalArgumentException("min must be less than max.");
}
double length = Math.abs(max - min);
double markStepHint = scale.getMajorTickMarkStepHint();
if(markStepHint > lengthInPixels)
markStepHint = lengthInPixels;
double gridStepHint = length / lengthInPixels
* markStepHint;
if(scale.isDateEnabled()) {
//by default, make the least step to be minutes
long timeStep = 60000l;
if (scale.getTimeUnit() == Calendar.SECOND) {
timeStep = 1000l;
} else if (scale.getTimeUnit() == Calendar.MINUTE) {
timeStep = 60000l;
}else if (scale.getTimeUnit() == Calendar.HOUR_OF_DAY) {
timeStep = 3600000l;
}else if (scale.getTimeUnit() == Calendar.DATE) {
timeStep = 86400000l;
}else if (scale.getTimeUnit() == Calendar.MONTH) {
timeStep = 30l*86400000l;
}else if (scale.getTimeUnit() == Calendar.YEAR) {
timeStep = 365l*86400000l;
}
double temp = gridStepHint + (timeStep - gridStepHint%timeStep);
return temp;
}
double mantissa = gridStepHint;
int exp = 0;
if (mantissa < 1) {
if(mantissa != 0){
while (mantissa < 1) {
mantissa *= 10.0;
exp--;
}
}
} else {
while (mantissa >= 10) {
mantissa /= 10.0;
exp++;
}
}
double gridStep;
if (mantissa > 7.5) {
// 10*10^exp
gridStep = 10 * Math.pow(10, exp);
} else if (mantissa > 3.5) {
// 5*10^exp
gridStep = 5 * Math.pow(10, exp);
} else if (mantissa > 1.5) {
// 2.0*10^exp
gridStep = 2 * Math.pow(10, exp);
} else {
gridStep = Math.pow(10, exp); // 1*10^exponent
}
if (minBigger)
gridStep = -gridStep;
return gridStep;
}
/**
* 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;
}
public static Body Time() {
return new Body(TIME, Calendar.HOUR, Calendar.HOUR_OF_DAY,
Calendar.MINUTE);
}
public int[] getFields() {
return new int[] { Calendar.HOUR_OF_DAY, Calendar.HOUR, Calendar.MINUTE,
Calendar.SECOND, Calendar.AM_PM };
}
/**
* Test DateUtils.truncate()-method with Calendar.HOUR_OF_DAY
*
* @throws Exception
* @since 3.0
*/
public void testTruncateHourOfDay() throws Exception {
final int calendarField = Calendar.HOUR_OF_DAY;
Date lastTruncateDate = dateTimeParser.parse("June 1, 2008 8:59:59.999");
baseTruncateTest(targetHourOfDayDate, lastTruncateDate, calendarField);
}