Example usage for org.joda.time DateTime plusYears

List of usage examples for org.joda.time DateTime plusYears

Introduction

In this page you can find the example usage for org.joda.time DateTime plusYears.

Prototype

public DateTime plusYears(int years) 

Source Link

Document

Returns a copy of this datetime plus the specified number of years.

Usage

From source file:app.sunstreak.yourpisd.util.DateHelper.java

License:Open Source License

public static String daysRelative(DateTime dt) {
    while (dt.isBefore(startOfSchoolYear))
        dt = dt.plusYears(1);

    // if today//from   ww w.  j a  v  a  2 s .  c o  m
    if (dt.toLocalDate().isEqual(new LocalDate()))
        return "(today)";

    Period pd;
    if (dt.isBeforeNow())
        pd = new Interval(dt, new LocalDate().toDateTimeAtStartOfDay()).toPeriod();
    else
        pd = new Interval(new LocalDate().toDateTimeAtStartOfDay(), dt).toPeriod();
    StringBuilder sb = new StringBuilder("\n(");

    int compare = dt.compareTo(new DateTime());

    sb.append(pf.print(pd));
    // Compare to now.
    if (dt.isBeforeNow())
        sb.append(" ago)");
    else
        sb.append(" from now)");
    return sb.toString();
}

From source file:com.cisco.dvbu.ps.utils.date.DateAddDate.java

License:Open Source License

/**
 * Called to invoke the stored procedure.  Will only be called a
 * single time per instance.  Can throw CustomProcedureException or
 * SQLException if there is an error during invoke.
 *//*from   w w  w .  j  a va  2s . c  om*/
@Override
public void invoke(Object[] inputValues) throws CustomProcedureException, SQLException {
    java.util.Date startDate = null;
    Calendar startCal = null;
    DateTime startDateTime = null;
    DateTime endDateTime = null;
    String datePart = null;
    int dateLength = 0;

    try {
        result = null;
        if (inputValues[0] == null || inputValues[1] == null || inputValues[2] == null) {
            return;
        }

        datePart = (String) inputValues[0];
        dateLength = (Integer) inputValues[1];

        startDate = (java.util.Date) inputValues[2];
        startCal = Calendar.getInstance();
        startCal.setTime(startDate);
        startDateTime = new DateTime(startCal.get(Calendar.YEAR), startCal.get(Calendar.MONTH) + 1,
                startCal.get(Calendar.DAY_OF_MONTH), 0, 0, 0, 0);

        if (datePart.equalsIgnoreCase("second")) {
            endDateTime = startDateTime.plusSeconds(dateLength);
        }

        if (datePart.equalsIgnoreCase("minute")) {
            endDateTime = startDateTime.plusMinutes(dateLength);
        }

        if (datePart.equalsIgnoreCase("hour")) {
            endDateTime = startDateTime.plusHours(dateLength);
        }

        if (datePart.equalsIgnoreCase("day")) {
            endDateTime = startDateTime.plusDays(dateLength);
        }

        if (datePart.equalsIgnoreCase("week")) {
            endDateTime = startDateTime.plusWeeks(dateLength);
        }

        if (datePart.equalsIgnoreCase("month")) {
            endDateTime = startDateTime.plusMonths(dateLength);
        }

        if (datePart.equalsIgnoreCase("year")) {
            endDateTime = startDateTime.plusYears(dateLength);
        }

        result = new java.sql.Date(endDateTime.getMillis());
    } catch (Throwable t) {
        throw new CustomProcedureException(t);
    }
}

From source file:com.cisco.dvbu.ps.utils.date.DateAddTimestamp.java

License:Open Source License

/**
 * Called to invoke the stored procedure.  Will only be called a
 * single time per instance.  Can throw CustomProcedureException or
 * SQLException if there is an error during invoke.
 *//* www. java 2  s.  c  om*/
@Override
public void invoke(Object[] inputValues) throws CustomProcedureException, SQLException {
    Timestamp startDate = null;
    Calendar startCal = null;
    DateTime startDateTime = null;
    DateTime endDateTime = null;
    String datePart = null;
    int dateLength = 0;

    try {
        result = null;
        if (inputValues[0] == null || inputValues[1] == null || inputValues[2] == null) {
            return;
        }

        datePart = (String) inputValues[0];
        dateLength = (Integer) inputValues[1];

        startDate = (Timestamp) inputValues[2];
        startCal = Calendar.getInstance();
        startCal.setTime(startDate);
        startDateTime = new DateTime(startCal.get(Calendar.YEAR), startCal.get(Calendar.MONTH) + 1,
                startCal.get(Calendar.DAY_OF_MONTH), startCal.get(Calendar.HOUR_OF_DAY),
                startCal.get(Calendar.MINUTE), startCal.get(Calendar.SECOND),
                startCal.get(Calendar.MILLISECOND));

        if (datePart.equalsIgnoreCase("second")) {
            endDateTime = startDateTime.plusSeconds(dateLength);
        }

        if (datePart.equalsIgnoreCase("minute")) {
            endDateTime = startDateTime.plusMinutes(dateLength);
        }

        if (datePart.equalsIgnoreCase("hour")) {
            endDateTime = startDateTime.plusHours(dateLength);
        }

        if (datePart.equalsIgnoreCase("day")) {
            endDateTime = startDateTime.plusDays(dateLength);
        }

        if (datePart.equalsIgnoreCase("week")) {
            endDateTime = startDateTime.plusWeeks(dateLength);
        }

        if (datePart.equalsIgnoreCase("month")) {
            endDateTime = startDateTime.plusMonths(dateLength);
        }

        if (datePart.equalsIgnoreCase("year")) {
            endDateTime = startDateTime.plusYears(dateLength);
        }

        result = new Timestamp(endDateTime.toDate().getTime());
    } catch (Throwable t) {
        throw new CustomProcedureException(t);
    }
}

From source file:com.cloudhopper.commons.util.time.DateTimePeriod.java

License:Apache License

/**
 * Converts this period to a list of year periods.  Partial years will not be
 * included.  For example, a period of "2009-2010" will return a list
 * of 2 years - one for 2009 and one for 2010.  On the other hand, a period
 * of "January 2009" would return an empty list since partial
 * years are not included.//from  www  .  j a v  a 2 s. c om
 * @return A list of year periods contained within this period
 */
public List<DateTimePeriod> toYears() {
    ArrayList<DateTimePeriod> list = new ArrayList<DateTimePeriod>();

    // default "current" year to start datetime
    DateTime currentStart = getStart();
    // calculate "next" year
    DateTime nextStart = currentStart.plusYears(1);
    // continue adding until we've reached the end
    while (nextStart.isBefore(getEnd()) || nextStart.isEqual(getEnd())) {
        // its okay to add the current
        list.add(new DateTimeYear(currentStart, nextStart));
        // increment both
        currentStart = nextStart;
        nextStart = currentStart.plusYears(1);
    }

    return list;
}

From source file:com.cloudhopper.commons.util.time.DateTimePeriod.java

License:Apache License

static public DateTimePeriod createYear(DateTime start) {
    DateTime end = start.plusYears(1);
    return new DateTimeYear(start, end);
}

From source file:com.cronutils.model.time.ExecutionTime.java

License:Apache License

/**
 * If date is not match, will return next closest match.
 * If date is match, will return this date.
 * @param date - reference DateTime instance - never null;
 * @return DateTime instance, never null. Value obeys logic specified above.
 * @throws NoSuchValueException// www  .  ja  v  a 2s .co  m
 */
DateTime nextClosestMatch(DateTime date) throws NoSuchValueException {
    List<Integer> year = yearsValueGenerator.generateCandidates(date.getYear(), date.getYear());
    TimeNode days = null;
    int lowestMonth = months.getValues().get(0);
    int lowestHour = hours.getValues().get(0);
    int lowestMinute = minutes.getValues().get(0);
    int lowestSecond = seconds.getValues().get(0);

    NearestValue nearestValue;
    DateTime newDate;
    if (year.isEmpty()) {
        int newYear = yearsValueGenerator.generateNextValue(date.getYear());
        days = generateDays(cronDefinition, new DateTime(newYear, lowestMonth, 1, 0, 0));
        return initDateTime(yearsValueGenerator.generateNextValue(date.getYear()), lowestMonth,
                days.getValues().get(0), lowestHour, lowestMinute, lowestSecond, date.getZone());
    }
    if (!months.getValues().contains(date.getMonthOfYear())) {
        nearestValue = months.getNextValue(date.getMonthOfYear(), 0);
        int nextMonths = nearestValue.getValue();
        if (nearestValue.getShifts() > 0) {
            newDate = new DateTime(date.getYear(), 1, 1, 0, 0, 0, date.getZone())
                    .plusYears(nearestValue.getShifts());
            return nextClosestMatch(newDate);
        }
        if (nearestValue.getValue() < date.getMonthOfYear()) {
            date = date.plusYears(1);
        }
        days = generateDays(cronDefinition, new DateTime(date.getYear(), nextMonths, 1, 0, 0));
        return initDateTime(date.getYear(), nextMonths, days.getValues().get(0), lowestHour, lowestMinute,
                lowestSecond, date.getZone());
    }
    days = generateDays(cronDefinition, date);
    if (!days.getValues().contains(date.getDayOfMonth())) {
        nearestValue = days.getNextValue(date.getDayOfMonth(), 0);
        if (nearestValue.getShifts() > 0) {
            newDate = new DateTime(date.getYear(), date.getMonthOfYear(), 1, 0, 0, 0, date.getZone())
                    .plusMonths(nearestValue.getShifts());
            return nextClosestMatch(newDate);
        }
        if (nearestValue.getValue() < date.getDayOfMonth()) {
            date = date.plusMonths(1);
        }
        return initDateTime(date.getYear(), date.getMonthOfYear(), nearestValue.getValue(), lowestHour,
                lowestMinute, lowestSecond, date.getZone());
    }
    if (!hours.getValues().contains(date.getHourOfDay())) {
        nearestValue = hours.getNextValue(date.getHourOfDay(), 0);
        int nextHours = nearestValue.getValue();
        if (nearestValue.getShifts() > 0) {
            newDate = new DateTime(date.getYear(), date.getMonthOfYear(), date.getDayOfMonth(), 0, 0, 0,
                    date.getZone()).plusDays(nearestValue.getShifts());
            return nextClosestMatch(newDate);
        }
        if (nearestValue.getValue() < date.getHourOfDay()) {
            date = date.plusDays(1);
        }
        return initDateTime(date.getYear(), date.getMonthOfYear(), date.getDayOfMonth(), nextHours,
                lowestMinute, lowestSecond, date.getZone());
    }
    if (!minutes.getValues().contains(date.getMinuteOfHour())) {
        nearestValue = minutes.getNextValue(date.getMinuteOfHour(), 0);
        int nextMinutes = nearestValue.getValue();
        if (nearestValue.getShifts() > 0) {
            newDate = new DateTime(date.getYear(), date.getMonthOfYear(), date.getDayOfMonth(),
                    date.getHourOfDay(), 0, 0, date.getZone()).plusHours(nearestValue.getShifts());
            return nextClosestMatch(newDate);
        }
        if (nearestValue.getValue() < date.getMinuteOfHour()) {
            date = date.plusHours(1);
        }
        return initDateTime(date.getYear(), date.getMonthOfYear(), date.getDayOfMonth(), date.getHourOfDay(),
                nextMinutes, lowestSecond, date.getZone());
    }
    if (!seconds.getValues().contains(date.getSecondOfMinute())) {
        nearestValue = seconds.getNextValue(date.getSecondOfMinute(), 0);
        int nextSeconds = nearestValue.getValue();
        if (nearestValue.getShifts() > 0) {
            newDate = new DateTime(date.getYear(), date.getMonthOfYear(), date.getDayOfMonth(),
                    date.getHourOfDay(), date.getMinuteOfHour(), 0, date.getZone())
                            .plusMinutes(nearestValue.getShifts());
            return nextClosestMatch(newDate);
        }
        if (nearestValue.getValue() < date.getSecondOfMinute()) {
            date = date.plusMinutes(1);
        }
        return initDateTime(date.getYear(), date.getMonthOfYear(), date.getDayOfMonth(), date.getHourOfDay(),
                date.getMinuteOfHour(), nextSeconds, date.getZone());
    }
    return date;
}

From source file:com.dz.module.vehicle.newcheck.CheckService.java

public List<CheckRecord> selecctCheckRecordsByTimePass(Date startTime, Date endTime) {
    if (startTime == null) {
        DateTime dt = new DateTime();
        startTime = dt.minusYears(100).toDate();
    }/*ww w  . ja va2  s .  c o  m*/
    if (endTime == null) {
        DateTime dt = new DateTime();
        endTime = dt.plusYears(100).toDate();
    }
    List<CheckRecord> crs = checkRecordDaoImp.selectRecordsByTimePass(startTime, endTime);
    for (CheckRecord each : crs) {
        if (each.getDriver() != null) {
            Driver d = (Driver) ObjectAccess.getObject("com.dz.module.driver.Driver", each.getDriver());
            each.setRenter(d == null ? "" : d.getName());
        }
    }
    return crs;
}

From source file:com.dz.module.vehicle.newcheck.CheckService.java

public Map<String, List<TJMessage>> tonji(Date startTime, Date endTime) {
    if (startTime == null) {
        DateTime dt = new DateTime();
        startTime = dt.minusYears(100).toDate();
    }/*from   w w  w.  ja  v  a  2  s  .  c o  m*/
    if (endTime == null) {
        DateTime dt = new DateTime();
        endTime = dt.plusYears(100).toDate();
    }
    Map<String, List<TJMessage>> maps = new HashMap<>();
    maps.put("checked", new ArrayList<TJMessage>());
    maps.put("unchecked", new ArrayList<TJMessage>());
    List<Vehicle> allVehicle = ObjectAccess.query(Vehicle.class, "");
    List<CheckRecord> checkRecords = checkRecordDaoImp.selectRecordsByTimePass(startTime, endTime);
    List<String> vehicleIds = new ArrayList<String>();
    for (CheckRecord each : checkRecords) {
        vehicleIds.add(each.getCarFrameNum());
    }
    for (Vehicle each : allVehicle) {
        if (each.getDriverId() != null) {
            String carFrameNum = each.getCarframeNum();
            List<TJMessage> tjms = null;
            if (vehicleIds.contains(carFrameNum)) {
                tjms = maps.get("checked");
                System.out.println("checked");
            } else
                tjms = maps.get("unchecked");
            TJMessage tjm = new TJMessage();
            tjm.setLicenseNUm(each.getLicenseNum());
            Driver driver = (Driver) ObjectAccess.getObject("com.dz.module.driver.Driver", each.getDriverId());
            tjm.setRenter(driver != null ? driver.getName() : "");
            tjm.setDept(each.getDept());
            tjm.setTelephone(driver != null ? driver.getPhoneNum1() : "");
            tjms.add(tjm);
        }
    }
    return maps;
}

From source file:com.goodhuddle.huddle.service.impl.PaymentServiceImpl.java

License:Open Source License

private Payment postProcessPayment(Payment payment) throws CardException, APIException, AuthenticationException,
        InvalidRequestException, APIConnectionException {

    // get the fee information
    PaymentSettings settings = getPaymentSettings();
    BalanceTransaction transaction = BalanceTransaction.retrieve(payment.getStripeBalanceTransactionId(),
            settings.getSecretKey());/* w w  w  . j  ava  2  s . com*/
    payment.setFeesInCents(transaction.getFee());
    paymentRepository.save(payment);

    if (payment.getSubscription() != null) {
        Subscription subscription = payment.getSubscription();
        DateTime nextPaymentDue = new DateTime(payment.getPaidOn());
        switch (subscription.getFrequency()) {
        case week:
            nextPaymentDue = nextPaymentDue.plusWeeks(1);
            break;
        case month:
            nextPaymentDue = nextPaymentDue.plusMonths(1);
            break;
        case year:
            nextPaymentDue = nextPaymentDue.plusYears(1);
            break;
        default:
            throw new IllegalArgumentException("Unsupported frequency: " + subscription.getFrequency());
        }
        subscription.setNextPaymentDue(nextPaymentDue);
        subscriptionRepository.save(subscription);
    }

    return payment;
}

From source file:com.huffingtonpost.chronos.util.CronExpression.java

License:Apache License

public DateTime nextTimeAfter(DateTime afterTime) {
    // will search for the next time within the next 4 years. If there is no
    // time matching, an InvalidArgumentException will be thrown (it is very
    // likely that the cron expression is invalid, like the February 30th).
    return nextTimeAfter(afterTime, afterTime.plusYears(4));
}