@Test public void testTimeToNextExecution() throws Exception { DateTime now = truncateToSeconds(DateTime.now()); DateTime expected = truncateToSeconds(now.plusSeconds(1)); ExecutionTime executionTime = ExecutionTime.forCron(quartzCronParser.parse(EVERY_SECOND)); assertEquals(new Interval(now, expected).toDuration(), executionTime.timeToNextExecution(now)); }
@Test public void test_aggregationByMinute() throws Exception { DateTimeZone utc = DateTimeZone.UTC; DateTime startDate = new DateTime(2014, 1, 1, 0, 0, 0, utc); DateTime endDate = new DateTime(2014, 1, 1, 0, 2, 20, utc); ListDataPointGroup group = new ListDataPointGroup("aggregationByDay"); for (DateTime second = startDate; second.isBefore(endDate); second = second.plusSeconds(10)) { group.addDataPoint(new LongDataPoint(second.getMillis(), 1L)); } SumAggregator aggregator = new SumAggregator(new DoubleDataPointFactoryImpl()); aggregator.setSampling(new Sampling(1, TimeUnit.MINUTES)); aggregator.setAlignSampling(false); DataPointGroup dayCount = aggregator.aggregate(group); assertThat(dayCount.hasNext(), is(true)); assertThat(dayCount.next().getLongValue(), is(6L)); assertThat(dayCount.hasNext(), is(true)); assertThat(dayCount.next().getLongValue(), is(6L)); assertThat(dayCount.hasNext(), is(true)); assertThat(dayCount.next().getLongValue(), is(2L)); assertThat(dayCount.hasNext(), is(false)); }
@Test public void testNextExecutionEverySecond() throws Exception { DateTime now = truncateToSeconds(DateTime.now()); DateTime expected = truncateToSeconds(now.plusSeconds(1)); ExecutionTime executionTime = ExecutionTime.forCron(quartzCronParser.parse(EVERY_SECOND)); assertEquals(expected, executionTime.nextExecution(now)); }
private DateTime ensureSameDate( DateTime date, int years, int monthsOfYear, int dayOfMonth, int hoursOfDay, int minutesOfHour, int secondsOfMinute, DateTimeZone timeZone) { if (date.getSecondOfMinute() != secondsOfMinute) { date = date.plusSeconds(secondsOfMinute - date.getSecondOfMinute()); } if (date.getMinuteOfHour() != minutesOfHour) { date = date.plusMinutes(minutesOfHour - date.getMinuteOfHour()); } if (date.getHourOfDay() != hoursOfDay) { date = date.plusHours(hoursOfDay - date.getHourOfDay()); } if (date.getDayOfMonth() != dayOfMonth) { date = date.plusDays(dayOfMonth - date.getDayOfMonth()); } if (date.getMonthOfYear() != monthsOfYear) { date = date.plusMonths(monthsOfYear - date.getMonthOfYear()); } if (date.getYear() != years) { date = date.plusYears(years - date.getYear()); } return date; }
/** * Test for issue #19 https://github.com/jmrozanec/cron-utils/issues/19 Reported case: When * nextExecution shifts to 32nd day (e.g. 2015-01-31 23:59:59 + 00:00:01), JodaTime will throw an * exception Expected: should shift one month */ @Test public void testShiftTo32ndDay() { String expression = "0/1 * * 1/1 * ? *"; // every second every day ExecutionTime executionTime = ExecutionTime.forCron(quartzCronParser.parse(expression)); DateTime now = new DateTime(2015, 1, 31, 23, 59, 59, 0); DateTime expected = now.plusSeconds(1); DateTime nextExecution = executionTime.nextExecution(now); assertEquals(expected, nextExecution); }
/** * Provide nearest date for next execution. * * @param date - jodatime DateTime instance. If null, a NullPointerException will be raised. * @return DateTime instance, never null. Next execution time. */ public DateTime nextExecution(DateTime date) { Validate.notNull(date); try { DateTime nextMatch = nextClosestMatch(date); if (nextMatch.equals(date)) { nextMatch = nextClosestMatch(date.plusSeconds(1)); } return nextMatch; } catch (NoSuchValueException e) { throw new IllegalArgumentException(e); } }
private void getLocalDateTime() throws JSONException { DateTime localDateTime; double rawOffset; double dstOffset; DateTime utcDate = new DateTime(DateTimeZone.UTC); double origin = (new DateTime(1970, 1, 1, 0, 0, 0).getMillis()) / 1000; double dateNow = utcDate.getMillis() / 1000; double timeStamp = dateNow - origin; Location loc = new Location(location); String urlTimeZone = "https://maps.googleapis.com/maps/api/timezone/json?location="; urlTimeZone += Double.toString(loc.latitude) + "," + Double.toString(loc.longitude) + "×tamp="; urlTimeZone += Double.toString(timeStamp); urlTimeZone += "&sensor=false"; HttpResponse hr = new HttpResponse(urlTimeZone); try { JSONObject tzResponse = new JSONObject(hr.response); dstOffset = tzResponse.getDouble("dstOffset"); rawOffset = tzResponse.getDouble("rawOffset"); timeZoneName = tzResponse.getString("timeZoneName"); localDateTime = utcDate.plusSeconds((int) rawOffset + (int) dstOffset); formattedAddress = loc.formattedAddress; latitude = loc.latitude; longitude = loc.longitude; year = localDateTime.getYear(); month = localDateTime.getMonthOfYear(); day = localDateTime.getDayOfMonth(); localOffset = (int) rawOffset / 3600; DateTimeFormatter dtf = DateTimeFormat.forPattern("dd-MM-yyyy HH:mm:ss"); formattedDateTime = dtf.print(localDateTime); } catch (JSONException e) { System.out.print("JPrayerTime: unknown timezone."); System.exit(1); } }
private void bulkloadDummyStreamData(String streamOwner, DateTime day) throws ClassNotFoundException, SQLException, IOException, NamingException, NoSuchAlgorithmException { DateTime curTime = new DateTime(day); DateTime endTime = new DateTime(day.plusDays(1).minusMillis(1)); // delete for the day StreamDatabaseDriver db = null; try { db = DatabaseConnector.getStreamDatabase(); db.deleteStream(streamOwner, DUMMY_STREAM_NAME, fmt.print(curTime), fmt.print(endTime)); } finally { if (db != null) { try { db.close(); } catch (SQLException e) { e.printStackTrace(); } } } // generate a bulkload data StringBuilder sb = new StringBuilder(); while (curTime.isBefore(endTime)) { sb.append(fmt.print(curTime)); sb.append(",1\n"); curTime = curTime.plusSeconds(DUMMY_STREAM_INTERVAL); } // perform bulkload String bulkloadData = sb.toString(); try { db = DatabaseConnector.getStreamDatabase(); db.bulkLoad(streamOwner, DUMMY_STREAM_NAME, bulkloadData); } finally { if (db != null) { try { db.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
@Test public void shouldGetEntitiesUpdatedAfterGivenDate() { DateTime dateUpdated = new DateTime(2020, 1, 1, 1, 1); Organization organization = InitialData.createOrganization(dateUpdated.plusSeconds(1), "lastUpdated"); CSD csd = InitialData.getInitialData(); csd.getProviderDirectory().getProviders().clear(); csd.getServiceDirectory().getServices().clear(); csd.getFacilityDirectory().getFacilities().clear(); csd.getOrganizationDirectory().getOrganizations().add(organization); csdService.update(csd); assertTrue(csdService.getCSD().equals(csd)); Set<Organization> lastUpdated = organizationService.getModifiedAfter(dateUpdated); assertEquals(1, lastUpdated.size()); assertEquals(lastUpdated.iterator().next(), organization); }
public static void main(String[] args) { // // Creates an instance of current DateTime which represents the // current date time. // DateTime dateTime = new DateTime(); System.out.println("DateTime = " + dateTime); // // Plus some hours, minutes, and seconds to the original DateTime. // System.out.println("Plus 1 hour is " + dateTime.plusHours(1)); System.out.println("Plus 10 minutes is " + dateTime.plusMinutes(10)); System.out.println("Plus 60 seconds is " + dateTime.plusSeconds(60)); // // Minus some hours, minutes, and seconds to the original DateTime. // System.out.println("Minus 1 hour is " + dateTime.minusHours(1)); System.out.println("Minus 10 minutes is " + dateTime.minusMinutes(10)); System.out.println("Minus 60 seconds is " + dateTime.minusSeconds(60)); }
/** * 计算指定时间之后的日期 * * @param date 待计算时间 * @param random 待计算数 * @param timeType 时间类型枚举 * @see org.codingsills.modules.utils.DateKit.TimeType * @return Date */ public static Date nextDate(Date date, int random, TimeType timeType) { Date nextDate = null; if (date == null) { return nextDate; } DateTime dateTime = fromDate(date); random = Math.abs(random); switch (timeType.getCode()) { case "Y": nextDate = dateTime.plusYears(random).toDate(); break; case "M": nextDate = dateTime.plusMonths(random).toDate(); break; case "W": nextDate = dateTime.plusWeeks(random).toDate(); break; case "D": nextDate = dateTime.plusDays(random).toDate(); break; case "H": nextDate = dateTime.plusHours(random).toDate(); break; case "MIN": nextDate = dateTime.plusMinutes(random).toDate(); break; case "S": nextDate = dateTime.plusSeconds(random).toDate(); break; case "MS": nextDate = dateTime.plusMillis(random).toDate(); break; default: break; } return nextDate; }
@JRubyMethod(name = "succ") public RubyTime succ() { return newTime(getRuntime(), dt.plusSeconds(1)); }
public static Date plusSeconds(Date date, int numberOfSeconds) { if (date == null) date = new Date(); DateTime dt = new DateTime(date); return dt.plusSeconds(numberOfSeconds).toDate(); }
public static String computeTime(String dateString, String val, String timeUnit) { DateTime date = new DateTime(); int value = 0; boolean isLong = false; if (!Strings.isEmpty(val) && !Strings.isEmpty(dateString) && !Strings.isEmpty(timeUnit)) { if (dateString.substring(dateString.indexOf("T") + 1, dateString.length()).length() == 0) { dateString = dateString + DEFAULT_TIME_FORMAT; } date = new DateTime(dateString); if (val.indexOf(".") > 0) { val = val.substring(0, val.indexOf(".")); } Long l = new Long(val); if ((l.longValue() < 0 && -l.longValue() > 32768) || (l.longValue() > 0 && l.longValue() > 32768)) { isLong = true; } if (!isLong) { value = Integer.parseInt(val); } if (!Strings.isEmpty(timeUnit)) { if (timeUnit.equals("d")) { if (!isLong) { if (value > 0) { date = date.plusDays(value); } else { date = date.minusDays(-value); } } else { if (l.longValue() < 0) { date = date.minus(-l.longValue() * 24 * 60 * 60 * 1000); } else { date = date.plus(l.longValue() * 24 * 60 * 60 * 1000); } } } else if (timeUnit.equals(HOUR)) { if (!isLong) { if (value > 0) { date = date.plusHours(value); } else { date = date.minusHours(-value); } } else { if (l.longValue() < 0) { date = date.minus(-l.longValue() * 60 * 60 * 1000); } else { date = date.plus(l.longValue() * 60 * 60 * 1000); } } } else if (timeUnit.equals(MIN)) { if (!isLong) { if (value > 0) { date = date.plusMinutes(value); } else { date = date.minusMinutes(-value); } } else { if (l.longValue() < 0) { date = date.minus(-l.longValue() * 60 * 1000); } else { date = date.plus(l.longValue() * 60 * 1000); } } } else if (timeUnit.equals(SEC)) { if (!isLong) { if (value > 0) { date = date.plusSeconds(value); } else { date = date.minusSeconds(-value); } } else { if (l.longValue() < 0) { date = date.minus(-l.longValue() * 1000); } else { date = date.plus(l.longValue() * 1000); } } } } } return FormatUtils.formatDateTimeToISO(date.toDate()); }
public synchronized DateTime addSecond() { systemDate = systemDate.plusSeconds(1); return systemDate; }
public synchronized DateTime addSeconds(int seconds) { systemDate = systemDate.plusSeconds(seconds); return systemDate; }