The snippet library.
Small, practical examples. Search by task or narrow things down below.
Check whether text contains a phrase
Use a case-sensitive Java substring check.
Java✓ Tested
Choose between two branches
Java: Use if and else to handle an available or empty stock count.
Java✓ Tested
Combine text with a name
Java: Construct a greeting from a fixed prefix and a stored name.
Java✓ Tested
Compare a value with a limit
Java: Turn a comparison into a readable yes or no result.
Java✓ Tested
Compare floating-point results with a tolerance
Java: For this bounded example, compare the absolute difference against a chosen error threshold.
Java✓ Tested
Convert an instant to a regional timezone
Java: Use ZonedDateTime with a named zone to apply that zone’s offset rules.
Java✓ Tested
Convert bytes to hexadecimal and back
Java: Use HexFormat for a standard-library representation of raw bytes.
Java✓ Tested
Convert epoch seconds into an instant
Java: Instant represents a UTC timeline point independent of the default timezone.
Java✓ Tested
Convert known numeric text to an integer
Java: Convert a trusted literal to an integer before adding. Validate external input before converting it.
Java✓ Tested
Copy a file without replacing an existing destination
Java: Files.copy fails if the destination already exists unless replacement is explicitly enabled.
Java✓ Tested
Count days between two dates
Java: Use ChronoUnit.DAYS for an elapsed count rather than a Period day component.
Java✓ Tested
Count the items in a collection
Java: Read the collection’s length instead of hard-coding the loop limit.
Java✓ Tested