Learn Java Easy. Chapter 10. Working with Date and Time

Chapter 10. Working with Date and Time

Chapter 10. Working with Date and Time

My part is already done. After creating the code for the programs Pedro needs, I’ve hidden them inside a system folder. It didn’t take me even half an hour to do it. I don’t need the computer anymore, so I’ll go read a bit.

I’m quite relieved because, now that this part of the plan works out, it doesn’t depend on me. I just have to wait and see what happens. Even the risk of getting caught is minimal. They could only discover me if Pedro or Bud reported me, but I trust them.

I’ve been informed that Chani’s cousin will see me again this afternoon, so I’ll tell him that I’m going out soon. My trip will be short, a few hours, but I don’t know exactly which day yet. Therefore, we’ll need to stay in contact.

Tomorrow afternoon, Pedro will come to the library and, in less than ten minutes, he’ll have to use my programs to create the distraction device. Before the alarm starts ringing, he’ll have a few minutes to leave the library and return to the cell. That way, no one will suspect him.

Once the alarm sounds, the prison officials will devote all their effort to turning it off and checking that the security system remains active. Bud will use that time to manage to open the cell and cross the first corridor, where there shouldn’t be any guards. Although he’ll have to be careful with the cameras.

In the second corridor, there’s a guard who never leaves his post, but he does circular patrols. So, knowing his surveillance pattern, Bud will have to avoid him to prevent being seen. Remember that when they manage to deactivate the alarm, the lights will go out. That will give Bud even more time. In theory, it will be enough time for him to enter the office unseen.

The riskiest part of the entire plan will be leaving the office. Bud will have to control the guards’ surveillance patterns to leave exactly at the right moment. He’ll also have to consider the camera patterns, exactly the same thing he did to reach the office.

To carry out this complicated plan, Bud is going to have to use some Java concepts that I haven’t learned yet, such as working with date and time. I won’t need to know how to do that, but I’d like to learn it anyway.

I’ve asked Bud to teach me, and he’s delighted. Maybe in the end, he’s not as calm as he seems, and perhaps he wants to keep his mind occupied. Whatever the case, I appreciate it. I like learning new things, and honestly, it also helps to keep my mind busy.

Theory for Working with Dates and Times in Java

The theoretical explanation in this topic will be a bit different from the previous ones. Quite more summarized. The first reason is that I don’t think it’s necessary to mention the importance that date and time have in any application or program. We all have a mobile phone or a computer, and we know that many functions of our devices don’t even work without the correct time data.

The second reason is that, by now, you’re already capable of understanding and writing a large part of the code used in Java. You understand, among other things, what variables are, the main structures, and the order of operation of programs. Therefore, this part will be a piece of cake for you.

The third reason is that the syntax of this topic is not complex at all. It can’t even be said to be a structure. We’ll simply use one line or another of code depending exactly on the type of date or time we need to obtain.

The LocalTime, LocalDate, and LocalDateTime classes

  • LocalTime represents only the time of day, without including any information about the date or time zone.
  • LocalDate represents only a date, without including any information about the time or time zone.
  • LocalDateTime combines the information of LocalDate (date) and LocalTime (time), representing both the date and the time, but without including information about the time zone.

We import them as follows:

  • import java.time.LocalTime;
  • import java.time.LocalDate;
  • import java.time.LocalDateTime;

Now, I’m going to show you, point by point, the code you’ll need to use depending on each specific situation.

–          Get the current date

To get the current date in Java, the LocalDate class is used. This class represents a date without time, in ISO format (yyyy-mm-dd).

LocalDate currentDate = LocalDate.now();

–          Get the current time

To get the current time, the LocalTime class is used, which represents a time without date in hh:mm format.

LocalTime currentTime = LocalTime.now();

–          Get the current date and time

The LocalDateTime class combines LocalDate and LocalTime to represent a date and a time in the same object.

LocalDateTime currentDateTime = LocalDateTime.now();

–          Create a specific date

You can create a specific date using the of method of the LocalDate class, which requires year, month, and day as parameters.

LocalDate date = LocalDate.of(2024, 5, 29);

–          Create a specific time

Similarly, LocalTime also has an of method to create a specific time, using hours, minutes, and seconds as parameters.

LocalTime time = LocalTime.of(14, 30, 45);

–          Add days to a date

You can add days to a date using the plusDays method of the LocalDate class.

LocalDate newDate = currentDate.plusDays(10); // Ten are the days added in this example

–          Subtract days from a date

LocalDate newDate = currentDate.minusDays(10); // Ten are the days subtracted

–          Add hours to a time

LocalTime newTime = currentTime.plusHours(3); // 3 are the hours added in this example

–          Subtract hours from a time

To subtract hours from a time, the minusHours method of the LocalTime class is used.

LocalTime newTime = currentTime.minusHours(3); // 3 are the hours subtracted in this example

–          Compare two dates

To compare two dates, LocalDate provides methods such as isAfter, isBefore, and isEqual. boolean isAfter = date1.isAfter(date2);

boolean isBefore = date1.isBefore(date2); boolean isEqual = date1.isEqual(date2);

–          Format a date

To format a date in a specific format, the DateTimeFormatter class is used. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(“dd/MM/yyyy”);

String formattedDate = date.format(formatter);

–          Get the difference between two dates

To calculate the difference between two dates, the Period class is used. Period difference = Period.between(startDate, endDate);

Summary of What We’ve Learned

In the Java programming language, if you need to get the current date, you can use the LocalDate class, while to get the current time, the LocalTime class is used. When it’s necessary to handle both date and time together, the LocalDateTime class is the appropriate option. To set a specific date, you can use the of method of LocalDate. Similarly, to define a specific time, the of method of LocalTime is used.

The plusDays and minusDays methods of LocalDate allow you to add or subtract days to a date, respectively. Likewise, with the plusHours and minusHours methods of LocalTime, you can add or subtract hours to a specific time.

The LocalDate class provides methods such as isAfter, isBefore, and isEqual to compare two dates. To format a date in a specific style, the DateTimeFormatter class is used. Finally, to determine the difference between two dates, the Period class is used.

Did you like it? Don’t keep it to yourself — share it like juicy gossip! 😏