List of usage examples for java.util GregorianCalendar add
@Override public void add(int field, int amount)
From source file:com.datamountaineer.streamreactor.connect.json.SimpleJsonConverterTest.java
@Test public void dateToJson() throws IOException { GregorianCalendar calendar = new GregorianCalendar(1970, Calendar.JANUARY, 1, 0, 0, 0); calendar.setTimeZone(TimeZone.getTimeZone("UTC")); calendar.add(Calendar.DATE, 10000); java.util.Date date = calendar.getTime(); JsonNode converted = converter.fromConnectData(Date.SCHEMA, date); assertTrue(converted.isTextual());//from w w w.j a v a 2s . com assertEquals(SimpleJsonConverter.ISO_DATE_FORMAT.format(date), converted.textValue()); }
From source file:com.datamountaineer.streamreactor.connect.json.SimpleJsonConverterTest.java
@Test public void timeToJson() throws IOException { GregorianCalendar calendar = new GregorianCalendar(1970, Calendar.JANUARY, 1, 0, 0, 0); calendar.setTimeZone(TimeZone.getTimeZone("UTC")); calendar.add(Calendar.MILLISECOND, 14400000); java.util.Date date = calendar.getTime(); JsonNode converted = converter.fromConnectData(Time.SCHEMA, date); assertTrue(converted.isTextual());// ww w . j a v a2s. com assertEquals(SimpleJsonConverter.TIME_FORMAT.format(date), converted.textValue()); }
From source file:com.datamountaineer.streamreactor.connect.json.SimpleJsonConverterTest.java
@Test public void timestampToJson() throws IOException { GregorianCalendar calendar = new GregorianCalendar(1970, Calendar.JANUARY, 1, 0, 0, 0); calendar.setTimeZone(TimeZone.getTimeZone("UTC")); calendar.add(Calendar.MILLISECOND, 2000000000); calendar.add(Calendar.MILLISECOND, 2000000000); java.util.Date date = calendar.getTime(); JsonNode converted = converter.fromConnectData(Timestamp.SCHEMA, date); assertTrue(converted.isLong());/*from ww w. ja v a 2 s. com*/ assertEquals(4000000000L, converted.longValue()); }
From source file:edu.lternet.pasta.portal.statistics.GrowthStats.java
private ArrayList<Integer> buildFrequencies(GregorianCalendar start, GregorianCalendar end, int scale, Long[] list) {/*from ww w . j a v a 2 s.c om*/ ArrayList<Integer> freqs = new ArrayList<Integer>(); GregorianCalendar lower = (GregorianCalendar) start.clone(); GregorianCalendar upper = new GregorianCalendar(); while (lower.getTimeInMillis() <= end.getTimeInMillis()) { upper.setTime(lower.getTime()); upper.add(scale, 1); int freq = 0; for (int i = 0; i < list.length; i++) { if (lower.getTimeInMillis() <= list[i] && list[i] < upper.getTimeInMillis()) { freq++; //System.out.printf("%d - %d - %d - %d%n", lower.getTimeInMillis(), list[i], upper.getTimeInMillis(), freq); } } freqs.add(freq); lower.setTime(upper.getTime()); } return freqs; }
From source file:org.jamwiki.servlets.PasswordResetServlet.java
private String getChallenge(HttpServletRequest request, WikiPageInfo pageInfo, WikiUser user) throws Exception { int numOfTries = Environment.getIntValue(Environment.PROP_EMAIL_SERVICE_FORGOT_PASSWORD_CHALLENGE_RETRIES); int lockDuration = Environment.getIntValue(Environment.PROP_EMAIL_SERVICE_FORGOT_PASSWORD_IP_LOCK_DURATION); if (user.getChallengeDate() != null && user.getChallengeIp() != null) { // compute some deadlines GregorianCalendar currentDate = new GregorianCalendar(); GregorianCalendar lockExpires = new GregorianCalendar(); lockExpires.setTimeInMillis(user.getChallengeDate().getTime()); lockExpires.add(Calendar.MINUTE, lockDuration); if (request.getRemoteAddr().equals(user.getChallengeIp()) && user.getChallengeTries() >= numOfTries && currentDate.before(lockExpires)) { pageInfo.addError(new WikiMessage("password.reset.password.error.ip.locked")); return null; }/* www.j a v a 2 s .c o m*/ // reset retries after lock timeout if (user.getChallengeTries() >= numOfTries && currentDate.after(lockExpires)) { user.setChallengeTries(0); WikiBase.getDataHandler().updatePwResetChallengeData(user); } } return new Integer((int) (Math.random() * Integer.MAX_VALUE)).toString(); }
From source file:com.edduarte.vokter.job.JobManager.java
public boolean createJob(final SubscribeRequest request) { String documentUrl = request.getDocumentUrl(); String clientUrl = request.getClientUrl(); try {/*from www.jav a 2 s . co m*/ // attempt creating a new DiffDetectorJob JobDetail detectionJob = JobBuilder.newJob(DetectionJob.class) .withIdentity(documentUrl, "detection" + documentUrl) .usingJobData(DetectionJob.PARENT_JOB_MANAGER, managerName) .usingJobData(DetectionJob.FAULT_COUNTER, 0).build(); Trigger detectionTrigger = TriggerBuilder.newTrigger() .withIdentity(documentUrl, "detection" + documentUrl).withSchedule(SimpleScheduleBuilder .simpleSchedule().withIntervalInSeconds(detectionInterval).repeatForever()) .build(); try { scheduler.scheduleJob(detectionJob, detectionTrigger); logger.info("Started detection job for '{}'.", documentUrl); } catch (ObjectAlreadyExistsException ignored) { // there is already a job monitoring the request url, so ignore this } ObjectMapper mapper = new ObjectMapper(); String keywordJson = mapper.writeValueAsString(request.getKeywords()); JobDetail matchingJob = JobBuilder.newJob(MatchingJob.class) .withIdentity(clientUrl, "matching" + documentUrl) .usingJobData(MatchingJob.PARENT_JOB_MANAGER, managerName) .usingJobData(MatchingJob.REQUEST_URL, documentUrl) .usingJobData(MatchingJob.KEYWORDS, keywordJson) .usingJobData(MatchingJob.IGNORE_ADDED, request.getIgnoreAdded()) .usingJobData(MatchingJob.IGNORE_REMOVED, request.getIgnoreRemoved()) .usingJobData(MatchingJob.HAS_NEW_DIFFS, false).build(); GregorianCalendar cal = new GregorianCalendar(); cal.add(Calendar.SECOND, request.getInterval()); Trigger matchingTrigger = TriggerBuilder.newTrigger().withIdentity(clientUrl, "matching" + documentUrl) .startAt(cal.getTime()).withSchedule(SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(request.getInterval()).repeatForever()) .build(); try { scheduler.scheduleJob(matchingJob, matchingTrigger); } catch (ObjectAlreadyExistsException ex) { return false; } } catch (SchedulerException | JsonProcessingException ex) { logger.error(ex.getMessage(), ex); } return true; }
From source file:org.apache.eagle.jpm.mr.history.crawler.JHFCrawlerDriverImpl.java
private void advanceOneDay() throws Exception { //flushJobCount(); GregorianCalendar cal = new GregorianCalendar(timeZone); cal.set(this.processDate.year, this.processDate.month, this.processDate.day, 0, 0, 0); cal.add(Calendar.DATE, 1); this.processDate.year = cal.get(Calendar.YEAR); this.processDate.month = cal.get(Calendar.MONTH); this.processDate.day = cal.get(Calendar.DAY_OF_MONTH); try {//from w w w.j a v a 2s. c o m clearProcessedJob(cal); } catch (Exception e) { LOG.error("failed to clear processed job ", e); } }
From source file:org.jamwiki.servlets.PasswordResetServlet.java
private boolean isChallengeOk(HttpServletRequest request, WikiPageInfo pageInfo, WikiUser user, String challenge) throws Exception { int timeout = Environment.getIntValue(Environment.PROP_EMAIL_SERVICE_FORGOT_PASSWORD_CHALLENGE_TIMEOUT); if (user.getChallengeValue() != null && user.getChallengeDate() != null && user.getChallengeIp() != null) { // compute some deadlines GregorianCalendar currentDate = new GregorianCalendar(); GregorianCalendar challengeExpires = new GregorianCalendar(); challengeExpires.setTimeInMillis(user.getChallengeDate().getTime()); challengeExpires.add(Calendar.MINUTE, timeout); if (!user.getChallengeValue().equals(challenge)) { pageInfo.addError(new WikiMessage("password.reset.password.error.challenge.nok")); } else if (currentDate.after(challengeExpires)) { pageInfo.addError(new WikiMessage("password.reset.password.error.challenge.expired")); resetChallengeData(user);/*from w w w . ja va 2 s . co m*/ } else { resetChallengeData(user); return true; } } else { pageInfo.addError(new WikiMessage("password.reset.password.error.challenge.nok")); } return false; }
From source file:org.apache.eagle.jpm.mr.history.crawler.JHFCrawlerDriverImpl.java
private void readAndCacheLastProcessedDate() throws Exception { String lastProcessedDate = JobHistoryZKStateManager.instance().readProcessedDate(partitionId); Matcher m = PATTERN_JOB_PROCESS_DATE.matcher(lastProcessedDate); if (m.find() && m.groupCount() == 3) { this.processDate.year = Integer.parseInt(m.group(1)); this.processDate.month = Integer.parseInt(m.group(2)) - 1; // zero based month this.processDate.day = Integer.parseInt(m.group(3)); } else {//from w ww . j a va2 s . c o m throw new IllegalStateException("job lastProcessedDate must have format YYYYMMDD " + lastProcessedDate); } GregorianCalendar cal = new GregorianCalendar(timeZone); cal.set(this.processDate.year, this.processDate.month, this.processDate.day, 0, 0, 0); cal.add(Calendar.DATE, 1); List<String> list = JobHistoryZKStateManager.instance() .readProcessedJobs(String.format(FORMAT_JOB_PROCESS_DATE, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH))); if (list != null) { this.processedJobFileNames = new HashSet<>(list); } }
From source file:org.opencms.notification.CmsNotificationCandidates.java
/** * Collects all resources that will expire in short time, or will become valid, or are not modified since a long time.<p> * //from w w w . j ava2s .co m * @param cms the CmsObject * * @throws CmsException if something goes wrong */ public CmsNotificationCandidates(CmsObject cms) throws CmsException { m_resources = new ArrayList(); m_cms = cms; m_cms.getRequestContext() .setCurrentProject(m_cms.readProject(OpenCms.getSystemInfo().getNotificationProject())); String folder = "/"; GregorianCalendar now = new GregorianCalendar(TimeZone.getDefault(), CmsLocaleManager.getDefaultLocale()); now.setTimeInMillis(System.currentTimeMillis()); GregorianCalendar inOneWeek = (GregorianCalendar) now.clone(); inOneWeek.add(Calendar.WEEK_OF_YEAR, 1); Iterator resources; CmsResource resource; // read all files with the 'notification-interval' property set try { resources = m_cms .readResourcesWithProperty(folder, CmsPropertyDefinition.PROPERTY_NOTIFICATION_INTERVAL) .iterator(); while (resources.hasNext()) { resource = (CmsResource) resources.next(); int notification_interval = Integer.parseInt(m_cms .readPropertyObject(resource, CmsPropertyDefinition.PROPERTY_NOTIFICATION_INTERVAL, true) .getValue()); GregorianCalendar intervalBefore = new GregorianCalendar(TimeZone.getDefault(), CmsLocaleManager.getDefaultLocale()); intervalBefore.setTimeInMillis(resource.getDateLastModified()); intervalBefore.add(Calendar.DAY_OF_YEAR, notification_interval); GregorianCalendar intervalAfter = (GregorianCalendar) intervalBefore.clone(); intervalAfter.add(Calendar.WEEK_OF_YEAR, -1); for (int i = 0; (i < 100) && intervalAfter.getTime().before(now.getTime()); i++) { if (intervalBefore.getTime().after(now.getTime())) { m_resources.add(new CmsExtendedNotificationCause(resource, CmsExtendedNotificationCause.RESOURCE_UPDATE_REQUIRED, intervalBefore.getTime())); } intervalBefore.add(Calendar.DAY_OF_YEAR, notification_interval); intervalAfter.add(Calendar.DAY_OF_YEAR, notification_interval); } } } catch (CmsDbEntryNotFoundException e) { // no resources with property 'notification-interval', ignore } // read all files that were not modified longer than the max notification-time GregorianCalendar oneYearAgo = (GregorianCalendar) now.clone(); oneYearAgo.add(Calendar.DAY_OF_YEAR, -OpenCms.getSystemInfo().getNotificationTime()); // create a resource filter to get the resources with CmsResourceFilter filter = CmsResourceFilter.IGNORE_EXPIRATION .addRequireLastModifiedBefore(oneYearAgo.getTimeInMillis()); resources = m_cms.readResources(folder, filter).iterator(); while (resources.hasNext()) { resource = (CmsResource) resources.next(); m_resources.add(new CmsExtendedNotificationCause(resource, CmsExtendedNotificationCause.RESOURCE_OUTDATED, new Date(resource.getDateLastModified()))); } // get all resources that will expire within the next week CmsResourceFilter resourceFilter = CmsResourceFilter.IGNORE_EXPIRATION .addRequireExpireBefore(inOneWeek.getTimeInMillis()); resourceFilter = resourceFilter.addRequireExpireAfter(now.getTimeInMillis()); resources = m_cms.readResources(folder, resourceFilter).iterator(); while (resources.hasNext()) { resource = (CmsResource) resources.next(); m_resources.add(new CmsExtendedNotificationCause(resource, CmsExtendedNotificationCause.RESOURCE_EXPIRES, new Date(resource.getDateExpired()))); } // get all resources that will release within the next week resourceFilter = CmsResourceFilter.IGNORE_EXPIRATION.addRequireReleaseBefore(inOneWeek.getTimeInMillis()); resourceFilter = resourceFilter.addRequireReleaseAfter(now.getTimeInMillis()); resources = m_cms.readResources(folder, resourceFilter).iterator(); while (resources.hasNext()) { resource = (CmsResource) resources.next(); m_resources.add(new CmsExtendedNotificationCause(resource, CmsExtendedNotificationCause.RESOURCE_RELEASE, new Date(resource.getDateReleased()))); } }