Example usage for org.joda.time Period normalizedStandard

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

Introduction

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

Prototype

public Period normalizedStandard(PeriodType type) 

Source Link

Document

Normalizes this period using standard rules, assuming a 12 month year, 7 day week, 24 hour day, 60 minute hour and 60 second minute, providing control over how the result is split into fields.

Usage

From source file:com.thinkbiganalytics.scheduler.util.TimerToCronExpression.java

License:Apache License

/**
 * Parse a timer string to a Joda time period
 *
 * @param timer      a string indicating a time unit (i.e. 5 sec)
 * @param periodType the Period unit to use.
 *///from ww  w.j a v a 2s .co  m
public static Period timerStringToPeriod(String timer, PeriodType periodType) {
    String cronString = null;
    Integer time = Integer.parseInt(StringUtils.substringBefore(timer, " "));
    String units = StringUtils.substringAfter(timer, " ").toLowerCase();
    //time to years,days,months,hours,min, sec
    Integer days = 0;
    Integer hours = 0;
    Integer min = 0;
    Integer sec = 0;
    Period p = null;
    if (units.startsWith("sec")) {
        p = Period.seconds(time);
    } else if (units.startsWith("min")) {
        p = Period.minutes(time);
    } else if (units.startsWith("hr") || units.startsWith("hour")) {
        p = Period.hours(time);
    } else if (units.startsWith("day")) {
        p = Period.days(time);
    }
    if (periodType != null) {
        p = p.normalizedStandard(periodType);
    } else {
    }
    return p;
}

From source file:com.wellsandwhistles.android.redditsp.common.SRTime.java

License:Open Source License

public static String formatDurationFrom(final Context context, final long startTime) {
    final String space = " ";
    final String comma = ",";
    final String separator = comma + space;

    final long endTime = utcCurrentTimeMillis();
    final DateTime dateTime = new DateTime(endTime);
    final DateTime localDateTime = dateTime.withZone(DateTimeZone.getDefault());
    Period period = new Duration(startTime, endTime).toPeriodTo(localDateTime);

    PeriodFormatter periodFormatter = new PeriodFormatterBuilder().appendYears().appendSuffix(space)
            .appendSuffix(context.getString(R.string.time_year), context.getString(R.string.time_years))
            .appendSeparator(separator).appendMonths().appendSuffix(space)
            .appendSuffix(context.getString(R.string.time_month), context.getString(R.string.time_months))
            .appendSeparator(separator).appendDays().appendSuffix(space)
            .appendSuffix(context.getString(R.string.time_day), context.getString(R.string.time_days))
            .appendSeparator(separator).appendHours().appendSuffix(space)
            .appendSuffix(context.getString(R.string.time_hour), context.getString(R.string.time_hours))
            .appendSeparator(separator).appendMinutes().appendSuffix(space)
            .appendSuffix(context.getString(R.string.time_min), context.getString(R.string.time_mins))
            .appendSeparator(separator).appendSeconds().appendSuffix(space)
            .appendSuffix(context.getString(R.string.time_sec), context.getString(R.string.time_secs))
            .appendSeparator(separator).appendMillis().appendSuffix(space)
            .appendSuffix(context.getString(R.string.time_ms)).toFormatter();

    String duration = periodFormatter.print(period.normalizedStandard(PeriodType.yearMonthDayTime()));

    List<String> parts = Arrays.asList(duration.split(comma));
    if (parts.size() >= 2) {
        duration = parts.get(0) + comma + parts.get(1);
    }/*from  ww  w. j a  va 2 s .co m*/

    return String.format(context.getString(R.string.time_ago), duration);
}

From source file:org.apereo.portlet.hr.timereporting.util.TimeCalculations.java

License:Apache License

public static String convertPeriodToHHMM(Period period) {
    Period timeStandardized = period.normalizedStandard(PeriodType.time());
    return getDoubleDigit(timeStandardized.getHours()) + ":" + getDoubleDigit(timeStandardized.getMinutes());
}

From source file:org.jasig.portlet.blackboardvcportlet.dao.impl.SessionImpl.java

License:Apache License

@Override
public String getTimeFancyText(DateTime from, DateTime to) {
    final String prefix = "Join in ";
    if (to != null) {
        Duration d = new Duration(to, from);
        Period timeUntil = new Period(to.toInstant(), from.toInstant(), PeriodType.dayTime());

        long standardDays = d.getStandardDays();

        if (standardDays > 0) {
            PeriodFormatter daysHours = new PeriodFormatterBuilder().appendDays().appendSuffix(" day", " days")
                    .appendSeparator(", and ").appendHours().appendSuffix(" hour", " hours").toFormatter();
            return prefix + daysHours.print(timeUntil.normalizedStandard(PeriodType.dayTime()));
        } else {/* ww w . java2  s .  c om*/
            PeriodFormatter dafaultFormatter = new PeriodFormatterBuilder().appendHours()
                    .appendSuffix(" hour", " hours").appendSeparator(", and ").appendMinutes()
                    .appendSuffix(" minute", " minutes").toFormatter();
            return prefix + dafaultFormatter.print(timeUntil.normalizedStandard(PeriodType.dayTime()));
        }

    } else {
        return null;
    }
}