Java LocalTime API Guide

The LocalTime class in Java 8 represents a time without a date and without a time-zone in the ISO-8601 calendar system, such as 10:15:30. This guide covers the common methods provided by the LocalTime class, demonstrating how to create, manipulate, and query time objects.

1. now()

Obtains the current time from the system clock in the default time-zone.

Example:

import java.time.LocalTime;

public class LocalTimeExample {
    public static void main(String[] args) {
        LocalTime currentTime = LocalTime.now();
        System.out.println("Current Time: " + currentTime);
    }
}

Output:

Current Time: 14:02:10.009808

2. of(int hour, int minute)

Obtains an instance of LocalTime from an hour and minute.

Example:

import java.time.LocalTime;

public class LocalTimeExample {
    public static void main(String[] args) {
        LocalTime time = LocalTime.of(10, 15);
        System.out.println("Specific Time: " + time);
    }
}

Output:

Specific Time: 10:15

3. of(int hour, int minute, int second)

Obtains an instance of LocalTime from an hour, minute, and second.

Example:

import java.time.LocalTime;

public class LocalTimeExample {
    public static void main(String[] args) {
        LocalTime time = LocalTime.of(10, 15, 30);
        System.out.println("Specific Time: " + time);
    }
}

Output:

Specific Time: 10:15:30

4. parse(CharSequence text)

Obtains an instance of LocalTime from a text string such as 10:15:30.

Example:

import java.time.LocalTime;

public class LocalTimeExample {
    public static void main(String[] args) {
        LocalTime time = LocalTime.parse("10:15:30");
        System.out.println("Parsed Time: " + time);
    }
}

Output:

Parsed Time: 10:15:30

5. plusHours(long hours)

Returns a copy of this LocalTime with the specified number of hours added.

Example:

import java.time.LocalTime;

public class LocalTimeExample {
    public static void main(String[] args) {
        LocalTime time = LocalTime.now();
        LocalTime newTime = time.plusHours(2);
        System.out.println("Time plus 2 hours: " + newTime);
    }
}

Output:

Time plus 2 hours: 16:02:10.383270

6. plusMinutes(long minutes)

Returns a copy of this LocalTime with the specified number of minutes added.

Example:

import java.time.LocalTime;

public class LocalTimeExample {
    public static void main(String[] args) {
        LocalTime time = LocalTime.now();
        LocalTime newTime = time.plusMinutes(30);
        System.out.println("Time plus 30 minutes: " + newTime);
    }
}

Output:

Time plus 30 minutes: 14:32:10.485785

7. plusSeconds(long seconds)

Returns a copy of this LocalTime with the specified number of seconds added.

Example:

import java.time.LocalTime;

public class LocalTimeExample {
    public static void main(String[] args) {
        LocalTime time = LocalTime.now();
        LocalTime newTime = time.plusSeconds(45);
        System.out.println("Time plus 45 seconds: " + newTime);
    }
}

Output:

Time plus 45 seconds: 14:02:55.589370

8. minusHours(long hours)

Returns a copy of this LocalTime with the specified number of hours subtracted.

Example:

import java.time.LocalTime;

public class LocalTimeExample {
    public static void main(String[] args) {
        LocalTime time = LocalTime.now();
        LocalTime newTime = time.minusHours(1);
        System.out.println("Time minus 1 hour: " + newTime);
    }
}

Output:

Time minus 1 hour: 13:02:10.698581

9. minusMinutes(long minutes)

Returns a copy of this LocalTime with the specified number of minutes subtracted.

Example:

import java.time.LocalTime;

public class LocalTimeExample {
    public static void main(String[] args) {
        LocalTime time = LocalTime.now();
        LocalTime newTime = time.minusMinutes(15);
        System.out.println("Time minus 15 minutes: " + newTime);
    }
}

Output:

Time minus 15 minutes: 13:47:10.815675

10. minusSeconds(long seconds)

Returns a copy of this LocalTime with the specified number of seconds subtracted.

Example:

import java.time.LocalTime;

public class LocalTimeExample {
    public static void main(String[] args) {
        LocalTime time = LocalTime.now();
        LocalTime newTime = time.minusSeconds(30);
        System.out.println("Time minus 30 seconds: " + newTime);
    }
}

Output:

Time minus 30 seconds: 14:01:40.941092

11. isAfter(LocalTime other)

Checks if this time is after the specified time.

Example:

import java.time.LocalTime;

public class LocalTimeExample {
    public static void main(String[] args) {
        LocalTime time1 = LocalTime.now();
        LocalTime time2 = time1.minusHours(1);
        boolean isAfter = time1.isAfter(time2);
        System.out.println("Is after: " + isAfter);
    }
}

Output:

Is after: true

12. isBefore(LocalTime other)

Checks if this time is before the specified time.

Example:

import java.time.LocalTime;

public class LocalTimeExample {
    public static void main(String[] args) {
        LocalTime time1 = LocalTime.now();
        LocalTime time2 = time1.plusHours(1);
        boolean isBefore = time1.isBefore(time2);
        System.out.println("Is before: " + isBefore);
    }
}

Output:

Is before: true

13. isEqual(LocalTime other)

Checks if this time is equal to the specified time.

Example:

import java.time.LocalTime;

public class LocalTimeExample {
    public static void main(String[] args) {
        LocalTime time1 = LocalTime.now();
        LocalTime time2 = LocalTime.from(time1);
        boolean isEqual = time1.isEqual(time2);
        System.out.println("Is equal: " + isEqual);
    }
}

14. toString()

Outputs this time as a String, such as 10:15:30.

Example:

import java.time.LocalTime;

public class LocalTimeExample {
    public static void main(String[] args) {
        LocalTime time = LocalTime.now();
        System.out.println("Time: " + time.toString());
    }
}

Output:

Time: 14:02:11.278289

15. getHour()

Gets the hour-of-day field.

Example:

import java.time.LocalTime;

public class LocalTimeExample {
    public static void main(String[] args) {
        LocalTime time = LocalTime.now();
        int hour = time.getHour();
        System.out.println("Hour: " + hour);
    }
}

Output:

Hour: 14

16. getMinute()

Gets the minute-of-hour field.

Example:

import java.time.LocalTime;

public class LocalTimeExample {
    public static void main(String[] args) {
        LocalTime time = LocalTime.now();
        int minute = time.getMinute();
        System.out.println("Minute: " + minute);
    }
}

Output:

Minute: 2

17. getSecond()

Gets the second-of-minute field.

Example:

import java.time.LocalTime;

public class LocalTimeExample {
    public static void main(String[] args) {
        LocalTime time = LocalTime.now();
        int second = time.getSecond();
        System.out.println("Second: " + second);
    }
}

Output:

Second: 11

18. getNano()

Gets the nano-of-second field.

Example:

import java.time.LocalTime;

public class LocalTimeExample {
    public static void main(String[] args) {
        LocalTime time = LocalTime.now();
        int nano = time.getNano();
        System.out.println("Nano: " + nano);
    }
}

Output:

Nano: 637659000

19. truncatedTo(TemporalUnit unit)

Returns

a copy of this LocalTime with the time truncated to the specified unit.

Example:

import java.time.LocalTime;
import java.time.temporal.ChronoUnit;

public class LocalTimeExample {
    public static void main(String[] args) {
        LocalTime time = LocalTime.now();
        LocalTime truncatedTime = time.truncatedTo(ChronoUnit.MINUTES);
        System.out.println("Truncated Time: " + truncatedTime);
    }
}

Output:

Truncated Time: 14:02

20. toSecondOfDay()

Extracts the time as seconds of day, from 0 to 24 * 60 * 60 - 1.

Example:

import java.time.LocalTime;

public class LocalTimeExample {
    public static void main(String[] args) {
        LocalTime time = LocalTime.now();
        int secondsOfDay = time.toSecondOfDay();
        System.out.println("Seconds of the day: " + secondsOfDay);
    }
}

Output:

Seconds of the day: 50531

21. toNanoOfDay()

Extracts the time as nanos of day, from 0 to 24 * 60 * 60 * 1,000,000,000 - 1.

Example:

import java.time.LocalTime;

public class LocalTimeExample {
    public static void main(String[] args) {
        LocalTime time = LocalTime.now();
        long nanosOfDay = time.toNanoOfDay();
        System.out.println("Nanos of the day: " + nanosOfDay);
    }
}

Output:

Nanos of the day: 50531911730000

This guide covers the common methods provided by the LocalTime class in Java 8, demonstrating how to create, manipulate, and query time objects. Each method is illustrated with working examples to help you understand how to use them effectively in your applications.

Comments