Example usage for org.joda.time Period getHours

List of usage examples for org.joda.time Period getHours

Introduction

In this page you can find the example usage for org.joda.time Period getHours.

Prototype

public int getHours() 

Source Link

Document

Gets the hours field part of the period.

Usage

From source file:com.github.dbourdette.glass.tools.UtilsTool.java

License:Apache License

public String duration(Date start, Date end) {
    Period period = new Period(start.getTime(), end.getTime());

    StringBuilder builder = new StringBuilder();

    appendDuration(builder, period.getDays(), "d");
    appendDuration(builder, period.getHours(), "h");
    appendDuration(builder, period.getMinutes(), "m");
    appendDuration(builder, period.getSeconds(), "s");

    return builder.toString().trim();
}

From source file:com.github.jobs.utils.RelativeDate.java

License:Apache License

/**
 * This method returns a String representing the relative
 * date by comparing the Calendar being passed in to the
 * date / time that it is right now.// w  w  w  .  j  av  a  2 s .c o m
 *
 * @param context used to build the string response
 * @param time    time to compare with current time
 * @return a string representing the time ago
 */
public static String getTimeAgo(Context context, long time) {
    DateTime baseDate = new DateTime(time);
    DateTime now = new DateTime();
    Period period = new Period(baseDate, now);

    if (period.getSeconds() < 0 || period.getMinutes() < 0) {
        return context.getString(R.string.just_now);
    }

    if (period.getYears() > 0) {
        int resId = period.getYears() == 1 ? R.string.one_year_ago : R.string.years_ago;
        return buildString(context, resId, period.getYears());
    }

    if (period.getMonths() > 0) {
        int resId = period.getMonths() == 1 ? R.string.one_month_ago : R.string.months_ago;
        return buildString(context, resId, period.getMonths());
    }

    if (period.getWeeks() > 0) {
        int resId = period.getWeeks() == 1 ? R.string.one_week_ago : R.string.weeks_ago;
        return buildString(context, resId, period.getWeeks());
    }

    if (period.getDays() > 0) {
        int resId = period.getDays() == 1 ? R.string.one_day_ago : R.string.days_ago;
        return buildString(context, resId, period.getDays());
    }

    if (period.getHours() > 0) {
        int resId = period.getHours() == 1 ? R.string.one_hour_ago : R.string.hours_ago;
        return buildString(context, resId, period.getHours());
    }

    if (period.getMinutes() > 0) {
        int resId = period.getMinutes() == 1 ? R.string.one_minute_ago : R.string.minutes_ago;
        return buildString(context, resId, period.getMinutes());
    }

    int resId = period.getSeconds() == 1 ? R.string.one_second_ago : R.string.seconds_ago;
    return buildString(context, resId, period.getSeconds());
}

From source file:com.google.livingstories.server.util.TimeUtil.java

License:Apache License

/**
 * Return a String representation of the time that has passed from the given time to
 * right now./*w w w  . j av a 2  s.c o  m*/
 * This method returns an approximate user-friendly duration. Eg. If 2 months, 12 days and 4 hours
 * have passed, the method will return "2 months ago".
 * TODO: the results of this method need to be internationalized
 */
public static String getElapsedTimeString(Date updateCreationTime) {
    Period period = new Period(updateCreationTime.getTime(), new Date().getTime(),
            PeriodType.yearMonthDayTime());

    int years = period.getYears();
    int months = period.getMonths();
    int days = period.getDays();
    int hours = period.getHours();
    int minutes = period.getMinutes();
    int seconds = period.getSeconds();

    String timeLabel = "";

    if (years > 0) {
        timeLabel = years == 1 ? " year " : " years ";
        return "" + years + timeLabel + "ago";
    } else if (months > 0) {
        timeLabel = months == 1 ? " month " : " months ";
        return "" + months + timeLabel + "ago";
    } else if (days > 0) {
        timeLabel = days == 1 ? " day " : " days ";
        return "" + days + timeLabel + "ago";
    } else if (hours > 0) {
        timeLabel = hours == 1 ? " hour " : " hours ";
        return "" + hours + timeLabel + "ago";
    } else if (minutes > 0) {
        timeLabel = minutes == 1 ? " minute " : " minutes ";
        return "" + minutes + timeLabel + "ago";
    } else if (seconds > 0) {
        timeLabel = seconds == 1 ? " second " : " seconds ";
        return "" + seconds + timeLabel + "ago";
    } else {
        return "1 second ago";
    }
}

From source file:com.jajja.jorm.mixins.Postgres.java

License:Open Source License

public static PGInterval toInterval(Period period) {
    if (period == null) {
        return null;
    }//from   ww  w. j a v a  2  s .co  m
    return new PGInterval(period.getYears(), period.getMonths(), period.getDays(), period.getHours(),
            period.getMinutes(), period.getSeconds() + (double) period.getMillis() / 1000);
}

From source file:com.jbirdvegas.mgerrit.search.AgeSearch.java

License:Apache License

/**
 * Adds adjustment to the shortest set time range in period. E.g.
 *  period("5 days 3 hours", 1) -> "5 days 4 hours". This will fall
 *  back to adjusting years if no field in the period is set.
 * @param period The period to be adjusted
 * @param adjustment The adjustment. Note that positive values will result
 *                   in larger periods and an earlier time
 * @return The adjusted period/*from w w w  . j a v  a 2s .  c om*/
 */
private Period adjust(final Period period, int adjustment) {
    if (adjustment == 0)
        return period;

    // Order is VERY important here
    LinkedHashMap<Integer, DurationFieldType> map = new LinkedHashMap<>();
    map.put(period.getSeconds(), DurationFieldType.seconds());
    map.put(period.getMinutes(), DurationFieldType.minutes());
    map.put(period.getHours(), DurationFieldType.hours());
    map.put(period.getDays(), DurationFieldType.days());
    map.put(period.getWeeks(), DurationFieldType.weeks());
    map.put(period.getMonths(), DurationFieldType.months());
    map.put(period.getYears(), DurationFieldType.years());

    for (Map.Entry<Integer, DurationFieldType> entry : map.entrySet()) {
        if (entry.getKey() > 0) {
            return period.withFieldAdded(entry.getValue(), adjustment);
        }
    }
    // Fall back to modifying years
    return period.withFieldAdded(DurationFieldType.years(), adjustment);
}

From source file:com.microsoft.azure.management.servicebus.implementation.TimeSpan.java

License:Open Source License

/**
 * Gets TimeSpan from given period.//from w  w  w  .  jav a2s. c o  m
 *
 * @param period duration in period format
 * @return TimeSpan
 */
public static TimeSpan fromPeriod(Period period) {
    // Normalize (e.g. move weeks to hour part)
    //
    Period p = new Period(period.toStandardDuration().getMillis());
    return TimeSpan
            .parse((new TimeSpan().withDays(p.getDays()).withHours(p.getHours()).withMinutes(p.getMinutes())
                    .withSeconds(p.getSeconds()).withMilliseconds(p.getMillis())).toString());
}

From source file:com.mobileman.kuravis.core.util.DateTimeUtils.java

License:Apache License

/**
 * @param date//  w  ww  .  jav  a 2s  . c o m
 * @return formatted elapsed time from now
 */
public static String fmtElapsedTime(Date date) {
    if (date == null) {
        return "";
    }
    Period period = new Period(date.getTime(), Calendar.getInstance().getTimeInMillis());
    PeriodFormatterBuilder pf = new PeriodFormatterBuilder();
    pf.appendPrefix(" vor ");
    if (period.getYears() > 0) {
        pf.appendYears().appendSuffix(" Jahr", " Jahren");
    } else if (period.getMonths() > 0) {
        pf.appendMonths().appendSuffix(" Monat", " Monaten");
    } else if (period.getWeeks() > 0) {
        pf.appendWeeks().appendSuffix(" Woche ", " Wochen");
    } else if (period.getDays() > 0) {
        pf.appendDays().appendSuffix(" Tag ", " Tagen");
    } else if (period.getHours() > 0) {
        pf.appendHours().appendSuffix(" Stunde ", " Stunden");
    } else if (period.getMinutes() > 0) {
        pf.appendMinutes().appendSuffix(" Minute ", " Minuten");
    } else if (period.getSeconds() > 0) {
        pf.appendSeconds().appendSuffix(" Sekunde ", " Sekunden");
    }
    return pf.toFormatter().print(period);
}

From source file:com.moss.jodapersist.SplitHoursMinutesDurationUserType.java

License:Open Source License

public void nullSafeSet(PreparedStatement statement, Object value, int index)
        throws HibernateException, SQLException {
    Period period = (Period) value;

    statement.setInt(index, period.getHours());
    statement.setInt(index + 1, period.getDays());
}

From source file:com.mycompany.ajaxandxml.ws.DurationAdapter.java

@Override
public String marshal(Period v) throws Exception {
    return v.getHours() + " hour " + v.getMinutes() + " minutes and " + v.getSeconds() + " seconds";
}

From source file:com.qcadoo.mes.avgLaborCostCalcForOrder.AverageCostService.java

License:Open Source License

private BigDecimal getWorkedHoursOfWorker(final Entity shift, final DateTime dateOfDay) {
    BigDecimal hours = BigDecimal.ZERO;
    List<ShiftHour> workedHours = shiftsService.getHoursForShift(shift, dateOfDay.toDate(),
            dateOfDay.plusDays(1).toDate());
    for (ShiftHour shiftHour : workedHours) {
        DateTime dateFrom = new DateTime(shiftHour.getDateFrom());
        DateTime dateTo = new DateTime(shiftHour.getDateTo());
        Period p = new Period(dateFrom, dateTo);
        hours = hours.add(new BigDecimal(p.getHours()));
    }/*from   ww w.  j  a  v  a2s  .  co  m*/
    return hours;
}