Skip to content

Commit 609c959

Browse files
authored
Merge pull request #189 from javaevolved/brunoborges-locale-of-pattern
Add Locale.of modernization pattern
2 parents 0054ea9 + 64970b5 commit 609c959

4 files changed

Lines changed: 57 additions & 2 deletions

File tree

content/datetime/hex-format.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ support:
4141
state: "available"
4242
description: "Widely available since JDK 17 LTS (Sept 2021)"
4343
prev: "datetime/math-clamp"
44-
next: "security/pem-encoding"
44+
next: "datetime/locale-of"
4545
related:
4646
- "datetime/date-formatting"
4747
- "datetime/instant-precision"

content/datetime/locale-of.yaml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
id: 361b53ba-ec9d-45a1-95e5-8c4b3aa7731a
3+
slug: "locale-of"
4+
title: "Locale constructors to Locale.of()"
5+
category: "datetime"
6+
difficulty: "beginner"
7+
jdkVersion: "19"
8+
oldLabel: "Deprecated constructor"
9+
modernLabel: "Java 19+"
10+
oldApproach: "Locale constructors"
11+
modernApproach: "Locale.of()"
12+
oldCode: |-
13+
Locale brazilianPortuguese =
14+
new Locale("pt", "BR");
15+
modernCode: |-
16+
Locale brazilianPortuguese =
17+
Locale.of("pt", "BR");
18+
summary: "Create language and region locales with Locale.of() instead of deprecated constructors."
19+
explanation: "Locale factory methods replace constructors deprecated in JDK 19. The factory\
20+
\ form is clearer, aligns with other value-based APIs, and centralizes locale creation."
21+
whyModernWins:
22+
- icon: ""
23+
title: "Supported API"
24+
desc: "Avoids deprecated constructors."
25+
- icon: "👁"
26+
title: "Clear construction"
27+
desc: "The named factory communicates intent."
28+
- icon: "🧩"
29+
title: "Consistent style"
30+
desc: "Matches modern JDK value-object factories."
31+
support:
32+
state: "available"
33+
description: "Available since JDK 19 (September 2022)"
34+
prev: "datetime/hex-format"
35+
next: "security/pem-encoding"
36+
related:
37+
- "datetime/date-formatting"
38+
- "datetime/java-time-basics"
39+
- "strings/string-formatted"
40+
tags:
41+
- datetime
42+
- constructors
43+
docs:
44+
- title: "Locale.of()"
45+
href: "https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/Locale.html#of(java.lang.String,java.lang.String)"

content/security/pem-encoding.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ whyModernWins:
3939
support:
4040
state: "preview"
4141
description: "Preview in JDK 25 (JEP 470). Requires --enable-preview."
42-
prev: "datetime/hex-format"
42+
prev: "datetime/locale-of"
4343
next: "security/key-derivation-functions"
4444
related:
4545
- "security/key-derivation-functions"

proof/datetime/LocaleOf.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
///usr/bin/env jbang "$0" "$@" ; exit $?
2+
//JAVA 25+
3+
import java.util.Locale;
4+
5+
/// Proof: locale-of
6+
/// Source: content/datetime/locale-of.yaml
7+
void main() {
8+
Locale brazilianPortuguese =
9+
Locale.of("pt", "BR");
10+
}

0 commit comments

Comments
 (0)