LocalTime

LocalTime Class

The LocalTime class represents a time of day without a date and time zone. It provides methods for creating, manipulating, and comparing time values.

var localtime = LocalTime.of(15, 30); // LTIME 15:30

Static Methods

now()

  • Returns the current time.

  • Example Usage:

    var currentTime = LocalTime.now();

parse(text: String)

  • Parses the input text to create a LocalTime instance.

  • Example Usage:

    var parsedTime = LocalTime.parse("15:30:45");

of(hour: Integer, minute: Integer, second: Integer)

  • Creates a LocalTime instance with the specified hour, minute, and second values.

  • Example Usage:

    var customTime = LocalTime.of(15, 30, 45);

of(hour: Integer, minute: Integer, second: Integer, nanoOfSecond: Integer)

  • Creates a LocalTime instance with the specified hour, minute, second, and nanosecond values.

  • Example Usage:

    var customTimeWithNano = LocalTime.of(15, 30, 45, 500000000);

Getter Methods

getHour()

  • Gets the hour-of-day field.

  • Returns the hour-of-day, from 0 to 23.

  • Example Usage:

    var hour = customTime.getHour();

getMinute()

  • Gets the minute-of-hour field.

  • Returns the minute-of-hour, from 0 to 59.

  • Example Usage:

    var minute = customTime.getMinute();

getSecond()

  • Gets the second-of-minute field.

  • Returns the second-of-minute, from 0 to 59.

  • Example Usage:

    var second = customTime.getSecond();

getNano()

  • Gets the nano-of-second field.

  • Returns the nano-of-second, from 0 to 999,999,999.

  • Example Usage:

    var nano = customTime.getNano();

Time Manipulation Methods

plusHours(hoursToAdd: Long)

  • Adds the specified number of hours to the current time and returns a new LocalTime instance.

  • Example Usage:

    var newTime = customTime.plusHours(2);

plusMinutes(minutesToAdd: Long)

  • Adds the specified number of minutes to the current time and returns a new LocalTime instance.

  • Example Usage:

    var newTime = customTime.plusMinutes(30);

plusSeconds(secondsToAdd: Long)

  • Adds the specified number of seconds to the current time and returns a new LocalTime instance.

  • Example Usage:

    var newTime = customTime.plusSeconds(15);

plusNanos(nanosToAdd: Long)

  • Adds the specified number of nanoseconds to the current time and returns a new LocalTime instance.

  • Example Usage:

    var newTime = customTime.plusNanos(500000000);

minusHours(hoursToSubtract: Long)

  • Subtracts the specified number of hours from the current time and returns a new LocalTime instance.

  • Example Usage:

    var newTime = customTime.minusHours(1);

minusMinutes(minutesToSubtract: Long)

  • Subtracts the specified number of minutes from the current time and returns a new LocalTime instance.

  • Example Usage:

    var newTime = customTime.minusMinutes(10);

minusSeconds(secondsToSubtract: Long)

  • Subtracts the specified number of seconds from the current time and returns a new LocalTime instance.

  • Example Usage:

    var newTime = customTime.minusSeconds(30);

minusNanos(nanosToSubtract: Long)

  • Subtracts the specified number of nanoseconds from the current time and returns a new LocalTime instance.

  • Example Usage:

    var newTime = customTime.minusNanos(100000000);

Comparison Methods

isBefore(otherInstant: LocalTime)

  • Checks if the current time is before the specified LocalTime instance.

  • Returns true if the current time is before otherInstant, false otherwise.

  • Example Usage:

    var before = customTime.isBefore(parsedTime);

isAfter(otherInstant: LocalTime)

  • Checks if the current time is after the specified LocalTime instance.

  • Returns true if the current time is after otherInstant, false otherwise.

  • Example Usage:

    var after = customTime.isAfter(parsedTime);

Conversion Method

toString()

  • Converts the LocalTime to a string representation in the format "HH:mm:ss".

  • Example Usage:

    var timeString = customTime.toString();

Last updated