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

Chapter 10. Working with Date and Time

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

 

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 betrayed me, but I trust them.

 

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

 

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 his cell. This way, no one will suspect him.

 

Once the alarm goes off, the prison officers will dedicate all their effort to turning it off and checking that the security system is still active. Bud will use that time to manage to open the cell and cross the first corridor, where there shouldn’t be any guard. Although he’ll have to be careful with the cameras.

 

In the second corridor, there’s a guard who never leaves his post but patrols in circles. So, knowing his surveillance pattern, Bud will have to dodge him to avoid 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 monitor the guards’ surveillance patterns to exit at exactly the right moment. He’ll also need to take into account the camera patterns, exactly the same as he did to reach the office.

 

To carry out this complicated plan, Bud will have to use some Java concepts that I haven’t learned yet, like 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, truthfully, it’s also good for me 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. Much more summarized. The first reason is that I don’t think it’s necessary to mention the importance of date and time in any application or Program. We all have a mobile phone or computer and know that many functions of our devices don’t even work without correct time data.

 

The second reason is that, at this point, you’re already able to understand and write 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 called a structure. We’ll simply use one line of code or another 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 time or time zone.
  • LocalDateTime combines the information from LocalDate (date) and LocalTime (time), representing both date and time, but without including time zone information.

 

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, use the LocalDate class. 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, use the LocalTime class, 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 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 days are added in this example

 

 

  • Subtract days from a date

 

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

 

 

  • Add hours to a time

 

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

 

 

  • Subtract hours from a time

 

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

 

LocalTime nuevaHora = horaActual.minusHours(3); // 3 son las horas restadas en este ejemplo

 

 

  • 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 = datdate1.isEqual(date2);

 

 

  • Format a date

 

// To format a date in a specific format, use the DateTimeFormatter class.

 

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, use the Period class.

 

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 you need 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 from a date, respectively. Likewise, with the plusHours and minusHours methods of LocalTime, you can add or subtract hours from 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 employed.

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