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
/** * 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(); }
Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, 1988); cal.set(Calendar.MONTH, Calendar.JANUARY); cal.set(Calendar.DAY_OF_MONTH, 1); Date dateRepresentation = cal.getTime();
/** helper for return current date **/ public String currentDate() { return new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime()); }
@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 void write(JSONSerializer serializer, Object object, BeanContext context) throws IOException { SerializeWriter out = serializer.out; String format = context.getFormat(); Calendar calendar = (Calendar) object; if (format.equals("unixtime")) { long seconds = calendar.getTimeInMillis() / 1000L; out.writeInt((int) seconds); return; } DateFormat dateFormat = new SimpleDateFormat(format); if (dateFormat == null) { dateFormat = new SimpleDateFormat(JSON.DEFFAULT_DATE_FORMAT, serializer.locale); dateFormat.setTimeZone(serializer.timeZone); } String text = dateFormat.format(calendar.getTime()); out.writeString(text); }
@Override public boolean isSkip() { if (null == m_expireDate) { return false; } try { Calendar now= Calendar.getInstance(); Date nowDate= m_inFormat.parse(m_inFormat.format(now.getTime())); now.setTime(nowDate); return !now.after(m_expireDate); } catch(ParseException pex) { throw new TestNGException("Cannot compare dates."); } }
private Date getDate(int year, int month, int dayOfMonth, int hour, int minute, int second, int millisecond) { Calendar cal = Calendar.getInstance(Locale.US); cal.setTimeZone(UTC); cal.clear(); cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, month); cal.set(Calendar.DAY_OF_MONTH, dayOfMonth); cal.set(Calendar.HOUR, hour); cal.set(Calendar.MINUTE, minute); cal.set(Calendar.SECOND, second); cal.set(Calendar.MILLISECOND, millisecond); return cal.getTime(); }
@Test public void testIncrementSecondWithPreviousExecutionTooEarly() throws Exception { CronTrigger trigger = new CronTrigger("11 * * * * *", timeZone); calendar.set(Calendar.SECOND, 11); SimpleTriggerContext context = new SimpleTriggerContext(); context.update(calendar.getTime(), new Date(calendar.getTimeInMillis() - 100), new Date(calendar.getTimeInMillis() - 90)); calendar.add(Calendar.MINUTE, 1); assertEquals(calendar.getTime(), trigger.nextExecutionTime(context)); }
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(); }
@Test public void testMonthlyTriggerInLongMonth() throws Exception { CronTrigger trigger = new CronTrigger("0 0 0 31 * *", timeZone); calendar.set(Calendar.MONTH, 9); calendar.set(Calendar.DAY_OF_MONTH, 30); Date date = calendar.getTime(); calendar.set(Calendar.DAY_OF_MONTH, 31); 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)); }
// Create an instance of SimpleDateFormat used for formatting // the string representation of date (month/day/year) DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); // Get the date today using Calendar object. Date today = Calendar.getInstance().getTime(); // Using DateFormat format method we can create a string // representation of a date with the defined format. String reportDate = df.format(today); // Print what date is today! System.out.println("Report Date: " + reportDate);
static String timeAsGmt() { SimpleDateFormat sdf = new SimpleDateFormat(); sdf.setTimeZone(TimeZone.getTimeZone("GMT")); sdf.applyPattern("dd MMM yyyy HH:mm:ss z"); return sdf.format(Calendar.getInstance().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)); }
Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, -1); System.out.println("Yesterday's date = "+ cal.getTime());
@Test public void testMonthlyTriggerInShortMonth() throws Exception { CronTrigger trigger = new CronTrigger("0 0 0 1 * *", timeZone); calendar.set(Calendar.MONTH, 9); calendar.set(Calendar.DAY_OF_MONTH, 30); Date date = calendar.getTime(); calendar.set(Calendar.MONTH, 10); calendar.set(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)); }
Calendar c = Calendar.getInstance(); c.setTime(dt); c.add(Calendar.DATE, 1); dt = c.getTime();