Example usage for java.time Period getYears

List of usage examples for java.time Period getYears

Introduction

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

Prototype

public int getYears() 

Source Link

Document

Gets the amount of years of this period.

Usage

From source file:Main.java

public static void main(String[] args) {
    Period p = Period.ofMonths(1);

    System.out.println(p.getYears());

}

From source file:Main.java

public static void main(String[] args) {
    int years = 3;
    int months = 6;
    Period time = Period.ofYears(years).withMonths(months);

    System.out.println(time.getYears());
    System.out.println(time.getMonths());
}

From source file:Main.java

public static void main(String[] args) {
    Period employmentPeriod = period(LocalDate.of(2000, Month.FEBRUARY, 1));
    int years = employmentPeriod.getYears();
    int months = employmentPeriod.getMonths();
    int days = employmentPeriod.getDays();

    System.out.println(years);/*from   www  .  j  a v a 2 s  .c om*/
    System.out.println(months);
    System.out.println(days);
}

From source file:Main.java

public static void main(String[] args) {

    LocalDate firstDate = LocalDate.of(2013, 5, 17);
    LocalDate secondDate = LocalDate.of(2015, 3, 7);
    Period period = Period.between(firstDate, secondDate);

    System.out.println(period);/*ww  w  . j av a  2s .c  o m*/

    int days = period.getDays(); // 18

    int months = period.getMonths(); // 9
    int years = period.getYears(); // 4
    boolean isNegative = period.isNegative(); // false

}

From source file:FactFind.PersonalDetails.java

static String CalculateAge(String DOB) {
    LocalDate today = LocalDate.now();

    String[] str_array = DOB.split("/");

    LocalDate birthday = LocalDate.of(Integer.parseInt(str_array[2]), Month.of(Integer.parseInt(str_array[1])),
            Integer.parseInt(str_array[0]));

    Period p = Period.between(birthday, today);

    return Integer.toString(p.getYears());
}

From source file:edu.lternet.pasta.portal.search.TemporalList.java

private static int calculateMonths(String beginDate, String endDate) {
    int durationInMonths;

    LocalDate localBeginDate = LocalDate.parse(beginDate);
    LocalDate localEndDate = LocalDate.parse(endDate);

    Period duration = Period.between(localBeginDate, localEndDate);
    int years = duration.getYears();
    int months = duration.getMonths();
    int days = duration.getDays();
    durationInMonths = ((years * 12) + months);
    if (days > 15) {
        durationInMonths++;/*from  w ww  .j av  a  2 s  . c o m*/
    }

    System.out.printf("Begin Date: %s,  End Date: %s\n", beginDate, endDate);
    System.out.printf("The duration is %d years, %d months and %d days\n", duration.getYears(),
            duration.getMonths(), duration.getDays());
    System.out.printf("The total duration in months is %d\n\n", durationInMonths);

    return durationInMonths;
}

From source file:example.app.model.Person.java

@Transient
@SuppressWarnings("all")
public int getAge() {
    LocalDate birthDate = getBirthDate();
    Assert.state(birthDate != null, String.format("birth date of person [%s] is unknown", getName()));
    Period period = Period.between(birthDate, LocalDate.now());
    return period.getYears();
}

From source file:svc.managers.SMSManager.java

private String validateDOBString(String dob) {
    String errMsg = "";
    dob = dob.trim();//from w ww.  j  a  va2 s.  com
    if (!dob.matches("^\\d{2}([./-])\\d{2}\\1\\d{4}$")) {
        //makes sure DOB is a valid date string format, also accepts "." and "-"
        errMsg = "You entered: '" + dob + "' Please re-enter your birthdate.  The format is: MM/DD/YYYY";
    } else {
        //be forgiving for dates delineated with "." or "-"
        dob = dob.replaceAll("[.-]", "/");
        if (!DatabaseUtilities.isStringValidUSDateString(dob)) {
            //DOB is not a valid date, meaning month is out of bounds or day of month does not align with month or year
            errMsg = "The date you entered: '" + dob
                    + "' was not a valid date.  Please re-enter your birthdate using MM/DD/YYYY";
        } else {
            //Check that the DOB is > 18 years old
            LocalDate dobLD = DatabaseUtilities.convertUSStringDateToLD(dob);
            LocalDate nowLD = DatabaseUtilities.getCurrentDate();
            Period age = dobLD.until(nowLD);
            if (age.getYears() < 18) {
                errMsg = "You must be at least 18 years old to use www.yourSTLcourts.com";
            }
        }
    }
    return errMsg;
}

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

/**
 * copy participant info//from ww w.j a v  a 2  s .co  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 LocalDate bday = part.getBirthDate();
    if (bday != null) {
        try {
            final DatatypeFactory df = DatatypeFactory.newInstance();
            final XMLGregorianCalendar cal = df
                    .newXMLGregorianCalendar(GregorianCalendar.from(bday.atStartOfDay(ZoneId.systemDefault())));
            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;
}