The course is part of this learning path

Start course

In this course, we will talk about DateTime Class. 

Learning Objectives

  • LocalDate Class
  • LocalTime Class
  • LocalDateTime Class
  • Period Class
  • DateTime Formatting

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams


  • No prior knowledge is required about the Java programming language
  • Basic computer knowledge

Hi there. In the previous lesson, we learned the LocalDateTime class. In this lesson, we'll talk about the Period class. This class is often used with the LocalDate class and allows us to determine the date in a certain period. For example, we can easily find the date 3 years, 5 months and 20 days from now with the Period class, or it's possible to obtain the difference between two specific dates easily, thanks to the Period class. The Period class is included in the java.time package. Also, the Period class is an immutable class and it's thread-safe just like the LocalDate, LocalTime and LocalDateTime classes. Yes, after this information, let's move on to Eclipse and get some practice. I'll create a new class in the datetimeexample package. 

I right click on the datetimeexample package and select the new class options. The class name can be PeriodExample. Lastly, I'll check the checkbox for the main method and click the 'Finish' button to create this class. Okay, first let's print the current date on the console. localDate currentDate = Now, let's print the current date on the console. S.out.println( " current date: " + currentDate). As you can see, the current time period is printed on the console. You already know this. Now let's use the Period class. I write Period here. We use the Period class in the java.time package. The name of this object can be period again. After the equal sign, I write the Period and put a dot. You can see the methods of the Period class, such as, between, of, ofDay, ofMonths, ofWeeks and ofYear. You can find the time between two dates with the between method. 

With the of method, you can jump to a specific date by using the year, month and day values. Or if you only want to work with dates such as month, week or year, you can choose other methods here. But if you notice, there's no method related to the time here because the Period class  is used together with the LocalDate class. Now, let's use the of method. This method takes three parameters. The first one is year, the second one is month, and the third one is day. Let the year be 3, let the month be 7, and let the day be 2. First, let's print this on the console, S.out.println( "period + period"). Let's run the code. As you can see, the period is printed on the console. 

If you notice, there is the letter P at the beginning, this indicates the period. 3Y means three years, 7M means seven months, 2D means two days. Now let's add this period to the current day object and print it to the console. S.out.println( "after adding period: " + period) ). Let's run and see. As you can see, the output is the year 2026, the month 1, and the day 20. Now let's examine this. We determined the month value as seven in the period. Since the current month is six, the sum of the seven and six is 13 months. This means one year and one month. Therefore, it adds one year to the year and actually adds four years to the current time. For this reason, the year 2026 became one in the month. 

The day is 18 + 2 = 20. Okay, now let's find the difference between the two specific dates. Let's create another LocalDate object. LocalDate weddingDate = LocalDate.of( ). The year will be 2023, the month will be 7 and the day will be 20. Now, let's find the time left to the wedding date by using the between method of the period class. S.out.println(" remaining time to the wedding: " + Period.between( currentDate, weddingDate) ). The first parameter will be the currentDate, the second parameter will be the weddingDate. So, it will subtract the first parameter from the second parameter, i.e., weddingDate - currentDate. Let's run and see. As you can see, the period is 1 year, 1 month and 2 days. Finally, let's try to use the Period class with the for loop. First, let's create a new Period object, Period newPeriod = Period.ofWeeks( ). This time I'll use the ofWeeks method and let the period be 2. Now, let's create the for loop. for( int i = 0; i < 10; i ++). 

In the loop, first I will add this period to the current date. If I leave it like that, the value of the current date will not change because the LocalDate is immutable. I'll assign it to the currentDate object again. So, at the beginning of the code I write, currentDate =. Now, let's print the current date on the console. S.out.println(" after two weeks: " + currentDate). Okay, let's run the code and see. As you can see, it added two weeks to the current date and printed it to the console. Then it added two more weeks once in each cycle. It added two weeks to the previous date until the cycle ends. After the value of i is 10, the loop is terminated. It's also possible to use the Period class in this way. Okay, the Period class is like that. Let's take a short break here. See you in the next video.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics