GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • java.evolved
  • ←
  • β†’

This view

  • Original page
  • New address

GeezerVison is showing javaevolved.dev β€” HexFormat | java.evolved

Home / Date/Time / HexFormat
Date/Time Intermediate

HexFormat

  • πŸ› Report a code issue
  • 🌐 Report a translation issue
  • πŸ’‘ Suggest a new pattern

Convert between hex strings and byte arrays with HexFormat.

Strings Formatting
Code Comparison
βœ• Java 8
// Pad to 2 digits, uppercase
String hex = String.format(
    "%02X", byteValue);
// Parse hex string
int val = Integer.parseInt(
    "FF", 16);
βœ“ Java 17+
var hex = HexFormat.of()
    .withUpperCase();
String s = hex.toHexDigits(
    byteValue);
byte[] bytes =
    hex.parseHex("48656C6C6F");
Share 𝕏
See a problem with this code? Let us know.
Why the modern way wins
πŸ“

Bidirectional

Convert bytes→hex and hex→bytes with one API.

πŸ”§

Configurable

Delimiters, prefix, suffix, upper/lower case.

πŸ“¦

Array support

Encode/decode entire byte arrays at once.

Old Approach
Manual Hex Conversion
Modern Approach
HexFormat
Since JDK
17
Difficulty
Intermediate
JDK Support
HexFormat
Available

Widely available since JDK 17 LTS (Sept 2021)

How it works

HexFormat provides bidirectional hex encoding/decoding for bytes, ints, and arrays. Configure delimiters, prefix, suffix, and case. No more manual formatting or parsing.

Related Documentation
HexFormat β†—
Proof
View proof source β†—
Related patterns
Date/Time Beginner

Date formatting

Pre-Java 8
// Not thread-safe!
SimpleDateFormat sdf =
    new SimpleDateFormat("yyyy-MM-dd");
String formatted = sdf.format(date);
// Must synchronize for concurrent use
Java 8+
DateTimeFormatter fmt =
    DateTimeFormatter.ofPattern(
        "uuuu-MM-dd");
String formatted =
    LocalDate.now().format(fmt);
// Thread-safe, immutable
Hover to see modern ➜
JDK 8+ β†’
Date/Time Intermediate

Instant with nanosecond precision

Java 8
// Millisecond precision only
long millis =
    System.currentTimeMillis();
// 1708012345678
Java 9+
// Microsecond/nanosecond precision
Instant now = Instant.now();
// 2025-02-15T20:12:25.678901234Z
long nanos = now.getNano();
Hover to see modern ➜
JDK 9+ β†’
Date/Time Beginner

Duration and Period

Pre-Java 8
// How many days between two dates?
long diff = date2.getTime()
    - date1.getTime();
long days = diff
    / (1000 * 60 * 60 * 24);
// ignores DST, leap seconds
Java 8+
long days = ChronoUnit.DAYS
    .between(date1, date2);
Period period = Period.between(
    date1, date2);
Duration elapsed = Duration.between(
    time1, time2);
Hover to see modern ➜
JDK 8+ β†’
You are visitor 01402503 β€” GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800Γ—600. No cookies required.