Example usage for java.time Period between

List of usage examples for java.time Period between

Introduction

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

Prototype

public static Period between(LocalDate startDateInclusive, LocalDate endDateExclusive) 

Source Link

Document

Obtains a Period consisting of the number of years, months, and days between two dates.

Usage

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);//from www  .  j  ava2 s. com

    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: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);/*from  w  ww. j  av  a 2  s .c  om*/
}

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);/*from ww w . j av  a2s  .co m*/

    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[] argv) {
    LocalDate today = LocalDate.now();
    LocalDate longestDay = today.with(Month.JUNE).withDayOfMonth(21);
    int p = Period.between(today, longestDay).getDays();
    System.out.println(p);// w w w.j a va 2s  .  c  o  m
}

From source file:Main.java

public static void main(String[] argv) {
    LocalDate today = LocalDate.now();
    LocalDate java8Release = LocalDate.of(2014, Month.MARCH, 14);
    Period period = Period.between(java8Release, today);
    System.out.println("Period between today and Java 8 release : " + period);
    System.out.println("Period between Java 8 release and today : " + Period.between(today, java8Release));

}

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);
    }//from w w w .j  av a 2  s.co m

    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:org.zaproxy.admin.PendingAddOnReleases.java

public static void main(String[] args) throws Exception {
    LocalDate now = LocalDate.now(ZoneOffset.UTC);
    boolean showChanges = true;

    ZapXmlConfiguration zapVersions = new ZapXmlConfiguration(Paths.get(ZAP_VERSIONS_FILE_NAME).toFile());
    AddOnCollection addOnCollection = new AddOnCollection(zapVersions, AddOnCollection.Platform.daily);

    zapVersions.setExpressionEngine(new XPathExpressionEngine());

    Set<AddOnData> addOns = getAllAddOns();
    int totalAddOns = addOns.size();

    Set<AddOnData> unreleasedAddOns = new TreeSet<>();
    Set<AddOnData> unchangedAddOns = new TreeSet<>();

    for (Iterator<AddOnData> it = addOns.iterator(); it.hasNext();) {
        AddOnData addOnData = it.next();
        AddOn addOn = addOnCollection.getAddOn(addOnData.getId());
        if (addOn == null) {
            unreleasedAddOns.add(addOnData);
            it.remove();/*from   w w w. java  2 s . com*/
        } else if (addOn.getVersion().compareTo(addOnData.getVersion()) >= 0) {
            it.remove();
        } else if (addOnData.getChanges().isEmpty()) {
            unchangedAddOns.add(addOnData);
            it.remove();
        }
    }

    if (!unreleasedAddOns.isEmpty()) {
        System.out.println("=============================");
        System.out.println("Unreleased add-ons (" + unreleasedAddOns.size() + " of " + totalAddOns + ")");
        System.out.println("=============================");
        for (AddOnData addOn : unreleasedAddOns) {
            System.out.println(addOn.getStatus() + "\t" + addOn.getName() + " v" + addOn.getVersion());
        }
        System.out.println("=============================\n");
    }

    if (!addOns.isEmpty()) {
        System.out.println("=======================================");
        System.out.println("New versions pending release (" + addOns.size() + " of " + totalAddOns + ")");
        System.out.println("=======================================");
        Status currentStatus = null;
        for (AddOnData addOn : addOns) {
            if (currentStatus != addOn.getStatus()) {
                currentStatus = addOn.getStatus();
                System.out.println(currentStatus);
            }
            LocalDate releaseDate = LocalDate.parse(zapVersions.getString("/addon_" + addOn.getId() + "/date"));
            System.out.println("  * " + addOn.getName() + " v" + addOn.getVersion() + " ("
                    + Period.between(releaseDate, now) + ")");

            if (showChanges) {
                for (String change : addOn.getChanges()) {
                    System.out.println("       - " + change);
                }
            }
        }
        System.out.println("=======================================\n");
    }

    if (!unchangedAddOns.isEmpty()) {
        System.out.println("=============================");
        System.out.println("Unchanged add-ons (" + unchangedAddOns.size() + " of " + totalAddOns + ")");
        System.out.println("=============================");
        for (AddOnData addOn : unchangedAddOns) {
            System.out.println(addOn.getStatus() + "\t" + addOn.getName() + " v" + addOn.getVersion());
        }
        System.out.println("=============================\n");
    }
}

From source file:Main.java

private static int getElapsedYears(LocalDate targetDate, LocalDate today) {
    return Period.between(targetDate, today).getYears();
}

From source file:Main.java

public static Period period(LocalDate hiringDate) {
    LocalDate today = LocalDate.now();
    return Period.between(hiringDate, today);
}

From source file:Main.java

@Override
public LocalDate queryFrom(TemporalAccessor temporal) {
    LocalDate date = LocalDate.from(temporal);
    LocalDate currentYearMLKDay = getMartinLutherKingDayForDateInYear(date.getYear());

    Period periodToCurrentYearMLKDay = Period.between(date, currentYearMLKDay);

    if (periodToCurrentYearMLKDay.isNegative() || periodToCurrentYearMLKDay.isZero()) {
        return getMartinLutherKingDayForDateInYear(date.getYear() + 1);
    } else {//from   ww w . j  av a 2s.c  om
        return currentYearMLKDay;
    }
}