Example usage for org.joda.time Seconds secondsBetween

List of usage examples for org.joda.time Seconds secondsBetween

Introduction

In this page you can find the example usage for org.joda.time Seconds secondsBetween.

Prototype

public static Seconds secondsBetween(ReadablePartial start, ReadablePartial end) 

Source Link

Document

Creates a Seconds representing the number of whole seconds between the two specified partial datetimes.

Usage

From source file:org.jraf.irondad.handler.countdown.CountDownHandler.java

License:Open Source License

private String getReply(String eventDateStr) {
    DateTime eventDateTime = DateTime.parse(eventDateStr);
    DateTime nowDateTime = DateTime.now();

    String res = null;//from  w ww. j av a 2s . c o m

    int nbDays = Days.daysBetween(nowDateTime.toLocalDateTime().toDateTime().withTimeAtStartOfDay(),
            eventDateTime.toLocalDateTime().toDateTime().withTimeAtStartOfDay()).getDays();
    if (nbDays > 2) {
        res = "Dans " + nbDays + " jours !";
    } else if (nbDays == 2) {
        res = "APRS-DEMAIN !!!";
    } else if (nbDays == 1) {
        res = "DEMAIN !!!!!!";
    } else if (nbDays == 0) {
        int nbHours = Hours.hoursBetween(nowDateTime, eventDateTime).getHours();
        if (nbHours > 0) {
            if (nbHours == 1)
                res = "Dans " + nbHours + " heure (et quelques) !!";
            else
                res = "Dans " + nbHours + " heures (et quelques) !";
        } else if (nbHours < 0) {
            res = "C'est en ce moment.";
        } else {
            int nbMinutes = Minutes.minutesBetween(nowDateTime, eventDateTime).getMinutes();
            if (nbMinutes > 0) {
                if (nbMinutes == 1)
                    res = "Dans 1 minute !!!";
                else
                    res = "Dans " + nbMinutes + " minutes !!";
            } else if (nbMinutes < 0) {
                res = "C'est en ce moment mme !!!!!";
            } else {
                int nbSeconds = Seconds.secondsBetween(nowDateTime, eventDateTime).getSeconds();
                if (nbSeconds > 0)
                    res = "Dans " + nbSeconds + " secondes !!!!";
                else if (nbSeconds < 0)
                    res = "a commence !!!!!";
                else
                    res = "C'est commenc !!!!!";
            }
        }
    } else if (nbDays == -1) {
        res = "C'est en ce moment...";
    } else {
        res = "C'est fini :(";
    }
    return res;
}

From source file:org.libreplan.business.workreports.entities.WorkReportLine.java

License:Open Source License

private EffortDuration getDiferenceBetweenTimeStartAndFinish() {
    return (clockStart != null) && (clockFinish != null)
            ? EffortDuration.seconds(Seconds.secondsBetween(clockStart, clockFinish).getSeconds())
            : null;/*w w  w.j av a2  s.c  o m*/
}

From source file:org.lightjason.agentspeak.action.buildin.datetime.CSecondsBetween.java

License:LGPL

@Override
protected final Stream<?> apply(final Stream<List<Instant>> p_datetime) {
    return p_datetime.map(i -> Seconds.secondsBetween(i.get(0), i.get(1))).mapToLong(Seconds::getSeconds)
            .boxed();//from www.  j  a va2 s.  com
}

From source file:org.lightjason.agentspeak.action.builtin.datetime.CSecondsBetween.java

License:LGPL

@Nonnull
@Override//from  ww w .  j av  a 2 s  .co m
protected final Stream<?> apply(@Nonnull final Stream<List<Instant>> p_datetime) {
    return p_datetime.map(i -> Seconds.secondsBetween(i.get(0), i.get(1))).mapToDouble(Seconds::getSeconds)
            .boxed();
}

From source file:org.multibit.utils.CSMiscUtils.java

License:MIT License

public static BigDecimal getDisplayUnitsForRawUnits(CSAsset asset, BigInteger rawQuantity) {
    if (asset == null)
        return BigDecimal.ZERO;
    CoinSparkGenesis genesis = asset.getGenesis();
    if (genesis == null)
        return BigDecimal.ZERO; // This can happen with brand new Manually transferred asset which has not yet been validated.
    int chargeBasisPoints = genesis.getChargeBasisPoints();
    int chargeExponent = genesis.getChargeFlatExponent();
    int chargeMantissa = genesis.getChargeFlatMantissa();
    int qtyExponent = genesis.getQtyExponent();
    int qtyMantissa = genesis.getQtyMantissa();

    double interestRate = asset.getInterestRate();
    Date issueDate = asset.getIssueDate();

    BigDecimal result = new BigDecimal(rawQuantity.toString());

    //System.out.println("interest rate = " + interestRate);
    //System.out.println("issue date = " + issueDate);

    //System.out.println("raw units =" + result);

    // 1. Compute interest
    if (issueDate != null && interestRate != 0.0) {

        BigDecimal rate = new BigDecimal(String.valueOf(interestRate));
        rate = rate.divide(new BigDecimal(100));
        rate = rate.add(BigDecimal.ONE);
        //interestRate = interestRate / 100;

        //System.out.println("interest rate 1 + ir/100 = " + rate.toPlainString());

        // get years elapsed
        DateTime d1 = new DateTime(issueDate);
        DateTime d2 = new DateTime();

        //System.out.println("Issue: " + d1 + "   Now: " + d2);
        int seconds = Math.abs(Seconds.secondsBetween(d1, d2).getSeconds());

        //System.out.println("...Number of seconds difference: " + seconds);

        BigDecimal elapsedSeconds = new BigDecimal(seconds);

        //System.out.println("...Number of seconds difference: " + elapsedSeconds.toPlainString());

        // To avoid exception, we need to set a precision.
        // java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
        // http://stackoverflow.com/questions/4591206/arithmeticexception-non-terminating-decimal-expansion-no-exact-representable
        BigDecimal elapsedYears = elapsedSeconds.divide(new BigDecimal(COINSPARK_SECONDS_IN_YEAR),
                MathContext.DECIMAL32);
        //System.out.println("...Number of years difference: " + elapsedYears.toPlainString());

        double base = elapsedSeconds.doubleValue();
        double exp = elapsedYears.doubleValue();
        //System.out.println("...base=" + base + "  exponent=" + exp);
        double interestMultipler = Math.pow(rate.doubleValue(), elapsedYears.doubleValue());

        //System.out.println("interest multipler =" + interestMultipler);

        result = result.multiply(new BigDecimal(interestMultipler));

        //System.out.println("raw units with interest multiplier =" + result);

        result = result.setScale(0, RoundingMode.DOWN);

        //System.out.println("raw units with interest multiplier, floored =" + result);

    }//from w w  w  . ja  v  a 2  s . c om

    // 2. Apply multiple
    int decimalPlaces = CSMiscUtils.getNumberOfDisplayDecimalPlaces(asset);
    BigDecimal display = result;
    if (decimalPlaces != 0) {
        //       System.out.println(">>>>> display = " + display.toPlainString());
        display = result.movePointLeft(decimalPlaces);
        //       System.out.println(">>>>> display = " + display.toPlainString());
    }

    //long qty = Utils.mantissaExponentToQty(qtyMantissa, qtyExponent);
    //   double multiple = asset.getMultiple();
    // let's just do it for now to make sure code is corret   
    //if (multiple != 1.0)
    //   BigDecimal m = new BigDecimal(String.valueOf(multiple));
    //   BigDecimal display = result.multiply(m);

    //System.out.println("multiplier=" + m + ", display=" + display);
    // Stripping zeros from internal zero with different scale does not work, so use 
    // JDK bug still seems to exist
    // http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6480539
    // http://stackoverflow.com/questions/5239137/clarification-on-behavior-of-bigdecimal-striptrailingzeroes
    int cmpZeroResult = display.compareTo(BigDecimal.ZERO);
    if (decimalPlaces == 0) {
        display = display.stripTrailingZeros();
    }

    // Stripping trailing zeros from internal zero with different scale does not work, so set to ZERO instead.
    if (0 == cmpZeroResult) {
        display = BigDecimal.ZERO;
    }
    return display;
}

From source file:org.multibit.utils.CSMiscUtils.java

License:MIT License

public static BigInteger getRawUnitsFromDisplayString(CSAsset asset, String display) {
    BigDecimal result = null;//  w ww  .  j av a2 s. co m
    try {
        //System.out.println("Start to get raw units from: " + display);
        result = new BigDecimal(display);
    } catch (NumberFormatException nfe) {
        nfe.printStackTrace();
        return null;
    }

    // Reverse apply the multiple
    int decimalPlaces = CSMiscUtils.getNumberOfDisplayDecimalPlaces(asset);
    if (decimalPlaces != 0) {
        result = result.movePointRight(decimalPlaces);
    }
    // FIXME: what if multiple is 0.0? ignore? error?
    //   double multiple = asset.getMultiple();
    //   BigDecimal m = new BigDecimal(String.valueOf(multiple));
    //   result = result.divide(m, MathContext.DECIMAL32);

    //System.out.println("multiplier=" + m + ", removed multiplier =" + display);

    double interestRate = asset.getInterestRate();

    BigDecimal rate = new BigDecimal(String.valueOf(interestRate));
    rate = rate.divide(new BigDecimal(100));
    rate = rate.add(BigDecimal.ONE);

    Date issueDate = asset.getIssueDate();
    DateTime d1 = new DateTime(issueDate);
    DateTime d2 = new DateTime();
    int seconds = Math.abs(Seconds.secondsBetween(d1, d2).getSeconds());

    //System.out.println("...Number of seconds difference: " + seconds);

    BigDecimal elapsedSeconds = new BigDecimal(seconds);
    BigDecimal elapsedYears = elapsedSeconds.divide(new BigDecimal(COINSPARK_SECONDS_IN_YEAR),
            MathContext.DECIMAL32);
    //System.out.println("...Number of years difference: " + elapsedYears.toPlainString());

    double base = elapsedSeconds.doubleValue();
    double exp = elapsedYears.doubleValue();
    //System.out.println("...base=" + base + "  exponent=" + exp);
    double interestMultipler = Math.pow(rate.doubleValue(), elapsedYears.doubleValue());

    //System.out.println("interest multipler =" + interestMultipler);

    result = result.divide(new BigDecimal(String.valueOf(interestMultipler)), MathContext.DECIMAL32);

    //System.out.println("result = " + result.toPlainString());

    result = result.setScale(0, RoundingMode.DOWN);
    result = result.stripTrailingZeros();

    //System.out.println("result floored = " + result.toPlainString());

    String resultString = result.toPlainString();
    return new BigInteger(resultString);
}

From source file:org.ojbc.bundles.adapters.staticmock.samplegen.AbstractSampleGenerator.java

License:RPL License

/**
 * Generates a random, uniformly distributed time between the two specified date/times.
 * // www .ja va 2  s . c  om
 * @param d1
 *            the first datetime
 * @param d2
 *            the second datetime
 * @return a random datetime between the two
 */
protected final DateTime generateUniformRandomTimeBetween(DateTime d1, DateTime d2) {
    DateTime start = d1;
    DateTime end = d2;
    if (d1.isAfter(d2)) {
        start = d2;
        end = d1;
    }
    int secondsBetween = Seconds.secondsBetween(start, end).getSeconds();
    int r = randomGenerator.nextInt(0, secondsBetween);
    return start.plusSeconds(r);
}

From source file:org.onebusaway.admin.search.impl.TimeWindowFilter.java

License:Apache License

private BigDecimal getTimeDifference(String timeReported) {
    DateTimeFormatter formatter = ISODateTimeFormat.dateTime();
    DateTime lastReportedTime = formatter.parseDateTime(timeReported);
    DateTime now = new DateTime();
    int seconds = Seconds.secondsBetween(lastReportedTime, now).getSeconds();
    BigDecimal difference = new BigDecimal(seconds);
    return difference;
}

From source file:org.openmrs.module.pihmalawi.reporting.definition.data.converter.DurationConverter.java

License:Open Source License

/**
 * @see DataConverter#convert(Object)//  ww  w .  j  a  v  a2 s  .c  om
 */
public Object convert(Object original) {
    if (original != null) {
        DateTime from = new DateTime((Date) original);
        if (durationToDate == null) {
            durationToDate = new Date();
        }
        DateTime to = new DateTime(durationToDate);
        if (durationUnit == DurationUnit.YEARS) {
            return Years.yearsBetween(from, to).getYears();
        } else if (durationUnit == DurationUnit.MONTHS) {
            return Months.monthsBetween(from, to).getMonths();
        } else if (durationUnit == DurationUnit.WEEKS) {
            return Weeks.weeksBetween(from, to).getWeeks();
        } else if (durationUnit == DurationUnit.DAYS) {
            return Days.daysBetween(from, to).getDays();
        } else if (durationUnit == DurationUnit.HOURS) {
            return Hours.hoursBetween(from, to).getHours();
        } else if (durationUnit == DurationUnit.MINUTES) {
            return Minutes.minutesBetween(from, to).getMinutes();
        } else if (durationUnit == DurationUnit.SECONDS) {
            return Seconds.secondsBetween(from, to).getSeconds();
        } else {
            throw new IllegalArgumentException("Unable to convert with duration unit: " + durationUnit);
        }
    }
    return null;
}

From source file:org.superfuntime.chatty.chat.ChatListener.java

License:Open Source License

@Listener
public void chatMessageReceived(ChatReceivedEvent event) {
    ChatMessage message = event.getMessage();
    if (message.getType() != ChatMessage.Type.SAID)
        return; //Only said messages
    Seconds secDiff = Seconds.secondsBetween(new DateTime(message.getTime()),
            new DateTime(Calendar.getInstance().getTime()));
    if (secDiff.getSeconds() > 120)
        return; //Don't reply to old messages
    logger.trace("Received '{}' from '{}'", message.getContent(), message.getSender().getDisplayName());
    CommandManager.parse(message);/* w  w  w .j  a v a 2 s  .c o m*/
}