Developer Tools for Random Data Generation // v2.6.1
root@generate-random:~/times/java$ _

Generate Random Times in Java

Complete code tutorial with examples and best practices

[ Code Example - Quick Summary ]

Language: Java

What: Generate random times in Java using LocalTime and Random. Covers 12-hour, 24-hour formats, and time validation for testing.

Try it: Use our interactive Times generator or integrate this code into your Java application.

Generate random times in Java using LocalTime and Random. Covers 12-hour, 24-hour formats, and time validation for testing. Looking for other languages? Check our code examples in PHP , JavaScript , Python , C# , C++ , Ruby and Go or use our interactive web generator.

Java Code Example

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class TimeGenerator {
    private static final Random random = new Random();

    // Generate random time (24-hour format HH:MM:SS)
    public static LocalTime randomTime24() {
        int hour = random.nextInt(24);
        int minute = random.nextInt(60);
        int second = random.nextInt(60);
        return LocalTime.of(hour, minute, second);
    }

    // Generate random time as formatted string
    public static String randomTimeString() {
        LocalTime time = randomTime24();
        return time.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
    }

    // Generate random time (12-hour format with AM/PM)
    public static String randomTime12() {
        int hour = random.nextInt(12) + 1;
        int minute = random.nextInt(60);
        String ampm = random.nextBoolean() ? "AM" : "PM";
        return String.format("%02d:%02d %s", hour, minute, ampm);
    }

    // Generate random time within business hours (9 AM - 5 PM)
    public static LocalTime randomBusinessTime() {
        int hour = random.nextInt(9) + 9; // 9-17
        int minute = random.nextInt(60);
        return LocalTime.of(hour, minute);
    }

    // Validate time string (HH:MM:SS)
    public static boolean isValidTime(String time) {
        try {
            LocalTime.parse(time, DateTimeFormatter.ofPattern("HH:mm:ss"));
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    // Generate multiple random times
    public static List<LocalTime> generateTimes(int count) {
        List<LocalTime> times = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            times.add(randomTime24());
        }
        return times;
    }

    public static void main(String[] args) {
        System.out.println("24-hour: " + randomTimeString());
        System.out.println("12-hour: " + randomTime12());
        System.out.println("Business: " + randomBusinessTime());
        System.out.println("Valid: " + isValidTime("14:30:45"));
        System.out.println("Batch: " + generateTimes(3));
    }
}

[EXPLANATION]

Java uses LocalTime.of(hour, minute, second) to create time objects. Random.nextInt(bound) generates hours (0-23), minutes (0-59), seconds (0-59). DateTimeFormatter formats output. String.format("%02d") adds leading zeros. LocalTime.parse() validates time strings. Java 8+ time API is preferred over legacy Date. LocalTime is immutable and thread-safe.

Expected Output

24-hour: 14:23:45
12-hour: 09:15 AM
Business: 11:30
Valid: true
Batch: [08:45:12, 16:20:33, 22:10:05]

Common Use Cases

  • Generate times for Spring Boot scheduling tests
  • Create time fields for JPA entity testing
  • Generate appointment times for booking systems
  • Test time validation in enterprise applications
  • Create working hours for employee management

Important Notes

  • LocalTime represents time without date or timezone
  • DateTimeFormatter.ofPattern("HH:mm:ss") for 24-hour format
  • Random.nextBoolean() returns true/false for AM/PM
  • LocalTime is immutable and thread-safe
  • Use LocalTime.now() for current time

Try Our Interactive Generator

Don't want to write code? Use our free web-based Times generator with instant results.

TRY TIMES GENERATOR →