Example usage for java.time Period getMonths

List of usage examples for java.time Period getMonths

Introduction

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

Prototype

public int getMonths() 

Source Link

Document

Gets the amount of months of this period.

Usage

From source file:Main.java

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

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

}

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 ww w . j a  v a2  s.c o  m
    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);/* w ww. j av a2s  .c om*/

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

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

}

From source file:Main.java

public static void main(String[] args) {
    LocalDate today = LocalDate.now();

    // Temporal adjusters for adjusting the dates
    System.out.println("First date of this month= " + today.with(TemporalAdjusters.firstDayOfMonth()));
    LocalDate lastDayOfYear = today.with(TemporalAdjusters.lastDayOfYear());
    System.out.println("Last date of this year= " + lastDayOfYear);

    Period period = today.until(lastDayOfYear);
    System.out.println("Period Format= " + period);
    System.out.println("Months remaining in the year= " + period.getMonths());

}

From source file:Main.java

public static void main(String[] args) {

    LocalDate today = LocalDate.now();
    LocalDate birthday = LocalDate.of(1960, Month.JANUARY, 1);

    LocalDate nextBDay = birthday.withYear(today.getYear());

    //If your birthday has occurred this year already, add 1 to the year.
    if (nextBDay.isBefore(today) || nextBDay.isEqual(today)) {
        nextBDay = nextBDay.plusYears(1);
    }//  w w w. j  a  v  a2 s  . c om

    Period p = Period.between(today, nextBDay);
    long p2 = ChronoUnit.DAYS.between(today, nextBDay);
    System.out.println("There are " + p.getMonths() + " months, and " + p.getDays()
            + " days until your next birthday. (" + p2 + " total)");
}

From source file:Main.java

public static void main(String[] args) {
    Period p = Period.between(LocalDate.of(2009, Month.JANUARY, 21), LocalDate.of(2019, Month.JANUARY, 21));

    System.out.println(p.get(ChronoUnit.DAYS));

    LocalDate today = LocalDate.now();
    LocalDate birthday = LocalDate.of(1960, Month.JANUARY, 1);

    LocalDate nextBDay = birthday.withYear(today.getYear());

    nextBDay = nextBDay.plusYears(1);/* w w w  .j av  a2 s .  c  o m*/

    p = Period.between(today, nextBDay);
    long p2 = ChronoUnit.DAYS.between(today, nextBDay);
    System.out.println(p.getMonths() + " months");
    System.out.println(p.getDays() + " days");
}

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 www.ja  v  a  2s . 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:ca.phon.session.io.xml.v12.XMLSessionWriter_v12.java

/**
 * copy participant info//from www .  j a  v  a2s . 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 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;
}