本文整理匯總了Java中java.util.Calendar.HOUR屬性的典型用法代碼示例。如果您正苦於以下問題:Java Calendar.HOUR屬性的具體用法?Java Calendar.HOUR怎麽用?Java Calendar.HOUR使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類java.util.Calendar
的用法示例。
在下文中一共展示了Calendar.HOUR屬性的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: parseTime
/**
* Parse a raw phrase into a date {@link Date}
*
* @param str Raw phrase
*
* @return Date object
*/
public static Date parseTime(String str)
{
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
for (String t : str.split("\\+"))
{
String[] end = t.split(":");
int type;
if (end[1].equalsIgnoreCase("d"))
type = Calendar.DAY_OF_YEAR;
else if (end[1].equalsIgnoreCase("h"))
type = Calendar.HOUR;
else if (end[1].equalsIgnoreCase("m"))
type = Calendar.MINUTE;
else
type = Calendar.SECOND;
cal.add(type, Integer.parseInt(end[0]));
}
return cal.getTime();
}
示例2: encode
public static String encode(String subject, ArrayList<String> roles) {
// prepare expiration date according to application properties
Date expDate = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(expDate);
int unit;
switch (applicationProperties.getTokenExpiration().getUnit()) {
case "SECOND":
unit = Calendar.SECOND;
break;
case "MINUTE":
unit = Calendar.MINUTE;
break;
default:
unit = Calendar.HOUR;
}
calendar.add(unit, applicationProperties.getTokenExpiration().getValue());
expDate = calendar.getTime();
return Jwts.builder().setSubject(subject).claim("roles", roles).setIssuedAt(new Date()).setExpiration(expDate)
.signWith(SignatureAlgorithm.HS256, key).compact();
}
示例3: getInterval
/**
* 獲取時間間隔類型
*
* @param field
* 時間間隔類型
* @return 日曆的時間間隔
*/
protected static int getInterval(String field) {
String tmpField = field.toUpperCase();
if (DEFAULT_YEAR.equals(tmpField)) {
return Calendar.YEAR;
} else if (DEFAULT_MONTH.equals(tmpField)) {
return Calendar.MONTH;
} else if (DEFAULT_DATE.equals(tmpField)) {
return Calendar.DATE;
} else if (DEFAULT_HOUR.equals(tmpField)) {
return Calendar.HOUR;
} else if (DEFAULT_MINUTE.equals(tmpField)) {
return Calendar.MINUTE;
} else {
return Calendar.SECOND;
}
}
示例4: dateDiff
public static long dateDiff(Date startTime, Date endTime, int type) {
long result = 0;
long nd = 1000 * 24 * 60 * 60;// 一天的毫秒數
long nh = 1000 * 60 * 60;// 一小時的毫秒數
long nm = 1000 * 60;// 一分鍾的毫秒數
long ns = 1000;// 一秒鍾的毫秒數
long diff;
try {
// 獲得兩個時間的毫秒時間差異
diff = endTime.getTime() - startTime.getTime();
switch (type) {
case Calendar.DATE:
result = diff / nd;// 計算差多少天
break;
case Calendar.HOUR:
result = diff % nd / nh;// 計算差多少小時
break;
case Calendar.MINUTE:
result = diff % nd % nh / nm;// 計算差多少分鍾
break;
case Calendar.SECOND:
result = diff % nd % nh % nm / ns;// 計算差多少秒
break;
default:
break;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
示例5: compareDate
/**
* 根據單位字段比較兩個日期
*
* @param date
* 日期1
* @param otherDate
* 日期2
* @param withUnit
* 單位字段,從Calendar field取值
* @return 等於返回0值, 大於返回大於0的值 小於返回小於0的值
*/
public static int compareDate(Date date, Date otherDate, int withUnit) {
Calendar dateCal = Calendar.getInstance();
dateCal.setTime(date);
Calendar otherDateCal = Calendar.getInstance();
otherDateCal.setTime(otherDate);
switch (withUnit) {
case Calendar.YEAR:
dateCal.clear(Calendar.MONTH);
otherDateCal.clear(Calendar.MONTH);
case Calendar.MONTH:
dateCal.set(Calendar.DATE, 1);
otherDateCal.set(Calendar.DATE, 1);
case Calendar.DATE:
dateCal.set(Calendar.HOUR_OF_DAY, 0);
otherDateCal.set(Calendar.HOUR_OF_DAY, 0);
case Calendar.HOUR:
dateCal.clear(Calendar.MINUTE);
otherDateCal.clear(Calendar.MINUTE);
case Calendar.MINUTE:
dateCal.clear(Calendar.SECOND);
otherDateCal.clear(Calendar.SECOND);
case Calendar.SECOND:
dateCal.clear(Calendar.MILLISECOND);
otherDateCal.clear(Calendar.MILLISECOND);
case Calendar.MILLISECOND:
break;
default:
throw new IllegalArgumentException("withUnit 單位字段 " + withUnit + " 不合法!!");
}
return dateCal.compareTo(otherDateCal);
}
示例6: compareTime
/**
* 根據單位字段比較兩個時間
*
* @param date
* 時間1
* @param otherDate
* 時間2
* @param withUnit
* 單位字段,從Calendar field取值
* @return 等於返回0值, 大於返回大於0的值 小於返回小於0的值
*/
public static int compareTime(Date date, Date otherDate, int withUnit) {
Calendar dateCal = Calendar.getInstance();
dateCal.setTime(date);
Calendar otherDateCal = Calendar.getInstance();
otherDateCal.setTime(otherDate);
dateCal.clear(Calendar.YEAR);
dateCal.clear(Calendar.MONTH);
dateCal.set(Calendar.DATE, 1);
otherDateCal.clear(Calendar.YEAR);
otherDateCal.clear(Calendar.MONTH);
otherDateCal.set(Calendar.DATE, 1);
switch (withUnit) {
case Calendar.HOUR:
dateCal.clear(Calendar.MINUTE);
otherDateCal.clear(Calendar.MINUTE);
case Calendar.MINUTE:
dateCal.clear(Calendar.SECOND);
otherDateCal.clear(Calendar.SECOND);
case Calendar.SECOND:
dateCal.clear(Calendar.MILLISECOND);
otherDateCal.clear(Calendar.MILLISECOND);
case Calendar.MILLISECOND:
break;
default:
throw new IllegalArgumentException("withUnit 單位字段 " + withUnit + " 不合法!!");
}
return dateCal.compareTo(otherDateCal);
}
示例7: getCalendarField
/**
* Returns the calendarField under the start of the selection, or -1 if
* there is no valid calendar field under the selection (or the spinner
* isn't editing dates.
*/
private int getCalendarField(JSpinner spinner)
{
JComponent editor = spinner.getEditor();
if( editor instanceof JSpinner.DateEditor )
{
JSpinner.DateEditor dateEditor = (JSpinner.DateEditor) editor;
JFormattedTextField ftf = dateEditor.getTextField();
int start = ftf.getSelectionStart();
JFormattedTextField.AbstractFormatter formatter = ftf.getFormatter();
if( formatter instanceof InternationalFormatter )
{
Format.Field[] fields = ((InternationalFormatter) formatter).getFields(start);
for( int counter = 0; counter < fields.length; counter++ )
{
if( fields[counter] instanceof DateFormat.Field )
{
int calendarField;
if( fields[counter] == DateFormat.Field.HOUR1 )
{
calendarField = Calendar.HOUR;
}
else
{
calendarField = ((DateFormat.Field) fields[counter]).getCalendarField();
}
if( calendarField != -1 )
{
return calendarField;
}
}
}
}
}
return -1;
}
示例8: getTimeRelativeTo
public long getTimeRelativeTo(long time)
{
int valueToUse = -(int) value;
int field = 0;
if (getUnit() == TimeUnit.MILLISECONDS)
{
field = Calendar.MILLISECOND;
//we do our own adjustment as calendar cannot handle a long.
time -= value;
valueToUse = 0;
}
else if (getUnit() == TimeUnit.SECONDS)
field = Calendar.SECOND;
else if (getUnit() == TimeUnit.MINUTES)
field = Calendar.MINUTE;
else if (getUnit() == TimeUnit.HOURS)
field = Calendar.HOUR;
else if (getUnit() == TimeUnit.DAYS)
field = Calendar.DATE;
else if (getUnit() == TimeUnit.WEEKS)
field = Calendar.WEEK_OF_MONTH;
else if (getUnit() == TimeUnit.MONTHS)
field = Calendar.MONTH;
else if (getUnit() == TimeUnit.YEARS)
field = Calendar.YEAR;
calendar.setTimeInMillis(time);
calendar.add(field, valueToUse);
return calendar.getTime().getTime();
}
示例9: getFutureTimeRelativeTo
public long getFutureTimeRelativeTo(long time)
{
int valueToUse = (int) value;
int field = 0;
if (getUnit() == TimeUnit.MILLISECONDS)
{
field = Calendar.MILLISECOND;
//we do our own adjustment as calendar cannot handle a long.
time += value;
valueToUse = 0;
}
else if (getUnit() == TimeUnit.SECONDS)
field = Calendar.SECOND;
else if (getUnit() == TimeUnit.MINUTES)
field = Calendar.MINUTE;
else if (getUnit() == TimeUnit.HOURS)
field = Calendar.HOUR;
else if (getUnit() == TimeUnit.DAYS)
field = Calendar.DATE;
else if (getUnit() == TimeUnit.WEEKS)
field = Calendar.WEEK_OF_MONTH;
else if (getUnit() == TimeUnit.MONTHS)
field = Calendar.MONTH;
else if (getUnit() == TimeUnit.YEARS)
field = Calendar.YEAR;
calendar.setTimeInMillis(time);
calendar.add(field, valueToUse);
return calendar.getTime().getTime();
}
示例10: compareTime
/**
* <p>Compares a calendar time value to another, indicating whether it is
* equal, less then or more than at a specified level.</p>
*
* @param value The Calendar value.
* @param compare The <code>Calendar</code> to check the value against.
* @param field The field <i>level</i> to compare to - e.g. specifying
* <code>Calendar.MINUTE</code> will compare the hours and minutes
* portions of the calendar.
* @return Zero if the first value is equal to the second, -1
* if it is less than the second or +1 if it is greater than the second.
*/
protected int compareTime(Calendar value, Calendar compare, int field) {
int result = 0;
// Compare Hour
result = calculateCompareResult(value, compare, Calendar.HOUR_OF_DAY);
if (result != 0 || (field == Calendar.HOUR || field == Calendar.HOUR_OF_DAY)) {
return result;
}
// Compare Minute
result = calculateCompareResult(value, compare, Calendar.MINUTE);
if (result != 0 || field == Calendar.MINUTE) {
return result;
}
// Compare Second
result = calculateCompareResult(value, compare, Calendar.SECOND);
if (result != 0 || field == Calendar.SECOND) {
return result;
}
// Compare Milliseconds
if (field == Calendar.MILLISECOND) {
return calculateCompareResult(value, compare, Calendar.MILLISECOND);
}
throw new IllegalArgumentException("Invalid field: " + field);
}
示例11: makeDatesEven
/**
* 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 its 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 its 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;
}
evenDates.add(calendar.getTime());
}
return evenDates;
} else {
return dates;
}
}