Example usage for org.joda.time Period getYears

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

Introduction

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

Prototype

public int getYears() 

Source Link

Document

Gets the years field part of the period.

Usage

From source file:au.com.scds.chats.dom.general.Person.java

License:Apache License

@Programmatic()
public Integer getAge(LocalDate futureDate) {
    if (futureDate == null)
        futureDate = LocalDate.now();
    Period p = new Period(getBirthdate(), futureDate);
    return p.getYears();
}

From source file:au.org.theark.study.service.StudyServiceImpl.java

License:Open Source License

private String calculatePedigreeAge(Date birthDate, Date selectDate) {
    String age = null;//  w w  w  .  java2  s.c  o m
    LocalDate oldDate = null;
    LocalDate newDate = null;

    oldDate = new LocalDate(birthDate);

    if (selectDate != null) {
        newDate = new LocalDate(selectDate);
    } else {
        newDate = new LocalDate();
    }

    Period period = new Period(oldDate, newDate, PeriodType.yearMonthDay());
    int years = period.getYears();
    age = "" + (years < 1 ? "&lt;1" : years);

    return age;
}

From source file:beans.utilidades.MetodosGenerales.java

public String calcularEdad(Date fechaNacimiento) {//calcular la edad a partir de la fecha de nacimiento    
    Period periodo = new Period(new DateTime(fechaNacimiento), new DateTime(new Date()));
    return String.valueOf(periodo.getYears()) + "A " + String.valueOf(periodo.getMonths()) + "M ";
}

From source file:beans.utilidades.MetodosGenerales.java

public int calcularEdadInt(Date fechaNacimiento) {//calcular la edad en aos
    Period periodo = new Period(new DateTime(fechaNacimiento), new DateTime(new Date()));
    if (periodo.getYears() == 0) {
        return 1;
    } else {//w  w w.j a va 2 s.  c  om
        return periodo.getYears();
    }
}

From source file:ca.phon.session.io.xml.v12.XMLSessionWriter_v12.java

License:Open Source License

/**
 * copy participant info/*from ww  w .ja  v  a  2 s .  c  o  m*/
 */
private ParticipantType copyParticipant(ObjectFactory factory, Participant part) {
    final ParticipantType retVal = factory.createParticipantType();

    if (part.getId() != null)
        retVal.setId(part.getId());

    retVal.setName(part.getName());

    final DateTime bday = part.getBirthDate();
    if (bday != null) {
        try {
            final DatatypeFactory df = DatatypeFactory.newInstance();
            final XMLGregorianCalendar cal = df.newXMLGregorianCalendar(bday.toGregorianCalendar());
            cal.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
            retVal.setBirthday(cal);
        } catch (DatatypeConfigurationException e) {
            LOGGER.log(Level.WARNING, e.toString(), e);
        }
    }

    final Period age = part.getAge(null);
    if (age != null) {
        try {
            final DatatypeFactory df = DatatypeFactory.newInstance();
            final Duration ageDuration = df.newDuration(true, age.getYears(), age.getMonths(), age.getDays(), 0,
                    0, 0);
            retVal.setAge(ageDuration);
        } catch (DatatypeConfigurationException e) {
            LOGGER.log(Level.WARNING, e.toString(), e);
        }
    }

    retVal.setEducation(part.getEducation());
    retVal.setGroup(part.getGroup());

    final String lang = part.getLanguage();
    final String langs[] = (lang != null ? lang.split(",") : new String[0]);
    for (String l : langs) {
        retVal.getLanguage().add(StringUtils.strip(l));
    }

    if (part.getSex() == Sex.MALE)
        retVal.setSex(SexType.MALE);
    else if (part.getSex() == Sex.FEMALE)
        retVal.setSex(SexType.FEMALE);

    ParticipantRole prole = part.getRole();
    if (prole == null)
        prole = ParticipantRole.TARGET_CHILD;
    retVal.setRole(prole.toString());

    // create ID based on role if possible
    if (retVal.getId() == null && prole != null) {
        if (prole == ParticipantRole.TARGET_CHILD) {
            retVal.setId("CHI");
        } else if (prole == ParticipantRole.MOTHER) {
            retVal.setId("MOT");
        } else if (prole == ParticipantRole.FATHER) {
            retVal.setId("FAT");
        } else if (prole == ParticipantRole.INTERVIEWER) {
            retVal.setId("INT");
        } else {
            retVal.setId("p" + (++pIdx));
        }
    }

    retVal.setSES(part.getSES());

    return retVal;
}

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 w ww.  ja  v a2 s  .c o 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.c2a.vie.managedbeans.deces.ContratgroupeManagedBean.java

public int ageassure() {
    Calendar calendar = new GregorianCalendar();
    LocalDate aujourdui = new LocalDate();
    calendar.setTime(selectassurepret.getDatnaisassure());
    int annee = calendar.get(Calendar.YEAR);
    int mois = calendar.get(Calendar.MONTH);
    int jours = calendar.get(Calendar.DAY_OF_MONTH);
    LocalDate naissance = new LocalDate(annee, mois, jours);
    Period p = new Period(naissance, aujourdui, PeriodType.yearMonthDay());
    return p.getYears();
}

From source file:com.c2a.vie.managedbeans.deces.ContratgroupeManagedBean.java

public int ageassureren() {
    Calendar calendar = new GregorianCalendar();
    LocalDate aujourdui = new LocalDate();
    calendar.setTime(selectrnouvelmentcontrat.getCodassure().getDatnaisassure());
    int annee = calendar.get(Calendar.YEAR);
    int mois = calendar.get(Calendar.MONTH);
    int jours = calendar.get(Calendar.DAY_OF_MONTH);
    LocalDate naissance = new LocalDate(annee, mois, jours);
    Period p = new Period(naissance, aujourdui, PeriodType.yearMonthDay());
    return p.getYears();
}

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.//from w  w w.j  a  v  a 2 s  .c  om
 *
 * @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.//from ww  w  .j a  v a  2s. c  om
 * 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";
    }
}