concept LocalTime in category java

appears as: LocalTime, LocalTime
Modern Java in Action: Lambdas, streams, reactive and functional programming

This is an excerpt from Manning's book Modern Java in Action: Lambdas, streams, reactive and functional programming.

12.1. LocalDate, LocalTime, LocalDateTime, Instant, Duration, and Period

We start by exploring how to create simple dates and intervals. The java.time package includes many new classes to help you: LocalDate, LocalTime, LocalDateTime, Instant, Duration, and Period.

Similarly, the time of day, such as 13:45:20, is represented by the LocalTime class. You can create instances of LocalTime by using two overloaded static factory methods named of. The first one accepts an hour and a minute, and the second one also accepts a second. Like the LocalDate class, the LocalTime class provides some getter methods to access its values, as shown in the following listing.

Listing 12.3. Creating a LocalTime and reading its values
LocalTime time = LocalTime.of(13, 45, 20);     #1
int hour = time.getHour();                     #2
int minute = time.getMinute();                 #3
int second = time.getSecond();                 #4

You can create both LocalDate and LocalTime by parsing a String representing them. To achieve this task, use their parse static methods:

LocalDate date = LocalDate.parse("2017-09-21");
LocalTime time = LocalTime.parse("13:45:20");

It’s possible to pass a DateTimeFormatter to the parse method. An instance of this class specifies how to format a date and/or a time object. It’s intended to be a replacement for the old java.util.DateFormat that we mentioned earlier. We show in more detail how you can use a DateTimeFormatter in section 12.2.2. Also note that both these parse methods throw a DateTimeParseException, which extends RuntimeException in case the String argument can’t be parsed as a valid LocalDate or LocalTime.

OCA Java SE 8 Programmer I Certification Guide

This is an excerpt from Manning's book OCA Java SE 8 Programmer I Certification Guide.

To store times like breakfast, conference talk start time, or in-store sale end time, you can use LocalTime. It stores time in the format hours-minutes-seconds (without a time zone) and to nanosecond precision. So you’re sure to see methods that accept nanoseconds as method arguments or methods that return this value. Like LocalDate, LocalTime is also immutable and hence safe to be used in a multithreaded environment.

You can use the instance methods minusHours(), minusMinutes(), minusSeconds(), and minusNanos() to create and return a copy of LocalTime instances with the specified period subtracted. The method names are self-explanatory. For example, minus-Hours(int) returns a copy of a LocalTime instance with the specified period in hours subtracted. The following example calculates and outputs the time when Shreya should leave from her office to watch a movie, given that the movie starts at 21:00 hours and it takes 35 minutes to commute to the movie theater:

LocalTime movieStartTime = LocalTime.parse("21:00:00");
int commuteMin = 35;
LocalTime shreyaStartTime = movieStartTime.minusMinutes(commuteMin);
System.out.println("Start by " + shreyaStartTime + " from office");
Java 8 in Action: Lambdas, streams, and functional-style programming

This is an excerpt from Manning's book Java 8 in Action: Lambdas, streams, and functional-style programming.

12.1. LocalDate, LocalTime, Instant, Duration, and Period

Let’s start by exploring how to create simple dates and intervals. The java.time package includes many new classes to help you: LocalDate, LocalTime, LocalDateTime, Instant, Duration, and Period.

Similarly, the time of day, such as 13:45:20, is represented by the LocalTime class. You can create instances of LocalTime using two overloaded static factory methods named of. The first one accepts an hour and a minute and the second one also accepts a second. Just like the LocalDate class, the LocalTime class provides some getter methods to access its values, as shown in the following listing.

Listing 12.3. Creating a LocalTime and reading its values
LocalTime time = LocalTime.of(13, 45, 20); #1 int hour = time.getHour(); #2 int minute = time.getMinute(); #3 int second = time.getSecond(); #4 #1: 13:45:20 #2: 13 #3: 45 #4: 20

Both LocalDate and LocalTime can be created by parsing a String representing them. You can achieve this using their parse static methods:

LocalDate date = LocalDate.parse("2014-03-18");
LocalTime time = LocalTime.parse("13:45:20");

It’s possible to pass a DateTimeFormatter to the parse method. An instance of this class specifies how to format a date and/or a time object. It’s intended as a replacement for the old java.util.DateFormat that we mentioned earlier. We show in more detail how you can use a DateTimeFormatter in section 12.2. Also note that these parse methods both throw a DateTimeParseException, which extends RuntimeException in case the String argument can’t be parsed as a valid LocalDate or LocalTime.