LocalTime Class



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 LocalDate class. In this lesson, we'll talk about the LocalTime class. The LocalTime class is included in the java.time package. This class does not store or represent a date or time zone. Instead, it's a description of the local time as seen as a wall clock. It cannot represent an instant on the timeline without additional information, such as an offset or time zone. The ISO-8601 calendar system is the modern civil calendar system used today in most of the world. This API assumes that all calendar systems use the same representation. This class for the time of day. Also, the LocalTime class is an immutable class. When we create an object of this class and assign a time to it, the value of that object always stays the same, until it's assigned to a different object. And the LocalTime class is thread-safe, just like the LocalDate class. 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 LocalTimeExample. 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 time to the console. I'll create a new object from the LocalTime class. We use the LocalTime class in the java.time package. The name of the object can be currentTime. After the = sign, I write LocalTime.now. Now, let's print the current time on the console. S out "current time" + currentTime". As you can see, the current time is printed on the console. We can also easily jump to a certain time after or before a specific time. For this, we use the plus() and minus() methods of the LocalTime class. For example, let's print one hour from now to the console. "LocalTime afterAnHourFromNow". "currentTime.plusHours". As you can see, there are available, the plusHours, plusMinutes, plusSeconds, or plusNanos methods. For now, we use the plusHours method. And I'll assign the 1 because we want to learn the time after one hour from now.

Now, let's print this on the console. S out "afterAnHourFromNow: " + afterAnHourFromNow. Let's run it. As you can see, the time after one hour from the current time is printed on the console. Here, the part after the second refers to the nanosecond. The order is as follows, first the hour, then the minute, then the second, and finally the nanosecond. Now, let's print 10 minutes ago to the console. I'm copying and pasting these two lines. Let the name of the object be "before10MinutesFromNow". And the method we'll use this time will be minusMinutes. It's enough to write 10 as the parameter because we want to know the time from 10 minutes ago. This will be "before10MinutesFromNow" and here too. Let's run and see. As you can see, the time before 10 minutes from the current time is printed on the console. We also said that the LocalTime class is immutable. To see this, let's print the currentTime object here once more to the console.

I'll copy this line and paste it here. If you notice, we have jumped to certain times by using the currentTime variable in the lines above. If the LocalTime class is mutable, the value of the currentTime variable should change. Otherwise if it is immutable, the same results should be printed on the console. Let's test it and observe the result. As you can see, the value of the currentTime object has not changed. From this result, you can understand that the LocalTime class is immutable. But we can make changes by passing a new value to the currentTime object. For example, let's transfer the time three hours later to the currentTime object. The "currentTime = currentTime.plusHours(2)". Now, let's print the current time to the console once again and test it. As you can see, the currentTime object now references the two hours from now. This value will now be valid for transactions after this line. Yes, I think the concept of immutable is also understood.

Of course if we want, we can also transfer a time that we have determined to a LocalTime object. For example, "LocalTime myMeetingTime = LocalTime.of". Thanks to the method of(), we can store specific time and use it in our code. Now, let's give the parameters of this method. The hour will be 17, the minute will be 30. Finally, the second can be 30. Now, let's print this to the console. S out "my meeting time: " + myMeetingTime. Let's run and test it. As you can see, we can easily print a specific time. We can also compare two different times with the LocalTime class. In other words, it's very easy to find out which of the two times is before and which is after with the LocalTime class because it has a readymade method that provides this. For example, let's compare my meeting time with the current time. For this, we'll use that isAfter() and isBefore() methods of the LocalTime class. These methods return true or false. Let's test it now. S out "is myMeetingTime before?" + myMeetingTime.isBefore(currentTime)". Let's run the code. As you can see, the result is printed as false because my meeting time is later than the current time. Yes, I think the LocalTime class is understood. The usage of methods is very similar to the LocalDate class. Let's take a short break here. See you in the next lesson.


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