本文整理匯總了Java中java.util.Calendar.DATE屬性的典型用法代碼示例。如果您正苦於以下問題:Java Calendar.DATE屬性的具體用法?Java Calendar.DATE怎麽用?Java Calendar.DATE使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類java.util.Calendar
的用法示例。
在下文中一共展示了Calendar.DATE屬性的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getMillisPerUnit
/**
* 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;
}
示例2: getCalendarField
/**
* Returns a field code (that can be used with the Calendar class) for a given 'unit' code.
* The 'unit' is one of: YEAR, MONTH, DAY, HOUR, MINUTE, SECOND and MILLISECOND.
*
* @param unit the unit.
*
* @return the field code.
*/
private int getCalendarField(int unit) {
switch (unit) {
case (YEAR):
return Calendar.YEAR;
case (MONTH):
return Calendar.MONTH;
case (DAY):
return Calendar.DATE;
case (HOUR):
return Calendar.HOUR_OF_DAY;
case (MINUTE):
return Calendar.MINUTE;
case (SECOND):
return Calendar.SECOND;
case (MILLISECOND):
return Calendar.MILLISECOND;
default:
return Calendar.MILLISECOND;
}
}
示例3: getCalendarField
/**
* Returns a field code (that can be used with the Calendar class) for a
* given 'unit' code. The 'unit' is one of: {@link #YEAR}, {@link #MONTH},
* {@link #DAY}, {@link #HOUR}, {@link #MINUTE}, {@link #SECOND} and
* {@link #MILLISECOND}.
*
* @param tickUnit the unit.
*
* @return The field code.
*/
private int getCalendarField(int tickUnit) {
switch (tickUnit) {
case (YEAR):
return Calendar.YEAR;
case (MONTH):
return Calendar.MONTH;
case (DAY):
return Calendar.DATE;
case (HOUR):
return Calendar.HOUR_OF_DAY;
case (MINUTE):
return Calendar.MINUTE;
case (SECOND):
return Calendar.SECOND;
case (MILLISECOND):
return Calendar.MILLISECOND;
default:
return Calendar.MILLISECOND;
}
}
示例4: 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;
}
}
示例5: 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;
}
示例6: 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);
}
示例7: 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();
}
示例8: 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();
}
示例9: checkTime
private boolean checkTime(Date date, int kind) {
Calendar currentCalendar = Calendar.getInstance();
currentCalendar.setTime(Utils.getAnalyticsDate());
Calendar orderCalendar = Calendar.getInstance();
orderCalendar.setTime(date);
switch (kind) {
case Calendar.DATE:
if (orderCalendar.get(Calendar.YEAR) == currentCalendar.get(Calendar.YEAR) &&
orderCalendar.get(Calendar.MONTH) == currentCalendar.get(Calendar.MONTH) &&
orderCalendar.get(Calendar.DATE) == currentCalendar.get(Calendar.DAY_OF_MONTH)) {
return true;
}
break;
case Calendar.WEEK_OF_YEAR:
if (orderCalendar.get(Calendar.YEAR) == currentCalendar.get(Calendar.YEAR) &&
orderCalendar.get(Calendar.WEEK_OF_YEAR) == currentCalendar.get(Calendar.WEEK_OF_YEAR)) {
return true;
}
break;
case Calendar.MONTH:
if (orderCalendar.get(Calendar.YEAR) == currentCalendar.get(Calendar.YEAR) &&
orderCalendar.get(Calendar.MONTH) == currentCalendar.get(Calendar.MONTH)) {
return true;
}
break;
default:
return false;
}
return false;
}
示例10: computeGregorianFields
/**
* 使用農曆日期計算出公曆日期
*/
private void computeGregorianFields() {
int y = lunarYear;
int m = lunarMonth;
int d = lunarDate;
areLunarFieldsComputed = true;
areFieldsSet = true;
lastSetLunar = false;
// 調整日期範圍
if (y < 1900) {
y = 1899;
}
else if (y > 2100) {
y = 2101;
}
if (m < -12) {
m = -12;
}
else if (m > 12) {
m = 12;
}
if (d < 1) {
d = 1;
}
else if (d > 30) {
d = 30;
}
int dateInt = y * 10000 + Math.abs(m) * 100 + d;
if (dateInt < 19001111) {
// 太小
set(1901, Calendar.JANUARY, 1);
super.complete();
} else if (dateInt > 21001201) {
// 太大
set(2100, Calendar.DECEMBER, 31);
super.complete();
} else {
if (Math.abs(m) > 12) {
m = 12;
}
int days = LunarCalendar.daysInLunarMonth(y, m);
if (days == 0) {
m = -m;
days = LunarCalendar.daysInLunarMonth(y, m);
}
if (d > days) {
d = days;
}
set(y, Math.abs(m) - 1, d);
computeLunarFields();
int amount = 0;
while (lunarYear != y || lunarMonth != m) {
amount += daysInLunarMonth(lunarYear, lunarMonth);
lunarMonth = nextLunarMonth(lunarYear, lunarMonth);
}
amount += d - lunarDate;
super.add(Calendar.DATE, amount);
}
computeLunarFields();
}
示例11: getFragment
/**
* 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;
}
示例12: 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;
}
}
示例13: setArrivalTime
public void setArrivalTime(String crisisID){
String currentDateTimeString = "" + Calendar.DATE + Calendar.getInstance().getTime();
mDatabase.child("crisis").child(crisisID).child("arrivalTime").setValue(currentDateTimeString);
}
示例14: getAllOrdersCount
public PieChartData getAllOrdersCount(String restaurantId, int kind) {
float[] time;
if (kind == Calendar.DATE) time = new float[24];
else if (kind == Calendar.WEEK_OF_YEAR) time = new float[7];
else time = new float[31];
for (int i = 0; i < time.length; i++) time[i] = 0;
List<String> ordersList = getOrders(restaurantId);
Calendar calendar = Calendar.getInstance();
Order order;
for (String orderKey : ordersList) {
order = orderRepository.get(orderKey);
if (checkTime(order.getDate(), kind)) {
calendar.setTime(order.getDate());
if (kind == Calendar.DATE)
time[calendar.get(Calendar.HOUR_OF_DAY)] += 1.0f;
else if (kind == Calendar.WEEK_OF_YEAR)
time[calendar.get(Calendar.DAY_OF_WEEK)-1] += 1.0f;
else
time[calendar.get(Calendar.DAY_OF_MONTH)] += 1.0f;
}
}
Integer count = 0;
for (float t : time) if (t != 0) ++count;
PieChartData data = new PieChartData(count);
String[] xData = data.getXData();
float[] yData = data.getYData();
Integer j = 0;
for (int i = 0; i < time.length; i++) {
if (time[i] != 0) {
yData[j] = time[i];
String label = "";
if (kind == Calendar.DATE) {
if (i < 10) label += '0';
label += String.valueOf(i) + "h - ";
if (i + 1 < 10) label += '0';
label += String.valueOf(i + 1) + 'h';
} else if (kind == Calendar.WEEK_OF_YEAR) {
if (i == 0) label = "Sunday";
else if (i == 1) label = "Monday";
else if (i == 2) label = "Tuesday";
else if (i == 3) label = "Wednesday";
else if (i == 4) label = "Thursday";
else if (i == 5) label = "Friday";
else if (i == 6) label = "Saturday";
} else {
label = "Day number " + String.valueOf(i) + " of the month.";
}
xData[j] = label;
++j;
}
}
data.setXData(xData);
data.setYData(yData);
return data;
}