本文整理了Java中java.util.Calendar.add()
方法的一些代码示例,展示了Calendar.add()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Calendar.add()
方法的具体详情如下:
包路径:java.util.Calendar
类名称:Calendar
方法名:add
[英]Adds the given amount to a Calendar field.
[中]将给定金额添加到日历字段。
代码示例来源:origin: ctripcorp/apollo
/**
* Currently the instance config is expired by 1 day, add one more hour to avoid possible time
* difference
*/
private Date getValidInstanceConfigDate() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
cal.add(Calendar.HOUR, -1);
return cal.getTime();
}
代码示例来源:origin: stackoverflow.com
String dt = "2008-01-01"; // Start date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(dt));
c.add(Calendar.DATE, 1); // number of days to add
dt = sdf.format(c.getTime()); // dt is now the new date
代码示例来源:origin: stackoverflow.com
// today
Calendar date = new GregorianCalendar();
// reset hour, minutes, seconds and millis
date.set(Calendar.HOUR_OF_DAY, 0);
date.set(Calendar.MINUTE, 0);
date.set(Calendar.SECOND, 0);
date.set(Calendar.MILLISECOND, 0);
// next day
date.add(Calendar.DAY_OF_MONTH, 1);
代码示例来源:origin: spring-projects/spring-framework
@Test
public void testIncrementDayOfMonthByOne() throws Exception {
CronTrigger trigger = new CronTrigger("* * * 10 * *", timeZone);
calendar.set(Calendar.DAY_OF_MONTH, 9);
Date date = calendar.getTime();
calendar.add(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
TriggerContext context = getTriggerContext(date);
assertEquals(calendar.getTime(), trigger.nextExecutionTime(context));
}
代码示例来源:origin: north2016/T-MVP
public static List<String> getOldWeekDays() {
final Calendar c = Calendar.getInstance();
String[] months = new String[8];
for (int i = 0; i < 8; i++) {
months[i] = new SimpleDateFormat("MM.dd").format(new Date(c
.getTimeInMillis()));
c.add(Calendar.DAY_OF_MONTH, -1);
}
return Arrays.asList(months);
}
代码示例来源:origin: spring-projects/spring-framework
calendar.setTime(date);
calendar.set(Calendar.MILLISECOND, 0);
long originalTimestamp = calendar.getTimeInMillis();
doNext(calendar, calendar.get(Calendar.YEAR));
if (calendar.getTimeInMillis() == originalTimestamp) {
calendar.add(Calendar.SECOND, 1);
doNext(calendar, calendar.get(Calendar.YEAR));
return calendar.getTime();
代码示例来源:origin: jaydenxiao2016/AndroidFire
public static String getCurrentDay2() {
String curDateTime = null;
try {
SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(dateFormatYMDHMS);
Calendar c = new GregorianCalendar();
c.add(Calendar.DAY_OF_MONTH, 0);
curDateTime = mSimpleDateFormat.format(c.getTime());
} catch (Exception e) {
e.printStackTrace();
}
return curDateTime;
}
代码示例来源:origin: stackoverflow.com
Date whateverDateYouWant = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(whateverDateYouWant);
int unroundedMinutes = calendar.get(Calendar.MINUTE);
int mod = unroundedMinutes % 15;
calendar.add(Calendar.MINUTE, mod < 8 ? -mod : (15-mod));
代码示例来源:origin: kiegroup/jbpm
private Timestamp getTomorrow() {
Calendar c = Calendar.getInstance();
c.setTime(getToday());
c.add(Calendar.DATE, 1);
return new Timestamp(c.getTimeInMillis());
}
代码示例来源:origin: robovm/robovm
private SimpleDateFormat(Locale locale) {
numberFormat = NumberFormat.getInstance(locale);
numberFormat.setParseIntegerOnly(true);
numberFormat.setGroupingUsed(false);
calendar = new GregorianCalendar(locale);
calendar.add(Calendar.YEAR, -80);
creationYear = calendar.get(Calendar.YEAR);
defaultCenturyStart = calendar.getTime();
}
代码示例来源:origin: Activiti/Activiti
protected Date calculateDueDate(CommandContext commandContext, int waitTimeInSeconds, Date oldDate) {
Calendar newDateCal = new GregorianCalendar();
if (oldDate != null) {
newDateCal.setTime(oldDate);
} else {
newDateCal.setTime(commandContext.getProcessEngineConfiguration().getClock().getCurrentTime());
}
newDateCal.add(Calendar.SECOND, waitTimeInSeconds);
return newDateCal.getTime();
}
代码示例来源:origin: apache/drill
protected Calendar nextDay(Date date, int dayOfWeek) {
calendar.setTime(date);
int currDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
int daysToAdd;
if (currDayOfWeek < dayOfWeek) {
daysToAdd = dayOfWeek - currDayOfWeek;
} else {
daysToAdd = 7 - currDayOfWeek + dayOfWeek;
}
calendar.add(Calendar.DATE, daysToAdd);
return calendar;
}
代码示例来源:origin: rey5137/material
private static long gotoFirstDayOfWeek(Calendar cal){
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
int firstDayOfWeek = cal.getFirstDayOfWeek();
int shift = dayOfWeek >= firstDayOfWeek ? (dayOfWeek - firstDayOfWeek) : (dayOfWeek + 7 - firstDayOfWeek);
cal.add(Calendar.DAY_OF_MONTH, -shift);
return cal.getTimeInMillis();
}
代码示例来源:origin: apache/activemq
protected static long doUpdateCurrentMonth(Calendar working, CronEntry month) throws MessageFormatException {
int currentMonth = working.get(Calendar.MONTH) + 1;
if (!isCurrent(month, currentMonth)) {
int nextMonth = getNext(month, currentMonth, working);
working.add(Calendar.MONTH, nextMonth);
// Reset to start of month.
resetToStartOfDay(working, 1);
return working.getTimeInMillis();
}
return 0L;
}
代码示例来源:origin: spring-projects/spring-framework
private int findNextDay(Calendar calendar, BitSet daysOfMonth, int dayOfMonth, BitSet daysOfWeek, int dayOfWeek,
List<Integer> resets) {
int count = 0;
int max = 366;
// the DAY_OF_WEEK values in java.util.Calendar start with 1 (Sunday),
// but in the cron pattern, they start with 0, so we subtract 1 here
while ((!daysOfMonth.get(dayOfMonth) || !daysOfWeek.get(dayOfWeek - 1)) && count++ < max) {
calendar.add(Calendar.DAY_OF_MONTH, 1);
dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
reset(calendar, resets);
}
if (count >= max) {
throw new IllegalArgumentException("Overflow in day for expression \"" + this.expression + "\"");
}
return dayOfMonth;
}
代码示例来源:origin: stackoverflow.com
Calendar c = Calendar.getInstance();
c.setTime(dt);
c.add(Calendar.DATE, 1);
dt = c.getTime();
代码示例来源:origin: spring-projects/spring-framework
@Test
public void testIncrementDayOfMonthAndRollover() throws Exception {
CronTrigger trigger = new CronTrigger("* * * 10 * *", timeZone);
calendar.set(Calendar.DAY_OF_MONTH, 11);
Date date = calendar.getTime();
calendar.add(Calendar.MONTH, 1);
calendar.set(Calendar.DAY_OF_MONTH, 10);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
TriggerContext context = getTriggerContext(date);
assertEquals(calendar.getTime(), trigger.nextExecutionTime(context));
}
代码示例来源:origin: AsyncHttpClient/async-http-client
/**
* Sets the Date and Cache headers for the HTTP Response
*
* @param response HTTP response
* @param fileToCache file to extract content type
*/
private static void setDateAndCacheHeaders(HttpResponse response, File fileToCache) {
SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
dateFormatter.setTimeZone(TimeZone.getTimeZone(HTTP_DATE_GMT_TIMEZONE));
// Date header
Calendar time = new GregorianCalendar();
response.headers().set(DATE, dateFormatter.format(time.getTime()));
// Add cache headers
time.add(Calendar.SECOND, HTTP_CACHE_SECONDS);
response.headers().set(EXPIRES, dateFormatter.format(time.getTime()));
response.headers().set(CACHE_CONTROL, "private, max-age=" + HTTP_CACHE_SECONDS);
response.headers().set(
LAST_MODIFIED, dateFormatter.format(new Date(fileToCache.lastModified())));
}
代码示例来源:origin: stackoverflow.com
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
System.out.println("Yesterday's date = "+ cal.getTime());
代码示例来源:origin: org.springframework/spring-context
private int findNextDay(Calendar calendar, BitSet daysOfMonth, int dayOfMonth, BitSet daysOfWeek, int dayOfWeek,
List<Integer> resets) {
int count = 0;
int max = 366;
// the DAY_OF_WEEK values in java.util.Calendar start with 1 (Sunday),
// but in the cron pattern, they start with 0, so we subtract 1 here
while ((!daysOfMonth.get(dayOfMonth) || !daysOfWeek.get(dayOfWeek - 1)) && count++ < max) {
calendar.add(Calendar.DAY_OF_MONTH, 1);
dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
reset(calendar, resets);
}
if (count >= max) {
throw new IllegalArgumentException("Overflow in day for expression \"" + this.expression + "\"");
}
return dayOfMonth;
}
内容来源于网络,如有侵权,请联系作者删除!