REGEXVAULTv2.0
Localization/Time Formats
Verified Safe

Time with Fractional Seconds and Timezone Regex for Java

/^(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(?:\.(\d{1,9}))?(?:Z|([+-])(2[0-3]|[01][0-9]):([0-5][0-9]))?$/

What this pattern does

This page provides a comprehensive, battle-tested regular expression for matching time with fractional seconds and timezone, ported and verified for Java. A rigorously tested regex reduces debugging time and protects your application from edge-case failures. The snippet below is ready to drop into your Java project — whether you're validating in a Spring Boot controller, a Jakarta EE service, or a standalone utility class.

Java Implementation

Java
// Time with Fractional Seconds and Timezone
// ReDoS-safe | RegexVault — Localization > Time Formats

import java.util.regex.Pattern;

public class TimeWithFractionalSecondsAndTimezoneValidator {
    private static final Pattern PATTERN =
        Pattern.compile("^(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(?:\\.(\\d{1,9}))?(?:Z|([+-])(2[0-3]|[01][0-9]):([0-5][0-9]))?$");

    public static boolean validate(String input) {
        return PATTERN.matcher(input).matches();
    }

    // Example
    public static void main(String[] args) {
        System.out.println(validate("12:30:45")); // true
    }
}

Test Cases

Matches (Valid)
Rejects (Invalid)
12:30:4524:00:00Z
12:30:45Z12:60:00Z
12:30:45.12312:30:45+25:00
12:30:45.123456789Z12:30:45.1234567890
12:30:45+08:00
00:00:00-05:30

When to use this pattern

This pattern is drawn from the Localization > Time Formats category and carries a ReDoS-safe certification. That matters for Java developers because critical in Java applications since the JVM regex engine uses backtracking and is susceptible to ReDoS without careful pattern design. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.

Common Pitfalls

India Standard Time (+05:30), Newfoundland (-03:30), Iran (+03:30), and Australia Central (+09:30) use 30-minute offsets. Nepal (+05:45) uses a 45-minute offset. Pattern allows all combinations.

Technical Notes

Capture groups: 1=hour, 2=minute, 3=second, 4=fractional, 5=tz sign, 6=tz hour, 7=tz minute. Z = UTC. Fractional seconds up to 9 places (nanoseconds). Half-hour timezones (+05:30 India, +09:30 Australia) are supported.

Have a pattern that belongs in the vault?

Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.

Submit a Pattern