Date and Time
The course is part of this learning path
In this course, we will talk about DateTime Class.
- LocalDate Class
- LocalTime Class
- LocalDateTime Class
- Period Class
- DateTime Formatting
- 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 LocalTime class. In this lesson, we'll talk about the LocalDateTime class. The LocalDateTime class is included in the java.time package. Also, the LocalDateTime 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 and LocalTime 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 LocalDateTimeExample. Last, I'll check the check box for the main method and click the 'Finish' button to create this class. Okay, let's print the current time to the console. I'll create a new object from the LocalTimeClass. We use the LocalDateTime class in the java.time package. The name of the object can be currentDateTime. After the equal sign, I write LocalDateTime.now. Now, let's print the current date time on the console; S out "current date time" + currentDateTime. As you can see, the current date time is printed on the console. The order is as follows. First, the year, then the month, then the day, then the T letter represents the time stamp. It separates the date and time from each other. After the T letter, first is the hour; then the minute and then the second and lastly the nanosecond.
Okay, we can also easily jump to a certain time after or before specific time. For this, we use the plus and minus methods of the LocalDateTime class. For example, let's print one day from now to the console. LocalDateTime afterADayFromNow. currentDateTime.plusDays. As you can see, there are available the plusDays, plusMonth, plusYears, plusHours, plusMinutes, plusSeconds, or plusNano methods. For now, we use the plusDays method, and I'll assign the 1 because we want to learn the time after one day from now. Now let's print this on the console. S out "afterADayFromNow" + afterADayFromNow. And let's run it. As you can see, the time after one day from the current time is printed on the console.
Now, let's print one month ago to the console. I'm copying and pasting these two lines. Let the name of the object be beforeAMonthFromNow. And, the method will use this time will be minus months. It's enough to write once the parameter, because we want to know the time from one month ago. This will be beforeAMonthFromNow and here too. Let's run and see. As you can see, the time before one month from the current time is printed on the console. We also said that the LocalDateTime class is immutable. To see this, let's print the currentDateTime 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 currentDateTime variable in the lines above. If the LocalDateTime class is mutable, the value of the current time variable should change. Otherwise, if it's immutable, the same result should be printed on the console. Let's test it and observe the result. As you can see, the value of the currentDateTime object has not changed. From this result, you can understand that the LocalDateTime class is also immutable, like the LocalDate and LocalTime classes, but we can make changes by passing a new value to the currentDateTime object.
For example, let's transfer the year two years ago to the currentDateTime object. The currentDateTime = currentDateTime. minusYears(2). Now let's print the currentDateTime to the console once again and test it. As you can see, the currentTime object now references the two years ago from now. This value will now be valid for transactions after this line. Yes, I think the concept of immutable is also understood for the LocalDateTime class. Of course if we want, we can also transfer a time that we have determined to a LocalDateTime object. For example, LocalDateTime myWeddingDateTime = LocalDateTime.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 year will be 2014, the month will be month.AUGUST, the day will be 14, the hour will be 15, the minute will be 30. Finally, the second can be 0. Now, let's print this to the console. S out "myWeddingDateTime" + myWeddingDateTime. Let's run it and test it. As you can see, we can easily print a specific time, but the second was not printed to the console because it was 0. If we write a non-zero value here, this time the second will also be printed to the console. Let's change this to 1 and run it again. As you can see, this time the second is printed on the console. Okay. We can also compare two different times with the LocalDateTime class.
In other words, it's very easy to find out which of the two times is before and which is after with the LocalDateTime class, because it has a readymade method that provides this. For example, let's compare myWeddingDateTime with the currentDateTime. For this, we will use the is after and is before methods of the LocalDateTime class. These methods return true or false. Let's test it now. S out "is myWeddingDateTime before ? " + myWeddingDateTime. isBefore(currentDateTime). Let's run the code. As you can see, the result is printed as true because myWeddingDateTime is earlier than the currentDateTime.
Yes, I think the LocalDateTime class is understood. The usage of methods is very similar to the LocalDate and LocalTime classes. Let's take a short break here. See you in the next lesson.
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.