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 this lesson, we'll talk about the LocalDate class. With Java 8, the use of date and time in Java has changed a bit. Before the Java 8, Date and Calendar classes were used which were included in the java.util package. After Java 8, different classes such as LocalDate, LocalTime, LocalDateTime, Period, etc., were presented to us in the java.time package. We can still use the old classes, but thanks to the classes in the java.time package, it's possible to do the necessary options with less code. If you're ready, let's start with a LocalDate. The LocalDate class is included in the java.time package. It represents a year, month, day in the ISO calendar and is useful for representing a date without a time. It can be used to represent a date only information, such as birth date or wedding date. Also, the LocalDate class is an immutable class. We learned what the concept of immutable is in our previous lessons. If you remember, the String class was also immutable. So, once created, its value was always the same unless transferred to a new object. The LocalDate class is also an immutable class.
When we create an object of this class and assign a date to it, the value of that object always stays the same until it's assigned to a different object. And the LocalDate class is thread-safe. The means of the thread-safe is that a method or class instance can be used by multiple threads at the same time without any problems occurring. Yes, after this information, let's move on to Eclipse and get some practice. First, I will create a new project. I click on the File menu and select the New Java Project options. The project name can be DateTimeExample. The Java version will be Java 8 or Java 1.8, and I click the 'Finish' button. Now, I will create a new class in this package. I right-click on the source folder and select the New Class options. The package name can be datetimeexample. And the class name can be LocalDateExample. Lastly, I'll check the check box for the main method and click the 'Finish' button to create this class. Okay, first let's print the current date to the console. I will create a new object from the LocalDate class. We use the LocalDate class in the java.time package. The name of the object can be currentDate.
After the equal sign, I write LocalDate.now. First, let's go to the LocalDate class by pressing the control key and clicking on it. As you can see, the LocalDate class doesn't have any constructor and it's a final class. Also, all methods of this class are static. For this reason, when we use the LocalDate class, we can use the methods of this class directly and we don't use the constructor parentheses. And if you use the new LocalDate here, you get a compilation error. It says, "the constructor LocalDate is undefined." This is important, don't forget this. Now, let's print the current date on the console. System.out.print("current date:" + currentDate). As you can see, the current date is printed on the console. While I was recording this video, the date was the 18th of June in the year 2022. We can also easily jump to a certain day after a specific date, or a month, or a year later or before. For this, we use the plus and minus methods of the LocalDate class. For example, let's print 10 days from now to the console. Local date after10DaysFromNow = currentDate.plusDays(). As you can see, there are available the plus day, plus week, plus month, or plus week methods. For now, we use the plusDays method.
And I will assign the 10 because we want to learn the day after 10 days from now. Now, let's print this on the console. System.out.print("after10DaysFromNow: " + after10DaysFromNow). And let's run it. As you can see, the date after 10 days from today, i.e., 28th of the month, is printed on the console. Now, let's print one week ago to the console. I'm copying and pasting these two lines. Let the name of the object be beforeOneWeekFromNow and the method we'll use this time will be minusWeeks. It's enough to write one as the parameter because we want to know the date from a week ago. This will be before one week from now and here too. Let's run and see. As you can see, the date before one week from today, i.e., 11th of the month, is printed on the console. We also said that the LocalDate class is immutable. To see this, let's print the currentDate object here once more to the console. I'll copy this line and paste it here. If you notice, we have jumped to certain dates by using the currentDate variable in the lines above. If the LocalDate class is mutable, the value of the currentDate variable should change.
Otherwise, if it's 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 currentDate object has not changed. From this result, you can understand that the LocalDate class is immutable. But we can make changes by passing a new value to the currentDate object. For example, let's transfer the date three days later to the currentDate object. The current date = currentDate.plusDays(3). Now, let's print the currentDate to the console once again and test it. As you can see, the currentDate object now references the 21st of the month. 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 date that we have determined to a LocalDate object. For example, let me assign my birthday. LocalDate myBirthday = LocalDate.of. Thanks to the method of, we can store a specific date and use it in our code. Now, let's give the parameters of this method. The year will be 1990, the month will be Month.FEBRUARY. Notice that the month class is of the enum type.
We have learned this before. Finally, it will be day one. Now, let's print this to the console. System.out.print("my birthday:" + myBirthday). Let's run it and test it. As you can see, we can easily print a specific date. We can also compare two different dates with the LocalDate class. In other words, it's very easy to find out which of the two dates is before and which is after with the LocalDate class because it has a ready made method that provides this. For example, let's compare my birthday with the current day. For this, we will use the isAfter and isBefore methods of the LocalDate class. These methods return true or false. Let's test it now. System.out.print("is my birthday before?" + myBirthday.isBefore(currentDate)). Let's run our code. As you can see, the result is printed as true because my birthday is earlier than the current date. Before concluding the lesson, I'd also like to show you the use of dates before the Java 8. Before Java 8, the Date class and Calendar class in the java.util package were used. For example, let's print the current date in the old way. Note that I am using Date from the java.util package. myCurrentDate = new Date().
Let's print it on the console. System.out.println ("date before Java 8:" + myCurrentDate). Let's run it and test it. Notice that both date and time are printed in a mixed way. Now, let's print a specific date to the console. We use the Calendar class for this. Calendar myCalendar = calendar.getInstance(). Then, we could reach the desired date with the set method of the Calendar class. myCalendar.set. The year will be 1990. The month will be Calendar.FEBRUARY. Finally, it will be day one. Now, let's pass it to a new variable. Date birthDay = myCalendar.getTime(). Finally, let's print the value of the birthDay object to the console. System.out.print("my birthday before Java 8:" + birthDay). Let's run the code and test it. As you can see, it's still possible to jump to a specific date. But notice that this time, we wrote more code and used both Date and Calendar classes. However, we can do this very easily and quickly by using the of method of the LocalDate class. Yes, I think the LocalDate class is understood. 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.