List of usage examples for org.apache.commons.lang.time DateUtils addYears
public static Date addYears(Date date, int amount)
From source file:org.openbravo.test.costing.TestCosting.java
private void assertProductCosting(String productId, List<ProductCostingAssert> productCostingAssertList) { try {//from w w w.ja v a 2 s . c o m Product product = OBDal.getInstance().get(Product.class, productId); List<Costing> productCostingList = getProductCostings(productId); assertEquals(productCostingList.size(), productCostingAssertList.size()); List<ProductCostingAssert> productCostingAssertList2 = new ArrayList<ProductCostingAssert>( productCostingAssertList); Collections.reverse(productCostingAssertList2); int j = 0; for (ProductCostingAssert productCostingAssert : productCostingAssertList2) { if (productCostingAssert.getWarehouse().getId().equals(WAREHOUSE1_ID)) break; else j++; } int indexWarehouse1 = productCostingAssertList2.size() - 1 - j; j = 0; for (ProductCostingAssert productCostingAssert : productCostingAssertList2) { if (productCostingAssert.getWarehouse().getId().equals(WAREHOUSE2_ID)) break; else j++; } int indexWarehouse2 = productCostingAssertList2.size() - 1 - j; int i = 0; for (Costing productCosting : productCostingList) { ProductCostingAssert productCostingAssert = productCostingAssertList.get(i); assertGeneralData(productCosting); assertEquals(productCosting.getCost().setScale(4, BigDecimal.ROUND_HALF_UP), productCostingAssert.getFinalCost().setScale(4, BigDecimal.ROUND_HALF_UP)); assertEquals( productCosting.getPrice() == null ? null : productCosting.getPrice().setScale(4, BigDecimal.ROUND_HALF_UP), productCostingAssert.getPrice() == null ? null : productCostingAssert.getPrice().setScale(4, BigDecimal.ROUND_HALF_UP)); assertEquals( productCosting.getOriginalCost() == null ? null : productCosting.getOriginalCost().setScale(4, BigDecimal.ROUND_HALF_UP), productCostingAssert.getOriginalCost() == null ? null : productCostingAssert.getOriginalCost().setScale(4, BigDecimal.ROUND_HALF_UP)); assertEquals(productCosting.getTotalMovementQuantity(), productCostingAssert.getQuantity()); if (productCostingAssert.getQuantity() == null) assertEquals(productCosting.getQuantity(), null); else assertEquals(productCosting.getQuantity(), productCostingAssert.getTransaction().getMovementQuantity()); assertEquals(productCosting.isManual(), productCostingAssert.isManual()); assertEquals(productCosting.isPermanent(), !productCostingAssert.isManual()); assertEquals(productCosting.getGoodsShipmentLine(), null); assertEquals(productCosting.getInvoiceLine(), null); assertEquals(productCosting.getProductionLine(), null); assertFalse(productCosting.isProduction()); assertEquals(productCosting.getWarehouse(), productCostingAssert.getWarehouse()); assertEquals(productCosting.getInventoryTransaction(), productCostingAssert.getTransaction()); assertEquals(productCosting.getCurrency(), productCostingAssert.getTransaction() != null ? productCostingAssert.getTransaction().getCurrency() : OBDal.getInstance().get(Currency.class, CURRENCY1_ID)); assertEquals(productCosting.getCostType(), productCostingAssert.getType()); if (productCostingAssert.getYear() != 0) assertEquals(formatDate(productCosting.getStartingDate()), formatDate(DateUtils.addYears( product.getPricingProductPriceList().get(0).getPriceListVersion().getValidFromDate(), productCostingAssert.getYear()))); else assertEquals(formatDate(productCosting.getStartingDate()), formatDate(today)); if (productCostingAssert.getType().equals("STA") || i == indexWarehouse1 || i == indexWarehouse2) { Calendar calendar = Calendar.getInstance(); calendar.set(9999, 11, 31); assertEquals(formatDate(productCosting.getEndingDate()), formatDate(calendar.getTime())); } else { assertEquals(formatDate(productCosting.getEndingDate()), formatDate(productCostingList.get(i + 1).getStartingDate())); } i++; } } catch (Exception e) { throw new OBException(e); } }
From source file:org.openmrs.module.reporting.evaluation.EvaluationUtil.java
/** * This method will parse the passed expression and return a value based on the following * criteria:<br/>//from w w w. j a va 2 s . com * <ul> * <li>Any string that matches a passed parameter will be replaced by the value of that parameter * <li>If this date is followed by an expression, it will attempt to evaluate this by * incrementing/decrementing days/weeks/months/years as specified</li> * <li>Examples: Given 2 parameters: * <ul> * <li>report.startDate = java.util.Date with value of [2007-01-10] * <li>report.gender = "male" * </ul> * The following should result:<br/> * <br/> * <pre> * evaluateParameterExpression("report.startDate") -> "2007-01-10" as Date * <pre> * </ul> * * @param expression * @return value for given expression, as an <code>Object</code> * @throws org.openmrs.module.reporting.evaluation.parameter.ParameterException */ public static Object evaluateParameterExpression(String expression, Map<String, Object> parameters) throws ParameterException { log.debug("evaluateParameterExpression(): " + expression); log.debug("Starting expression: " + expression); String[] paramAndFormat = expression.split(FORMAT_SEPARATOR, 2); Object paramValueToFormat = null; try { Matcher matcher = expressionPattern.matcher(paramAndFormat[0]); if (matcher.matches()) { String parameterName = matcher.group(1); paramValueToFormat = parameters.get(parameterName); if (paramValueToFormat == null) { log.debug("Looked like an expression but the parameter value is null"); } else { String operations = matcher.group(2); Matcher opMatcher = operationPattern.matcher(operations); while (opMatcher.find()) { String op = opMatcher.group(1); String number = opMatcher.group(2); String unit = opMatcher.group(3).toLowerCase(); if (paramValueToFormat instanceof Date) { if (!op.matches("[+-]")) { throw new IllegalArgumentException("Dates only support the + and - operators"); } Integer numAsInt; try { numAsInt = Integer.parseInt(number); } catch (NumberFormatException ex) { throw new IllegalArgumentException( "Dates do not support arithmetic with floating-point values"); } if ("-".equals(op)) { numAsInt = -numAsInt; } if ("w".equals(unit)) { unit = "d"; numAsInt *= 7; } if ("ms".equals(unit)) { paramValueToFormat = DateUtils.addMilliseconds((Date) paramValueToFormat, numAsInt); } else if ("s".equals(unit)) { paramValueToFormat = DateUtils.addSeconds((Date) paramValueToFormat, numAsInt); } else if ("h".equals(unit)) { paramValueToFormat = DateUtils.addHours((Date) paramValueToFormat, numAsInt); } else if ("m".equals(unit)) { paramValueToFormat = DateUtils.addMonths((Date) paramValueToFormat, numAsInt); } else if ("y".equals(unit)) { paramValueToFormat = DateUtils.addYears((Date) paramValueToFormat, numAsInt); } else if ("".equals(unit) || "d".equals(unit)) { paramValueToFormat = DateUtils.addDays((Date) paramValueToFormat, numAsInt); } else { throw new IllegalArgumentException("Unknown unit: " + unit); } } else { // assume it's a number if (!"".equals(unit)) { throw new IllegalArgumentException("Can't specify units in a non-date expression"); } if (paramValueToFormat instanceof Integer && number.matches("\\d+")) { Integer parsed = Integer.parseInt(number); if ("+".equals(op)) { paramValueToFormat = ((Integer) paramValueToFormat) + parsed; } else if ("-".equals(op)) { paramValueToFormat = ((Integer) paramValueToFormat) - parsed; } else if ("*".equals(op)) { paramValueToFormat = ((Integer) paramValueToFormat) * parsed; } else if ("/".equals(op)) { paramValueToFormat = ((Integer) paramValueToFormat) / parsed; } else { throw new IllegalArgumentException("Unknown operator " + op); } } else { // since one or both are decimal values, do double arithmetic Double parsed = Double.parseDouble(number); if ("+".equals(op)) { paramValueToFormat = ((Number) paramValueToFormat).doubleValue() + parsed; } else if ("-".equals(op)) { paramValueToFormat = ((Number) paramValueToFormat).doubleValue() - parsed; } else if ("*".equals(op)) { paramValueToFormat = ((Number) paramValueToFormat).doubleValue() * parsed; } else if ("/".equals(op)) { paramValueToFormat = ((Number) paramValueToFormat).doubleValue() / parsed; } else { throw new IllegalArgumentException("Unknown operator " + op); } } } } } } } catch (Exception e) { log.debug(e.getMessage()); throw new ParameterException("Error handling expression: " + paramAndFormat[0], e); } paramValueToFormat = ObjectUtil.nvl(paramValueToFormat, parameters.get(paramAndFormat[0])); if (ObjectUtil.isNull(paramValueToFormat)) { if (parameters.containsKey(paramAndFormat[0])) { return paramValueToFormat; } else { return expression; } } log.debug("Evaluated to: " + paramValueToFormat); // Attempt to format the evaluated value if appropriate if (paramAndFormat.length == 2) { paramValueToFormat = ObjectUtil.format(paramValueToFormat, paramAndFormat[1]); } return paramValueToFormat; }
From source file:org.silverpeas.calendar.CalendarViewContext.java
/** * Centralization.//from w w w. j a va 2s .c om * @param offset */ private void moveReferenceDate(int offset) { switch (viewType) { case YEARLY: setReferenceDay(DateUtils.addYears(referenceDay.getDate(), offset), offset); break; case MONTHLY: setReferenceDay(DateUtils.addMonths(referenceDay.getDate(), offset), offset); break; case WEEKLY: setReferenceDay(DateUtils.addWeeks(referenceDay.getDate(), offset), offset); break; case DAILY: setReferenceDay(DateUtils.addDays(referenceDay.getDate(), offset), offset); break; } }
From source file:ubic.gemma.util.DateUtil.java
/** * Turn a string like '-7d' into the date equivalent to "seven days ago". Supports 'd' for day, 'h' for hour, 'm' * for minutes, "M" for months and "y" for years. Start with a '-' to indicate times in the past ('+' is not * necessary for future). Values must be integers. * /*from w w w . j a va2 s. com*/ * @param date to be added/subtracted to * @param dateString * @author Paul Pavlidis * @return Date relative to 'now' as modified by the input date string. */ public static Date getRelativeDate(Date date, String dateString) { if (date == null) throw new IllegalArgumentException("Null date"); Pattern pat = Pattern.compile("([+-]?[0-9]+)([dmhMy])"); Matcher match = pat.matcher(dateString); boolean matches = match.matches(); if (!matches) { throw new IllegalArgumentException( "Couldn't make sense of " + dateString + ", please use something like -7d or -8h"); } int amount = Integer.parseInt(match.group(1).replace("+", "")); String unit = match.group(2); if (unit.equals("h")) { return DateUtils.addHours(date, amount); } else if (unit.equals("m")) { return DateUtils.addMinutes(date, amount); } else if (unit.equals("d")) { return DateUtils.addDays(date, amount); } else if (unit.equals("y")) { return DateUtils.addYears(date, amount); } else if (unit.equals("M")) { return DateUtils.addMonths(date, amount); } else { throw new IllegalArgumentException( "Couldn't make sense of units in " + dateString + ", please use something like -7d or -8h"); } }