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 — Locale constructors to Locale.of() | java.evolved

Home / Date/Time / Locale constructors to Locale.of()
Date/Time Beginner

Locale constructors to Locale.of()

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

Create language and region locales with Locale.of() instead of deprecated constructors.

Date & Time Constructors
Code Comparison
✕ Deprecated constructor
Locale brazilianPortuguese =
    new Locale("pt", "BR");
✓ Java 19+
Locale brazilianPortuguese =
    Locale.of("pt", "BR");
Share 𝕏
See a problem with this code? Let us know.
Why the modern way wins
✅

Supported API

Avoids deprecated constructors.

👁

Clear construction

The named factory communicates intent.

🧩

Consistent style

Matches modern JDK value-object factories.

Old Approach
Locale constructors
Modern Approach
Locale.of()
Since JDK
19
Difficulty
Beginner
JDK Support
Locale constructors to Locale.of()
Available

Available since JDK 19 (September 2022)

How it works

Locale factory methods replace constructors deprecated in JDK 19. The factory form is clearer, aligns with other value-based APIs, and centralizes locale creation.

Related Documentation
Locale.of() ↗
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 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+ →
Strings Beginner

String.formatted()

Java 8
String msg = String.format(
    "Hello %s, you are %d",
    name, age
);
Java 15+
String msg =
    "Hello %s, you are %d"
    .formatted(name, age);
Hover to see modern ➜
JDK 15+ →
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.