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 — Date formatting | java.evolved

Home / Date/Time / Date formatting
Date/Time Beginner

Date formatting

  • 🐛 Report a code issue
  • 🌐 Report a translation issue
  • 💡 Suggest a new pattern

Format dates with thread-safe, immutable DateTimeFormatter.

Date & Time Formatting
Code Comparison
✕ 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
Share 𝕏
See a problem with this code? Let us know.
Why the modern way wins
🛡️

Thread-safe

Share formatters across threads without synchronization.

📋

Built-in formats

ISO_LOCAL_DATE, ISO_INSTANT, etc. for standard formats.

🔒

Immutable

Store as static final constants safely.

Old Approach
SimpleDateFormat
Modern Approach
DateTimeFormatter
Since JDK
8
Difficulty
Beginner
JDK Support
Date formatting
Available

Widely available since JDK 8 (March 2014)

How it works

DateTimeFormatter is immutable and thread-safe, unlike SimpleDateFormat. It can be stored as a constant and shared. Predefined formatters like ISO_LOCAL_DATE are available for common formats.

Related Documentation
DateTimeFormatter ↗
Proof
View proof source ↗
Related patterns
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

java.time API basics

Pre-Java 8
// Mutable, confusing, zero-indexed months
Calendar cal = Calendar.getInstance();
cal.set(2025, 0, 15); // January = 0!
Date date = cal.getTime();
// not thread-safe
Java 8+
LocalDate date = LocalDate.of(
    2025, Month.JANUARY, 15);
LocalTime time = LocalTime.of(14, 30);
Instant now = Instant.now();
// immutable, thread-safe
Hover to see modern ➜
JDK 8+ →
Date/Time Intermediate

HexFormat

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");
Hover to see modern ➜
JDK 17+ →
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.