JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Checkout for Promo Codes

Java 8 Period example.


The Period class represents a quantity of time in terms of years, months and days. A Period uses date based values (years, months, days).

PeriodEx
package com.java2novice.datetime;

import java.time.LocalDate;
import java.time.Period;

public class PeriodEx {

	public static void main(String a[]) {

		// get current date
		LocalDate currTime = LocalDate.now();
		System.out.println("current date: "+currTime);

		// add 10 days to the current date time
		LocalDate dateAfter10Days = currTime.plus(Period.ofDays(10));
		System.out.println("date after 10 days: "+dateAfter10Days);

		// what is the date a week before
		LocalDate dateBeforeWeek = currTime.minus(Period.ofWeeks(3));
		System.out.println("date before 3 week: "+dateBeforeWeek);

		// no of days between two dates
		Period periofDiff = Period.between(dateBeforeWeek, dateAfter10Days);
		System.out.println("day difference: "+periofDiff.getDays());
		System.out.println("months difference: "+periofDiff.getMonths());
	}
}

Output:
current date: 2016-12-15
date after 10 days: 2016-12-25
date before 3 week: 2016-11-24
day difference: 1
months difference: 1
<< Previous Program | Next Program >>

Java-8 Date and Time API Examples

  1. Java 8 LocalDateTime example.
  2. Java 8 ZonedDateTime example.
  3. Java 8 LocalDate example.
  4. Java 8 LocalTime example.
  5. Create date time with custom values in Java 8.
  6. Java 8 Instant (Timestamp) example.
  7. Java 8 Period example.
  8. Java 8 Duration example.
  9. Java 8 Date parsing and formatting example.
  10. How to compare two dates in Java 8?
  11. How to get current date and time Java 8?
  12. How to calculate execution / elapsed time Java 8?
  13. How to convert string to Date Java 8?
  14. How to add/substract days to Date in Java 8?
Knowledge Centre
true and false are keywords?
true, false, and null might seem like keywords, but they are actually literals. You cannot use them as identifiers in your programs.
Famous Quotations
Good judgment comes from experience, and experience comes from bad judgment.
-- Barry LePatner

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2020 by Nataraja Gootooru. All Rights Reserved.