List of usage examples for org.joda.time LocalDateTime plusHours
public LocalDateTime plusHours(int hours)
From source file:com.axelor.apps.crm.service.EventReminderService.java
License:Open Source License
public LocalDateTime computeEndDateTime(LocalDateTime startDateTime, int durationHours, int durationMinutes) { return startDateTime.plusHours(durationHours).plusMinutes(durationMinutes); }
From source file:com.axelor.apps.organisation.service.LeaveRequestService.java
License:Open Source License
public LocalDateTime computeEndDateTime(LocalDateTime startDateTime, double durationDay) { return startDateTime.plusHours((int) durationDay * 24); }
From source file:com.axelor.apps.production.web.OperationOrderController.java
License:Open Source License
public void chargeByMachineHours(ActionRequest request, ActionResponse response) throws AxelorException { List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>(); DateTimeFormatter parser = ISODateTimeFormat.dateTime(); LocalDateTime fromDateTime = LocalDateTime.parse(request.getContext().get("fromDateTime").toString(), parser);/* w w w . j av a 2s .com*/ LocalDateTime toDateTime = LocalDateTime.parse(request.getContext().get("toDateTime").toString(), parser); LocalDateTime itDateTime = new LocalDateTime(fromDateTime); if (Days.daysBetween( new LocalDate(fromDateTime.getYear(), fromDateTime.getMonthOfYear(), fromDateTime.getDayOfMonth()), new LocalDate(toDateTime.getYear(), toDateTime.getMonthOfYear(), toDateTime.getDayOfMonth())) .getDays() > 20) { throw new AxelorException(String.format(I18n.get(IExceptionMessage.CHARGE_MACHINE_DAYS)), IException.CONFIGURATION_ERROR); } List<OperationOrder> operationOrderListTemp = operationOrderRepo.all() .filter("self.plannedStartDateT <= ?2 AND self.plannedEndDateT >= ?1", fromDateTime, toDateTime) .fetch(); Set<String> machineNameList = new HashSet<String>(); for (OperationOrder operationOrder : operationOrderListTemp) { if (operationOrder.getWorkCenter() != null && operationOrder.getWorkCenter().getMachine() != null) { if (!machineNameList.contains(operationOrder.getWorkCenter().getMachine().getName())) { machineNameList.add(operationOrder.getWorkCenter().getMachine().getName()); } } } while (!itDateTime.isAfter(toDateTime)) { List<OperationOrder> operationOrderList = operationOrderRepo.all() .filter("self.plannedStartDateT <= ?2 AND self.plannedEndDateT >= ?1", itDateTime, itDateTime.plusHours(1)) .fetch(); Map<String, BigDecimal> map = new HashMap<String, BigDecimal>(); for (OperationOrder operationOrder : operationOrderList) { if (operationOrder.getWorkCenter() != null && operationOrder.getWorkCenter().getMachine() != null) { String machine = operationOrder.getWorkCenter().getMachine().getName(); int numberOfMinutes = 0; if (operationOrder.getPlannedStartDateT().isBefore(itDateTime)) { numberOfMinutes = Minutes.minutesBetween(itDateTime, operationOrder.getPlannedEndDateT()) .getMinutes(); } else if (operationOrder.getPlannedEndDateT().isAfter(itDateTime.plusHours(1))) { numberOfMinutes = Minutes .minutesBetween(operationOrder.getPlannedStartDateT(), itDateTime.plusHours(1)) .getMinutes(); } else { numberOfMinutes = Minutes.minutesBetween(operationOrder.getPlannedStartDateT(), operationOrder.getPlannedEndDateT()).getMinutes(); } if (numberOfMinutes > 60) { numberOfMinutes = 60; } BigDecimal percentage = new BigDecimal(numberOfMinutes).multiply(new BigDecimal(100)) .divide(new BigDecimal(60), 2, RoundingMode.HALF_UP); if (map.containsKey(machine)) { map.put(machine, map.get(machine).add(percentage)); } else { map.put(machine, percentage); } } } Set<String> keyList = map.keySet(); for (String key : machineNameList) { if (keyList.contains(key)) { Map<String, Object> dataMap = new HashMap<String, Object>(); if (Hours.hoursBetween(fromDateTime, toDateTime).getHours() > 24) { dataMap.put("dateTime", (Object) itDateTime.toString("dd/MM/yyyy HH:mm")); } else { dataMap.put("dateTime", (Object) itDateTime.toString("HH:mm")); } dataMap.put("charge", (Object) map.get(key)); dataMap.put("machine", (Object) key); dataList.add(dataMap); } else { Map<String, Object> dataMap = new HashMap<String, Object>(); if (Hours.hoursBetween(fromDateTime, toDateTime).getHours() > 24) { dataMap.put("dateTime", (Object) itDateTime.toString("dd/MM/yyyy HH:mm")); } else { dataMap.put("dateTime", (Object) itDateTime.toString("HH:mm")); } dataMap.put("charge", (Object) BigDecimal.ZERO); dataMap.put("machine", (Object) key); dataList.add(dataMap); } } itDateTime = itDateTime.plusHours(1); } response.setData(dataList); }
From source file:com.axelor.apps.production.web.OperationOrderController.java
License:Open Source License
public void chargeByMachineDays(ActionRequest request, ActionResponse response) throws AxelorException { List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>(); DateTimeFormatter parser = ISODateTimeFormat.dateTime(); LocalDateTime fromDateTime = LocalDateTime.parse(request.getContext().get("fromDateTime").toString(), parser);/*from w w w .ja v a 2 s .com*/ fromDateTime = fromDateTime.withHourOfDay(0).withMinuteOfHour(0); LocalDateTime toDateTime = LocalDateTime.parse(request.getContext().get("toDateTime").toString(), parser); toDateTime = toDateTime.withHourOfDay(23).withMinuteOfHour(59); LocalDateTime itDateTime = new LocalDateTime(fromDateTime); if (Days.daysBetween( new LocalDate(fromDateTime.getYear(), fromDateTime.getMonthOfYear(), fromDateTime.getDayOfMonth()), new LocalDate(toDateTime.getYear(), toDateTime.getMonthOfYear(), toDateTime.getDayOfMonth())) .getDays() > 500) { throw new AxelorException(String.format(I18n.get(IExceptionMessage.CHARGE_MACHINE_DAYS)), IException.CONFIGURATION_ERROR); } List<OperationOrder> operationOrderListTemp = operationOrderRepo.all() .filter("self.plannedStartDateT <= ?2 AND self.plannedEndDateT >= ?1", fromDateTime, toDateTime) .fetch(); Set<String> machineNameList = new HashSet<String>(); for (OperationOrder operationOrder : operationOrderListTemp) { if (operationOrder.getWorkCenter() != null && operationOrder.getWorkCenter().getMachine() != null) { if (!machineNameList.contains(operationOrder.getWorkCenter().getMachine().getName())) { machineNameList.add(operationOrder.getWorkCenter().getMachine().getName()); } } } while (!itDateTime.isAfter(toDateTime)) { List<OperationOrder> operationOrderList = operationOrderRepo.all() .filter("self.plannedStartDateT <= ?2 AND self.plannedEndDateT >= ?1", itDateTime, itDateTime.plusHours(1)) .fetch(); Map<String, BigDecimal> map = new HashMap<String, BigDecimal>(); for (OperationOrder operationOrder : operationOrderList) { if (operationOrder.getWorkCenter() != null && operationOrder.getWorkCenter().getMachine() != null) { String machine = operationOrder.getWorkCenter().getMachine().getName(); int numberOfMinutes = 0; if (operationOrder.getPlannedStartDateT().isBefore(itDateTime)) { numberOfMinutes = Minutes.minutesBetween(itDateTime, operationOrder.getPlannedEndDateT()) .getMinutes(); } else if (operationOrder.getPlannedEndDateT().isAfter(itDateTime.plusHours(1))) { numberOfMinutes = Minutes .minutesBetween(operationOrder.getPlannedStartDateT(), itDateTime.plusHours(1)) .getMinutes(); } else { numberOfMinutes = Minutes.minutesBetween(operationOrder.getPlannedStartDateT(), operationOrder.getPlannedEndDateT()).getMinutes(); } if (numberOfMinutes > 60) { numberOfMinutes = 60; } int numberOfMinutesPerDay = 0; if (operationOrder.getWorkCenter().getMachine().getWeeklyPlanning() != null) { DayPlanning dayPlanning = weeklyPlanningService.findDayPlanning( operationOrder.getWorkCenter().getMachine().getWeeklyPlanning(), new LocalDate(itDateTime)); numberOfMinutesPerDay = Minutes .minutesBetween(dayPlanning.getMorningFrom(), dayPlanning.getMorningTo()) .getMinutes(); numberOfMinutesPerDay += Minutes .minutesBetween(dayPlanning.getAfternoonFrom(), dayPlanning.getAfternoonTo()) .getMinutes(); } else { numberOfMinutesPerDay = 60 * 8; } BigDecimal percentage = new BigDecimal(numberOfMinutes).multiply(new BigDecimal(100)) .divide(new BigDecimal(numberOfMinutesPerDay), 2, RoundingMode.HALF_UP); if (map.containsKey(machine)) { map.put(machine, map.get(machine).add(percentage)); } else { map.put(machine, percentage); } } } Set<String> keyList = map.keySet(); for (String key : machineNameList) { if (keyList.contains(key)) { int found = 0; for (Map<String, Object> mapIt : dataList) { if (mapIt.get("dateTime").equals((Object) itDateTime.toString("dd/MM/yyyy")) && mapIt.get("machine").equals((Object) key)) { mapIt.put("charge", new BigDecimal(mapIt.get("charge").toString()).add(map.get(key))); found = 1; break; } } if (found == 0) { Map<String, Object> dataMap = new HashMap<String, Object>(); dataMap.put("dateTime", (Object) itDateTime.toString("dd/MM/yyyy")); dataMap.put("charge", (Object) map.get(key)); dataMap.put("machine", (Object) key); dataList.add(dataMap); } } } itDateTime = itDateTime.plusHours(1); } response.setData(dataList); }
From source file:com.axelor.csv.script.ImportDateTime.java
License:Open Source License
public LocalDateTime updateHour(LocalDateTime dateTime, String hour) { if (!Strings.isNullOrEmpty(hour)) { Matcher matcher = patternMonth.matcher(hour); if (matcher.find()) { Integer hours = Integer.parseInt(matcher.group()); if (hour.startsWith("+")) dateTime = dateTime.plusHours(hours); else if (hour.startsWith("-")) dateTime = dateTime.minusHours(hours); else//from w w w . ja v a 2 s. c om dateTime = dateTime.withHourOfDay(hours); } } return dateTime; }
From source file:com.battlelancer.seriesguide.util.TimeTools.java
License:Apache License
/** * Handles DST gap (typically a missing clock hour when DST is getting enabled) by moving the * time forward in hour increments until the local date time is outside the gap. *//*from w w w . j a v a 2s. c o m*/ private static LocalDateTime handleDstGap(DateTimeZone showTimeZone, LocalDateTime localDateTime) { while (showTimeZone.isLocalDateTimeGap(localDateTime)) { // move time forward in 1 hour increments, until outside of the gap localDateTime = localDateTime.plusHours(1); } return localDateTime; }
From source file:com.google.api.ads.adwords.jaxws.extensions.report.model.util.DateUtil.java
License:Open Source License
/** * Attempts to parse the given {@code String} to a {@code DateTime} using one of the known * formatters.//from w w w. ja v a 2 s.c om * * The attempt falls back to all the formatters, and if the format is unknown, {@code null} is * returned. * * @param timestamp the time stamp in {@code String} format. * @return the parsed {@code DateTime}, or {@code null} in case that the format is unknown. */ public static DateTime parseDateTime(String timestamp) { if (timestamp != null) { for (DateTimeFormatter formatter : DateUtil.formatters) { try { LocalDateTime localDateTime = formatter.parseLocalDateTime(timestamp); return localDateTime.plusHours(12).toDateTime(DateTimeZone.UTC); } catch (IllegalArgumentException e) { // silently skips to the next formatter } } } return null; }
From source file:com.restservice.serviceLogic.ResultLogic.java
License:Open Source License
/** * Request handler for getting the count per hour statistic * Additional logic beyond the transactor database connection to insert additional zeros * for dates where no count (row) has been returned * //from ww w .j a va 2 s . c o m * @param id * search term index * @return envelope containing a status message and a search result count * per date DTO * @throws SQLException * @throws ExecutionException * @throws InterruptedException */ public Envelope getCountAndNewsPerHour(Long id, String lang) throws SQLException, InterruptedException, ExecutionException { SearchTermsPerQueryPerDate countsPerDay; Envelope env = new Envelope(); countsPerDay = transactor.getCountPerHour(id, lang); // Fill with zeros ArrayList<Integer> newCounts = new ArrayList<Integer>(); ArrayList<LocalDateTime> newDates = new ArrayList<LocalDateTime>(); if (!countsPerDay.getDates().isEmpty()) { ArrayList<LocalDateTime> oldDates = new ArrayList<>(); for (LocalDateTime curDate : countsPerDay.getDates()) { oldDates.add(new LocalDateTime(curDate.getYear(), curDate.getMonthOfYear(), curDate.getDayOfMonth(), curDate.getHourOfDay(), 0)); } newDates.add(oldDates.get(0)); newCounts.add(countsPerDay.getCounts().get(0)); for (int i = 1; i < oldDates.size(); i++) { if (!oldDates.get(i - 1).plusHours(1).equals(oldDates.get(i))) { LocalDateTime startDate = oldDates.get(i - 1); LocalDateTime endDate = oldDates.get(i); while (!startDate.equals(endDate)) { startDate = startDate.plusHours(1); if (startDate.equals(endDate)) { newDates.add(oldDates.get(i)); newCounts.add(countsPerDay.getCounts().get(i)); } else { newCounts.add(0); newDates.add(startDate); } } } else { newDates.add(oldDates.get(i)); newCounts.add(countsPerDay.getCounts().get(i)); } } } countsPerDay.setCounts(newCounts); countsPerDay.setDates(newDates); countsPerDay.updateDateStrings(); // convert to nice output format CountAndNewsPerHour countAndNews = new CountAndNewsPerHour(); for (Integer index = 0; index < countsPerDay.getCounts().size(); index++) { CountPeaksNewsAndDate element = new CountPeaksNewsAndDate(); element.setRawDate(countsPerDay.getDates().get(index)); element.setCount(countsPerDay.getCounts().get(index)); element.setPeak(false); countAndNews.getGraph().add(element); } countAndNews.setQuery(countsPerDay.getQuery()); // find and marks peaks ArrayList<Integer> peakIndices = PeaksUtil.findPeaks24(countAndNews); for (Integer peakIndex : peakIndices) { countAndNews.getGraph().get(peakIndex).setPeak(true); } if (peakIndices.size() > 0) { // create news fetchers HashMap<Integer, Future<ArrayList<NewsItem>>> newsFetchers = new HashMap<Integer, Future<ArrayList<NewsItem>>>(); ExecutorService executor = Executors.newFixedThreadPool(peakIndices.size()); for (Integer peakIndex : peakIndices) { LocalDateTime date = countAndNews.getGraph().get(peakIndex).getRawDate(); newsFetchers.put(peakIndex, executor.submit( new TopNewsFetcherThread(id, date.getDayOfMonth(), date.getMonthOfYear(), date.getYear()))); } // retrieve news fetchers results executor.shutdown(); java.util.Iterator<Entry<Integer, Future<ArrayList<NewsItem>>>> iterator = newsFetchers.entrySet() .iterator(); while (iterator.hasNext()) { Entry<Integer, Future<ArrayList<NewsItem>>> entry = iterator.next(); ArrayList<NewsItem> result = entry.getValue().get(); if (result != null) { for (NewsItem newsitem : result) { countAndNews.getGraph().get(entry.getKey()).getNews().add(newsitem.toShortString()); } } } } env.setData(countAndNews); return env; }
From source file:com.restservice.serviceLogic.ResultLogic.java
License:Open Source License
/** * Request handler for getting the count per hour statistic * //from www. j a va 2 s . c o m * @param id * search term index * @param lang * iso language code of the language (all languages are selected * if this parameter is null) * * @return envelope containing a status message and the number of * positive/negative tweets per hour * @throws SQLException */ public Envelope getSentimentPerHour(Long id, String lang) throws SQLException { SentimentPerQueryPerDate data; Envelope env = new Envelope(); // TODO: Filling these zeros shouldn't been done three times (twice // here and once in getCountPerHour). Fix it! See ticket #86 data = transactor.getSentimentPerHour(id, lang); ArrayList<LocalDateTime> oldDatesPositive = new ArrayList<>(); ArrayList<Integer> newCountsPositive = new ArrayList<Integer>(); ArrayList<LocalDateTime> newDatesPositive = new ArrayList<LocalDateTime>(); ArrayList<LocalDateTime> oldDatesNegative = new ArrayList<>(); ArrayList<Integer> newCountsNegative = new ArrayList<Integer>(); ArrayList<LocalDateTime> newDatesNegative = new ArrayList<LocalDateTime>(); // Reset minutes, seconds and miliseconds to 0 if (!data.getPositiveCounts().getDates().isEmpty()) { for (LocalDateTime curDate : data.getPositiveCounts().getDates()) { oldDatesPositive.add(new LocalDateTime(curDate.getYear(), curDate.getMonthOfYear(), curDate.getDayOfMonth(), curDate.getHourOfDay(), 0)); } } if (!data.getNegativeCounts().getDates().isEmpty()) { for (LocalDateTime curDate : data.getNegativeCounts().getDates()) { oldDatesNegative.add(new LocalDateTime(curDate.getYear(), curDate.getMonthOfYear(), curDate.getDayOfMonth(), curDate.getHourOfDay(), 0)); } } // Get first date from both (positive or negative) and fill the // other one with leading zero counts if (!oldDatesPositive.isEmpty() && !oldDatesNegative.isEmpty()) { // The first positive date is earlier than the first negative // date if (oldDatesPositive.get(0).compareTo(oldDatesNegative.get(0)) == -1) { LocalDateTime curDate = oldDatesPositive.get(0); while (!curDate.equals(oldDatesNegative.get(0))) { newCountsNegative.add(0); newDatesNegative.add(curDate); curDate = curDate.plusHours(1); } } // The first negative date is earlier than the first positive // date else if (oldDatesPositive.get(0).compareTo(oldDatesNegative.get(0)) == 1) { LocalDateTime curDate = oldDatesNegative.get(0); while (!curDate.equals(oldDatesPositive.get(0))) { newCountsPositive.add(0); newDatesPositive.add(curDate); curDate = curDate.plusHours(1); } } } // Fill hours that have 0 counts for positive tweets if (!oldDatesPositive.isEmpty()) { newDatesPositive.add(oldDatesPositive.get(0)); newCountsPositive.add(data.getPositiveCounts().getCounts().get(0)); for (int i = 1; i < oldDatesPositive.size(); i++) { if (!oldDatesPositive.get(i - 1).plusHours(1).equals(oldDatesPositive.get(i))) { LocalDateTime startDate = oldDatesPositive.get(i - 1); LocalDateTime endDate = oldDatesPositive.get(i); while (!startDate.equals(endDate)) { startDate = startDate.plusHours(1); if (startDate.equals(endDate)) { newDatesPositive.add(oldDatesPositive.get(i)); newCountsPositive.add(data.getPositiveCounts().getCounts().get(i)); } else { newCountsPositive.add(0); newDatesPositive.add(startDate); } } } else { newDatesPositive.add(oldDatesPositive.get(i)); newCountsPositive.add(data.getPositiveCounts().getCounts().get(i)); } } } // Fill hours that have 0 counts for negative tweets if (!oldDatesNegative.isEmpty()) { newDatesNegative.add(oldDatesNegative.get(0)); newCountsNegative.add(data.getNegativeCounts().getCounts().get(0)); for (int i = 1; i < oldDatesNegative.size(); i++) { if (!oldDatesNegative.get(i - 1).plusHours(1).equals(oldDatesNegative.get(i))) { LocalDateTime startDate = oldDatesNegative.get(i - 1); LocalDateTime endDate = oldDatesNegative.get(i); while (!startDate.equals(endDate)) { startDate = startDate.plusHours(1); if (startDate.equals(endDate)) { newDatesNegative.add(oldDatesNegative.get(i)); newCountsNegative.add(data.getNegativeCounts().getCounts().get(i)); } else { newCountsNegative.add(0); newDatesNegative.add(startDate); } } } else { newDatesNegative.add(oldDatesNegative.get(i)); newCountsNegative.add(data.getNegativeCounts().getCounts().get(i)); } } } // Fill negative with zeros when only positive exists if (!newDatesPositive.isEmpty() && newDatesNegative.isEmpty()) { for (LocalDateTime curDate : newDatesPositive) { newCountsNegative.add(0); newDatesNegative.add(curDate); } } // Fill positive with zeros when only negative exists else if (newDatesPositive.isEmpty() && !newDatesNegative.isEmpty()) { for (LocalDateTime curDate : newDatesNegative) { newCountsPositive.add(0); newDatesPositive.add(curDate); } } // Get last date from both (positive or negative) and fill the other // one with trailing zero counts if (!newDatesPositive.isEmpty() && !newDatesNegative.isEmpty()) { // The last positive date is later than the last negative date if (newDatesPositive.get(newDatesPositive.size() - 1) .compareTo(newDatesNegative.get(newDatesNegative.size() - 1)) == -1) { LocalDateTime curDate = newDatesPositive.get(newDatesPositive.size() - 1); while (!curDate.equals(newDatesNegative.get(newDatesNegative.size() - 1))) { newCountsNegative.add(0); newDatesNegative.add(curDate); curDate = curDate.plusHours(1); } } // The last negative date is later than the last positive date else if (newDatesPositive.get(newDatesPositive.size() - 1) .compareTo(newDatesNegative.get(newDatesNegative.size() - 1)) == 1) { LocalDateTime curDate = newDatesNegative.get(newDatesNegative.size() - 1); while (!curDate.equals(newDatesPositive.get(newDatesPositive.size() - 1))) { newCountsPositive.add(0); newDatesPositive.add(curDate); curDate = curDate.plusHours(1); } } } data.getPositiveCounts().setCounts(newCountsPositive); data.getPositiveCounts().setDates(newDatesPositive); data.getPositiveCounts().updateDateStrings(); data.getNegativeCounts().setCounts(newCountsNegative); data.getNegativeCounts().setDates(newDatesNegative); data.getNegativeCounts().updateDateStrings(); env.setData(data); return env; }
From source file:de.avanux.smartapplianceenabler.appliance.DayTimeframe.java
License:Open Source License
protected Interval buildMidnightAdjustedInterval(LocalDateTime now) { if (start != null && end != null) { LocalDateTime earliestStartDateTime = new LocalDate(now).toLocalDateTime(start.toLocalTime()); LocalDateTime latestEndDateTime = new LocalDate(now).toLocalDateTime(end.toLocalTime()); if (isOverMidnight(earliestStartDateTime, latestEndDateTime)) { if (now.toLocalTime().isAfter(start.toLocalTime())) { // before midnight latestEndDateTime = latestEndDateTime.plusHours(24); } else if (now.toLocalTime().isBefore(end.toLocalTime())) { // after midnight, before end earliestStartDateTime = earliestStartDateTime.minusHours(24); } else { // after midnight, after end latestEndDateTime = latestEndDateTime.plusHours(24); }// w ww .j av a 2 s . co m } return new Interval(earliestStartDateTime.toDateTime(), latestEndDateTime.toDateTime()) .withChronology(ISOChronology.getInstance()); } return null; }