Example usage for org.joda.time Hours hoursBetween

List of usage examples for org.joda.time Hours hoursBetween

Introduction

In this page you can find the example usage for org.joda.time Hours hoursBetween.

Prototype

public static Hours hoursBetween(ReadablePartial start, ReadablePartial end) 

Source Link

Document

Creates a Hours representing the number of whole hours between the two specified partial datetimes.

Usage

From source file:AppPackage.timeDiff.java

public String timeDiff(String time1, String time2) {

    this.time1 = time1;
    this.time2 = time2;

    DateTimeFormatter format = DateTimeFormat.forPattern("HH:mm:ss");
    DateTime dt1 = format.parseDateTime(time1);
    DateTime dt2 = format.parseDateTime(time2);

    long hours = Hours.hoursBetween(dt1, dt2).getHours() % 24;
    long minutes = Minutes.minutesBetween(dt1, dt2).getMinutes() % 60;
    //                long seconds = Seconds.secondsBetween(dt1, dt2).getSeconds() % 60;
    String diffResult = String.format("%02d:%02d:00", hours, minutes);
    return diffResult;

}

From source file:ar.com.zir.utils.DateUtils.java

License:Open Source License

/**
 * Method that uses Joda Library to obtain the difference between two dates
 * in the specified date part/*from   ww  w. j  a  v  a2s . c  om*/
 * 
 * @param datePart the datePart to use in the calculation
 * @param date1 first Date object
 * @param date2 second Date object
 * @return the result from date1 - date2 or -1 if specified datePart is not supported
 * @see java.util.Calendar
 */
public static int dateDiff(int datePart, Date date1, Date date2) {
    Calendar cal1 = Calendar.getInstance();
    cal1.setTime(date1);
    Calendar cal2 = Calendar.getInstance();
    cal2.setTime(date2);
    switch (datePart) {
    case DATE_PART_SECOND:
        return Seconds
                .secondsBetween(new DateTime(cal2.getTimeInMillis()), new DateTime(cal1.getTimeInMillis()))
                .getSeconds();
    case DATE_PART_MINUTE:
        return Minutes
                .minutesBetween(new DateTime(cal2.getTimeInMillis()), new DateTime(cal1.getTimeInMillis()))
                .getMinutes();
    case DATE_PART_HOUR:
        return Hours.hoursBetween(new DateTime(cal2.getTimeInMillis()), new DateTime(cal1.getTimeInMillis()))
                .getHours();
    case DATE_PART_DAY:
        return Days.daysBetween(new DateTime(cal2.getTimeInMillis()), new DateTime(cal1.getTimeInMillis()))
                .getDays();
    case DATE_PART_MONTH:
        return Months.monthsBetween(new DateTime(cal2.getTimeInMillis()), new DateTime(cal1.getTimeInMillis()))
                .getMonths();
    case DATE_PART_YEAR:
        return Years.yearsBetween(new DateTime(cal2.getTimeInMillis()), new DateTime(cal1.getTimeInMillis()))
                .getYears();
    default:
        return -1;
    }

}

From source file:betalabs.libtests.unfolding.AnimatedTemporalDotsApp.java

License:Open Source License

public void drawEarthquakeDots(PVector pos, DateTime time) {
    fill(255, 0, 0, 100);//from   w ww. j  a  va 2 s. c o m
    stroke(255, 0, 0, 200);
    strokeWeight(1);

    // Size of circle depends on age of earthquake, with 12h = max (20px)
    int hours = Hours.hoursBetween(time, currentTime).getHours();
    float size = constrain(map(hours, 0, 12, 20, 0), 0, 20);
    scale(1);

    ellipse(pos.x, pos.y, size * 3, size * 3);
}

From source file:br.edu.utfpr.cm.JGitMinerWeb.services.matrix.auxiliary.AuxMeanReplyTime.java

public void setMean() {

    DateTime dataAnterior = null;/*from  w  w w  .j  a  v a2 s.  c o  m*/

    for (EntityComment comentario : getCommentsList()) {
        System.out.println(issue.getNumber() + " - hora: " + comentario.getCreatedAt());

        if ((dataAnterior == null) && (getCommentsList().size() > 0)) {
            dataAnterior = new DateTime(comentario.getCreatedAt());
        } else {
            mean += Hours.hoursBetween(new DateTime(comentario.getCreatedAt()), dataAnterior).getHours();

            System.out.println(issue.getNumber() + " - " + mean);
        }

    }
    System.out.println("soma: " + mean);
    mean = mean / getCommentsList().size();
    System.out.println("media: " + mean);
}

From source file:br.edu.utfpr.cm.JGitMinerWeb.services.matriz.auxiliary.AuxMeanReplyTime.java

public void setMean() {

    DateTime dataAnterior = null;//from   ww w  .  ja v a2  s  .  c  om

    for (EntityComment comentario : getCommentsList()) {
        System.out.println(issue.getNumber() + " - hora: " + comentario.getCreatedAt());

        if ((dataAnterior == null) && (getCommentsList().size() > 0)) {
            dataAnterior = new DateTime(comentario.getCreatedAt());
        } else {
            mean += Hours.hoursBetween(dataAnterior, new DateTime(comentario.getCreatedAt())).getHours();

            System.out.println(issue.getNumber() + " - " + mean);
        }

    }
    System.out.println("soma: " + mean);
    mean = mean / getCommentsList().size();
    System.out.println("media: " + mean);
}

From source file:br.edu.utfpr.cm.JGitMinerWeb.services.metric.social.TempoDeResposta.java

@Override
public void calculateMetric(AuxAllMetrics pair) {

    Comparator comparator = new Comparator() {
        @Override//from   w w w .  j  ava  2 s  . com
        public int compare(Object obj1, Object obj2) {
            EntityComment to1 = (EntityComment) obj1;
            EntityComment to2 = (EntityComment) obj2;

            return to1.getCreatedAt().compareTo(to2.getCreatedAt());
        }
    };

    for (EntityPullRequest pullRequest : pair.getPullRequests()) {

        DateTime dataAnterior = null;
        Integer mean = 0;
        // System.out.println("comments size: " + pullRequest.getIssue().getComments().size());
        try {
            List<EntityComment> comentarios = Lists.newArrayList(pullRequest.getIssue().getComments());
            Collections.sort(comentarios, comparator);

            for (EntityComment comment : comentarios) {

                if (comentarios.indexOf(comment) != 0) {
                    int anterior = comentarios.indexOf(comment) - 1;
                    dataAnterior = new DateTime(comentarios.get(anterior).getCreatedAt());
                    mean += Hours.hoursBetween(dataAnterior, new DateTime(comment.getCreatedAt())).getHours();
                }

            }
            if (comentarios.size() > 0) {
                mean = mean / comentarios.size();
            }

            result.put(pair.getId(), mean);

        } catch (Exception e) {
            System.out.println("Erro");
            System.out.println(e.toString());
        }

    }
}

From source file:ca.ualberta.physics.cssdp.domain.auth.Session.java

License:Apache License

public boolean hasExpired() {
    return Hours.hoursBetween(tokenDate, new LocalDateTime()).getHours() > 48;
}

From source file:cl.usach.managedbeans.CreditosManagedBean.java

public int obtenerHorasDiff(Date inicio, Date fin) {
    DateTime dtI = new DateTime(inicio);
    DateTime dtF = new DateTime(fin);
    int tiempo = Hours.hoursBetween(dtI, dtF).getHours();
    return tiempo;
}

From source file:cl.usach.managedbeans.EscritorioManagedBean.java

public String obtenerTiempo(Date inicio) {
    if (inicio == null)
        return "-";
    DateTime dtI = new DateTime(inicio);
    DateTime dtF = new DateTime(new Date());
    String tiempo = "";
    String aux;/*w  w w .  j  a  va 2 s.c  om*/
    if (Days.daysBetween(dtI, dtF).getDays() > 0)
        tiempo += Days.daysBetween(dtI, dtF).getDays() + " d, ";
    aux = Hours.hoursBetween(dtI, dtF).getHours() % 24 + ":";
    if (aux.length() == 2)
        aux = "0" + aux;
    tiempo += aux;
    aux = Minutes.minutesBetween(dtI, dtF).getMinutes() % 60 + "";
    if (aux.length() == 1)
        aux = "0" + aux;
    tiempo += aux + " hrs";
    return tiempo;
}

From source file:co.malm.mglam_reads.backend.util.DateDiffUtil.java

License:Apache License

/**
 * Calculates time differences using two dates, and stores it into a
 * {@linkplain java.util.Map} object./* w w w  . j av  a 2 s . c  om*/
 *
 * @param map calculations data
 * @param dt1 first date for diff
 * @param dt2 second date for diff
 * @see org.joda.time.DateTime
 * @see org.joda.time.Years
 * @see org.joda.time.Months
 * @see org.joda.time.Weeks
 * @see org.joda.time.Days
 * @see org.joda.time.Hours
 * @see org.joda.time.Minutes
 * @see org.joda.time.Seconds
 */
private void calculate(HashMap<String, Integer> map, DateTime dt1, DateTime dt2) {

    final int SECONDS_IN_MINUTE = 60;
    final int MINUTES_IN_HOUR = 60;
    final int HOURS_IN_DAY = 24;
    final int DAYS_IN_MONTH = 31;
    final int MONTHS_IN_YEAR = 12;
    final int WEEKS_IN_DAY = 7;

    int diffYears = Years.yearsBetween(dt1, dt2).getYears();
    if (diffYears > 0) {
        map.put("years", diffYears);
    }

    int diffMonths = Months.monthsBetween(dt1, dt2).getMonths();
    if (diffMonths >= 1 && diffMonths < MONTHS_IN_YEAR) {
        map.put("months", diffMonths);
    }

    int diffWeeks = Weeks.weeksBetween(dt1, dt2).getWeeks();
    if (diffWeeks >= 1 && diffWeeks < WEEKS_IN_DAY) {
        map.put("weeks", diffWeeks);
    }

    int diffDays = Days.daysBetween(dt1, dt2).getDays();
    if (diffDays >= 1 && diffDays < DAYS_IN_MONTH) {
        map.put("days", diffDays);
    }

    int diffHours = Hours.hoursBetween(dt1, dt2).getHours();
    if (diffHours >= 1 && diffHours < HOURS_IN_DAY) {
        map.put("hours", diffHours);
    }

    int diffMinutes = Minutes.minutesBetween(dt1, dt2).getMinutes();
    if (diffMinutes >= 1 && diffMinutes < MINUTES_IN_HOUR) {
        map.put("minutes", diffMinutes);
    }

    int diffSeconds = Seconds.secondsBetween(dt1, dt2).getSeconds();
    if (diffSeconds >= 1 && diffSeconds < SECONDS_IN_MINUTE) {
        map.put("seconds", diffSeconds);
    }
}