Example usage for org.joda.time Period getWeeks

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

Introduction

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

Prototype

public int getWeeks() 

Source Link

Document

Gets the weeks field part of the period.

Usage

From source file:com.alta189.cyborg.commandkit.twitter.TwitterCommands.java

License:Open Source License

@Command(name = "twitter", desc = "displays the last tweet of a twitter user", aliases = { "twit" })
@Usage(".twitter <user>")
public CommandResult twitter(CommandSource source, CommandContext context) {
    if (source.getSource() == CommandSource.Source.USER
            && (context.getPrefix() == null || !context.getPrefix().equals("."))) {
        return null;
    }//from   www . jav a 2  s  .  co  m
    if (context.getArgs() == null || context.getArgs().length < 1) {
        return get(ReturnType.NOTICE, "Correct usage is .twitter <user>", source, context);
    }

    try {
        List<Status> statusList = twitter.getUserTimeline(context.getArgs()[0]);
        if (statusList == null || statusList.size() < 1) {
            return get(ReturnType.MESSAGE, "User has no tweets!", source, context);
        }
        Status status = statusList.get(0);
        status.getUser();
        StringBuilder builder = new StringBuilder();
        builder.append(status.getUser().getScreenName()).append(Colors.BLUE).append(": ").append(Colors.NORMAL)
                .append(status.getText()).append(" (");

        Period period = new Period(new DateTime(status.getCreatedAt()), new DateTime());
        if (period.getWeeks() > 2 || period.getMonths() > 1 || period.getYears() > 1) {
            builder.append(longTimeFormatter.print(period));
        } else {
            builder.append(timeFormatter.print(period));
        }
        builder.append(" ago)");
        return get(ReturnType.MESSAGE, builder.toString().replace(lineBreak, " "), source, context);
    } catch (TwitterException e) {
        if (e.getStatusCode() == 404) {
            return get(ReturnType.MESSAGE, "User not found!", source, context);
        } else if (e.getStatusCode() == 401) {
            return get(ReturnType.MESSAGE, "Access denied by Twitter!", source, context);
        } else {
            e.printStackTrace();
            return get(ReturnType.MESSAGE, "There was an internal error!", source, context);
        }
    }
}

From source file:com.cloudhopper.commons.util.demo.UptimeMain.java

License:Apache License

public static void main(String[] args) {

    //Period period = new Period(uptime, PeriodType.standard().withYearsRemoved().withWeeksRemoved().withMonthsRemoved().withMillisRemoved());
    //MutablePeriod period = new Duration(uptime).toPeriod().toMutablePeriod();

    long uptime = UPTIME_56_SECS;

    // ah, ha -- this is super important -- need to normalize the period!
    PeriodType periodType = PeriodType.standard().withYearsRemoved().withMonthsRemoved().withWeeksRemoved()
            .withMillisRemoved();//from w  ww .java  2s . co  m
    Period period = new Period(uptime).normalizedStandard(periodType);

    System.out.println("Uptime: " + uptime + " ms");
    System.out.println("Weeks: " + period.getWeeks());
    System.out.println("Days: " + period.getDays());
    System.out.println("Millis: " + period.getMillis() + " ms");

    // print out the uptime
    String uptimeStyleString = PeriodFormatterUtil.getStandardUptimeStyle().print(period);
    String linuxStyleString = PeriodFormatterUtil.getLinuxUptimeStyle().print(period);

    System.out.println(uptimeStyleString);
    System.out.println(linuxStyleString);

    PeriodFormatter fmt = new PeriodFormatterBuilder().printZeroNever().appendDays()
            .appendSuffix(" day ", " days ").appendHours().appendSuffix(" hours ").appendMinutes()
            .appendSuffix(" mins ").printZeroAlways().appendSeconds().appendSuffix(" secs ").toFormatter();

    String str0 = fmt.print(period);
    System.out.println(str0);

    String str1 = PeriodFormat.getDefault().print(period);
    System.out.println(str1);
}

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 ww.j  av  a2s.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.gst.portfolio.loanaccount.loanschedule.domain.LoanApplicationTerms.java

License:Apache License

private Integer getPeriodsBetween(LocalDate fromDate, LocalDate toDate) {
    Integer numberOfPeriods = 0;/*from   w w w  .j  av a 2  s.c  o m*/
    PeriodType periodType = PeriodType.yearMonthDay();
    Period difference = new Period(fromDate, toDate, periodType);
    switch (this.repaymentPeriodFrequencyType) {
    case DAYS:
        numberOfPeriods = difference.getDays();
        break;
    case WEEKS:
        periodType = PeriodType.weeks();
        difference = new Period(fromDate, toDate, periodType);
        numberOfPeriods = difference.getWeeks();
        break;
    case MONTHS:
        numberOfPeriods = difference.getMonths();
        break;
    case YEARS:
        numberOfPeriods = difference.getYears();
        break;
    default:
        break;
    }
    return numberOfPeriods;
}

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   ww  w .j  av a2s . c o m
 */
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.mobileman.kuravis.core.util.DateTimeUtils.java

License:Apache License

/**
 * @param date//  ww w  . j  a  v  a2s .c  om
 * @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.sap.dirigible.runtime.metrics.TimeUtils.java

License:Open Source License

private static DateTime dateTimeCeiling(DateTime dt, Period p) {
    if (p.getYears() != 0) {
        return dt.yearOfEra().roundCeilingCopy().minusYears(dt.getYearOfEra() % p.getYears());
    } else if (p.getMonths() != 0) {
        return dt.monthOfYear().roundCeilingCopy().minusMonths((dt.getMonthOfYear() - 1) % p.getMonths());
    } else if (p.getWeeks() != 0) {
        return dt.weekOfWeekyear().roundCeilingCopy().minusWeeks((dt.getWeekOfWeekyear() - 1) % p.getWeeks());
    } else if (p.getDays() != 0) {
        return dt.dayOfMonth().roundCeilingCopy().minusDays((dt.getDayOfMonth() - 1) % p.getDays());
    } else if (p.getHours() != 0) {
        return dt.hourOfDay().roundCeilingCopy().minusHours(dt.getHourOfDay() % p.getHours());
    } else if (p.getMinutes() != 0) {
        return dt.minuteOfHour().roundCeilingCopy().minusMinutes(dt.getMinuteOfHour() % p.getMinutes());
    } else if (p.getSeconds() != 0) {
        return dt.secondOfMinute().roundCeilingCopy().minusSeconds(dt.getSecondOfMinute() % p.getSeconds());
    }// w w  w .  j a  va  2s . co  m
    return dt.millisOfSecond().roundCeilingCopy().minusMillis(dt.getMillisOfSecond() % p.getMillis());
}

From source file:imas.inventory.sessionbean.YieldManagementSessionBean.java

@Override
public String getFlightFromNowToDepartureString(FlightEntity flight) {
    DateTime departureTime = new DateTime(flight.getDepartureDate().getTime());
    DateTime now = new DateTime();
    Period period = new Period(now, departureTime);
    int years = period.getYears();
    int months = period.getMonths();
    int days = 7 * period.getWeeks() + period.getDays();

    String res = "";
    if (years > 0) {
        if (years > 1) {
            res = res + years + " years ";
        } else {/*from  www. j a  va  2s  . com*/
            res = "1 year ";
        }
    }
    if (months > 0) {
        if (months > 1) {
            res = res + months + " months ";
        } else {
            res = res + "1 month ";
        }
    }
    if (days > 0) {
        if (days > 1) {
            res = res + days + " days";
        } else {
            res = res + "1 day";
        }
    }

    return res;
}

From source file:influent.server.utilities.DateTimeParser.java

License:MIT License

/**
 * @see http://joda-time.sourceforge.net/apidocs/org/joda/time/Period.html#normalizedStandard()
 *//*from  w  w w  . j a  v  a2 s  .com*/
public static Period normalize(Period period) {
    long millis = period.getMillis();
    millis += period.getSeconds() * DateTimeConstants.MILLIS_PER_SECOND;
    millis += period.getMinutes() * DateTimeConstants.MILLIS_PER_MINUTE;
    millis += period.getHours() * DateTimeConstants.MILLIS_PER_HOUR;
    millis += period.getDays() * DateTimeConstants.MILLIS_PER_DAY;
    millis += period.getWeeks() * DateTimeConstants.MILLIS_PER_WEEK;

    Period result = new Period(millis, DateTimeUtils.getPeriodType(PeriodType.standard()),
            ISOChronology.getInstanceUTC());
    int years = period.getYears();
    int months = period.getMonths();

    if (years != 0 || months != 0) {
        years = FieldUtils.safeAdd(years, months / 12);
        months = months % 12;
        if (years != 0) {
            result = result.withYears(years);
        }
        if (months != 0) {
            result = result.withMonths(months);
        }
    }

    return result;
}

From source file:io.druid.sql.calcite.expression.builtin.TimeFloorOperatorConversion.java

License:Apache License

private static boolean periodIsDayMultiple(final Period period) {
    return period.getMillis() == 0 && period.getSeconds() == 0 && period.getMinutes() == 0
            && period.getHours() == 0 && (period.getDays() > 0 || period.getWeeks() > 0
                    || period.getMonths() > 0 || period.getYears() > 0);
}