/** * 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(); }
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
@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)); }
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); }
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();
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; }
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));
private Timestamp getTomorrow() { Calendar c = Calendar.getInstance(); c.setTime(getToday()); c.add(Calendar.DATE, 1); return new Timestamp(c.getTimeInMillis()); }
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(); }
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(); }
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; }
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(); }
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; }
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; }
Calendar c = Calendar.getInstance(); c.setTime(dt); c.add(Calendar.DATE, 1); dt = c.getTime();
@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)); }
/** * 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()))); }
Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, -1); System.out.println("Yesterday's date = "+ cal.getTime());
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; }