This is an automated email from the ASF dual-hosted git repository.

chaokunyang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/fory.git


The following commit(s) were added to refs/heads/main by this push:
     new 5613ea0fe feat(java): support timezone in JSON date formats (#3911)
5613ea0fe is described below

commit 5613ea0fe3f5e26db750e6c8b60c904e0eb27e2e
Author: Shawn Yang <[email protected]>
AuthorDate: Mon Aug 3 13:36:36 2026 +0800

    feat(java): support timezone in JSON date formats (#3911)
    
    ## Why?
    
    
    
    ## What does this PR do?
    
    
    
    ## Related issues
    
    #3908
    
    ## AI Contribution Checklist
    
    
    
    - [ ] Substantial AI assistance was used in this PR: `yes` / `no`
    - [ ] If `yes`, I included a completed [AI Contribution
    
Checklist](https://github.com/apache/fory/blob/main/AI_POLICY.md#9-contributor-checklist-for-ai-assisted-prs)
    in this PR description and the required `AI Usage Disclosure`.
    - [ ] If `yes`, my PR description includes the required `ai_review`
    summary and screenshot evidence or equivalent persisted links of the
    final clean AI review results from both fresh reviewers described in
    `AI_POLICY.md`, the Fory-guided reviewer and the independent general
    reviewer, on the current PR diff or current HEAD after the latest code
    changes.
    
    
    
    ## Does this PR introduce any user-facing change?
    
    
    
    - [ ] Does this PR introduce any public API change?
    - [ ] Does this PR introduce any binary protocol compatibility change?
    
    ## Benchmark
---
 docs/guide/java/android-support.md                 |   3 +-
 docs/guide/java/graalvm-support.md                 |   7 +-
 docs/guide/java/json-support.md                    |  14 ++
 integration_tests/android_tests/proguard-rules.pro |   1 +
 .../fory/android/ForyAndroidInstrumentedTest.java  |   5 +
 .../apache/fory/android/AndroidJsonScenarios.java  |  29 +++++
 .../org/apache/fory/graalvm/ForyJsonExample.java   |  30 +++++
 .../processing/JsonTypeProcessorTest.java          |  32 +++--
 java/fory-json/README.md                           |  14 ++
 .../apache/fory/json/annotation/JsonFormat.java    |  14 +-
 .../fory/json/codec/DateTimeFormatCodec.java       |  58 ++++++++-
 .../org/apache/fory/json/codec/ScalarCodecs.java   |  96 ++++++++------
 .../fory/json/resolver/JsonTypeResolver.java       |   3 +-
 .../fory-json/native-image.properties              |   9 +-
 .../json/ForyJsonGraalVMFeatureJarVerifier.java    |   6 +-
 .../apache/fory/json/JsonAndroidRuntimeTest.java   |  16 +++
 .../apache/fory/json/JsonFormatAnnotationTest.java | 142 +++++++++++++++++++--
 17 files changed, 398 insertions(+), 81 deletions(-)

diff --git a/docs/guide/java/android-support.md 
b/docs/guide/java/android-support.md
index 0a90942f7..9a6ff8be3 100644
--- a/docs/guide/java/android-support.md
+++ b/docs/guide/java/android-support.md
@@ -162,7 +162,8 @@ annotations, and the Base64 codec constructor. Without 
`@JsonType`, these annota
 through reflection, but a release-minified application must keep the exact 
annotated members,
 annotation attributes, and codec constructor itself. A `JsonValue` method may 
use a non-JavaBean
 name, so its manual rule must name that method explicitly. `JsonFormat` keeps 
the same direct-field
-and one-wrapper-level behavior as on the JVM.
+and one-wrapper-level behavior as on the JVM, including `timezone` for 
`Instant`, `ZonedDateTime`,
+and `OffsetDateTime`.
 
 Android Fory JSON requires a retained no-argument constructor for an ordinary 
mutable class; it may
 be non-public when Android reflection can make it accessible. `JsonCreator` 
constructor-backed
diff --git a/docs/guide/java/graalvm-support.md 
b/docs/guide/java/graalvm-support.md
index 824622f7a..088ffed80 100644
--- a/docs/guide/java/graalvm-support.md
+++ b/docs/guide/java/graalvm-support.md
@@ -163,9 +163,10 @@ JVM and Android.
 `JsonValue` fields and effective public zero-argument methods are supported, 
including matching
 one-String `JsonCreator` constructors and public static factories. Fixed 
`JsonRawValue` fields and
 getters support trusted raw String values, and fixed `JsonBase64` fields and 
getters support Base64
-`byte[]` values as on the JVM. `JsonFormat` date/time fields use the same 
direct-field and
-one-wrapper-level behavior as on the JVM. For direct target annotations, 
annotate each reachable
-owning model with `JsonType` so Native Image retains these members and the 
Base64 codec constructor.
+`byte[]` values as on the JVM. `JsonFormat` date/time fields use the same 
direct-field,
+one-wrapper-level, and `timezone` behavior as on the JVM. For direct target 
annotations, annotate
+each reachable owning model with `JsonType` so Native Image retains these 
members and the Base64
+codec constructor.
 A directly annotated `JsonValue` Record uses its generated component accessor 
and canonical
 constructor operations. An effective declaration supplied by a Mixin uses the 
Mixin workflow above
 instead.
diff --git a/docs/guide/java/json-support.md b/docs/guide/java/json-support.md
index e26fe5c35..2e5e8533c 100644
--- a/docs/guide/java/json-support.md
+++ b/docs/guide/java/json-support.md
@@ -657,6 +657,7 @@ Use `JsonFormat` on a date/time field to select its JSON 
text pattern in both di
 use `DateTimeFormatter` syntax and the root locale:
 
 ```java
+import java.time.Instant;
 import java.time.LocalDate;
 import java.util.List;
 import java.util.Map;
@@ -675,6 +676,9 @@ public final class Schedule {
 
   @JsonFormat(pattern = "dd/MM/uuuu")
   public Map<String, LocalDate> daysByName;
+
+  @JsonFormat(pattern = "uuuu-MM-dd HH:mm:ss XXX", timezone = "Asia/Shanghai")
+  public Instant timestamp;
 }
 ```
 
@@ -691,6 +695,16 @@ Supported values are exact `LocalDate`, `LocalTime`, 
`LocalDateTime`, `Instant`,
 offset carried by the value. The pattern must contain enough information to 
reconstruct the
 declared type.
 
+Set `timezone` to a valid `ZoneId` identifier to format and parse `Instant`, 
`ZonedDateTime`, or
+`OffsetDateTime` in that zone. For example, the `timestamp` field above writes
+`Instant.parse("2024-01-02T03:04:05Z")` as `"2024-01-02 11:04:05 +08:00"`. The 
parsed value keeps
+the same instant for matching timezone text. The configured zone supplies 
missing zone or offset
+information during parsing; an explicit zone or offset in the JSON text 
participates in the usual
+`DateTimeFormatter` resolution. Include an offset in the pattern when an exact 
instant must survive
+a daylight saving time overlap. Omitting `timezone` preserves the default 
behavior described
+above. Invalid zone identifiers and a non-empty `timezone` on other supported 
date/time types are
+rejected.
+
 `JsonFormat` is a field annotation, not a type-use annotation. A record 
component works through its
 generated field. Nested wrappers, Map keys, raw or wildcard direct children, 
JSON Any values, and
 unwrapped values are intentionally rejected. Types with ambiguous formatting 
semantics, including
diff --git a/integration_tests/android_tests/proguard-rules.pro 
b/integration_tests/android_tests/proguard-rules.pro
index 80115ad83..6e00f62da 100644
--- a/integration_tests/android_tests/proguard-rules.pro
+++ b/integration_tests/android_tests/proguard-rules.pro
@@ -13,6 +13,7 @@
   public static void generatedPlainRules();
   public static void generatedRecord();
   public static void generatedValueRecord();
+  public static void generatedFormatTimezone();
   public static void manualCodecs();
   public static void generatedCodecs();
   public static void generatedUnwrapped();
diff --git 
a/integration_tests/android_tests/src/androidTest/java/org/apache/fory/android/ForyAndroidInstrumentedTest.java
 
b/integration_tests/android_tests/src/androidTest/java/org/apache/fory/android/ForyAndroidInstrumentedTest.java
index ff713689a..5555c2926 100644
--- 
a/integration_tests/android_tests/src/androidTest/java/org/apache/fory/android/ForyAndroidInstrumentedTest.java
+++ 
b/integration_tests/android_tests/src/androidTest/java/org/apache/fory/android/ForyAndroidInstrumentedTest.java
@@ -55,6 +55,11 @@ public class ForyAndroidInstrumentedTest {
     AndroidJsonScenarios.generatedValueRecord();
   }
 
+  @Test
+  public void jsonFormatTimezone() {
+    AndroidJsonScenarios.generatedFormatTimezone();
+  }
+
   @Test
   public void manualJsonCodecs() {
     AndroidJsonScenarios.manualCodecs();
diff --git 
a/integration_tests/android_tests/src/main/java/org/apache/fory/android/AndroidJsonScenarios.java
 
b/integration_tests/android_tests/src/main/java/org/apache/fory/android/AndroidJsonScenarios.java
index 5b0499951..b1f02aab4 100644
--- 
a/integration_tests/android_tests/src/main/java/org/apache/fory/android/AndroidJsonScenarios.java
+++ 
b/integration_tests/android_tests/src/main/java/org/apache/fory/android/AndroidJsonScenarios.java
@@ -19,12 +19,15 @@
 
 package org.apache.fory.android;
 
+import java.time.Instant;
 import java.util.Arrays;
+import java.util.List;
 import java.util.Optional;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.concurrent.atomic.AtomicReferenceArray;
 import org.apache.fory.json.ForyJson;
 import org.apache.fory.json.annotation.JsonCreator;
+import org.apache.fory.json.annotation.JsonFormat;
 import org.apache.fory.json.annotation.JsonMixin;
 import org.apache.fory.json.annotation.JsonProperty;
 import org.apache.fory.json.annotation.JsonPropertyOrder;
@@ -104,6 +107,23 @@ public final class AndroidJsonScenarios {
         json.fromJson("\"decoded-value\"", 
GeneratedJsonValueRecord.class).value());
   }
 
+  public static void generatedFormatTimezone() {
+    ForyJson json = ForyJson.builder().build();
+    Instant instant = Instant.parse("2024-01-02T03:04:05Z");
+    FormatTimezoneModel value = new FormatTimezoneModel();
+    value.instant = instant;
+    value.instants = Arrays.asList(instant, instant.plusSeconds(3600));
+    String encoded = json.toJson(value);
+    checkEquals(
+        "{\"instant\":\"2024-01-02 11:04:05 +08:00\","
+            + "\"instants\":[\"2024-01-02 11:04:05 +08:00\","
+            + "\"2024-01-02 12:04:05 +08:00\"]}",
+        encoded);
+    FormatTimezoneModel decoded = json.fromJson(encoded, 
FormatTimezoneModel.class);
+    checkEquals(instant, decoded.instant);
+    checkEquals(value.instants, decoded.instants);
+  }
+
   public static void manualCodecs() {
     ForyJson json = ForyJson.builder().build();
     ManualJsonModel value = new ManualJsonModel();
@@ -428,6 +448,15 @@ public final class AndroidJsonScenarios {
     GeneratedJsonMixinValueRecordAnnotations(String value) {}
   }
 
+  @JsonType
+  public static final class FormatTimezoneModel {
+    @JsonFormat(pattern = "uuuu-MM-dd HH:mm:ss XXX", timezone = 
"Asia/Shanghai")
+    public Instant instant;
+
+    @JsonFormat(pattern = "uuuu-MM-dd HH:mm:ss XXX", timezone = 
"Asia/Shanghai")
+    public List<Instant> instants;
+  }
+
   private static void check(boolean condition) {
     if (!condition) {
       throw new AssertionError("check failed");
diff --git 
a/integration_tests/graalvm_tests/src/main/java/org/apache/fory/graalvm/ForyJsonExample.java
 
b/integration_tests/graalvm_tests/src/main/java/org/apache/fory/graalvm/ForyJsonExample.java
index 9003fdca2..981b9af4a 100644
--- 
a/integration_tests/graalvm_tests/src/main/java/org/apache/fory/graalvm/ForyJsonExample.java
+++ 
b/integration_tests/graalvm_tests/src/main/java/org/apache/fory/graalvm/ForyJsonExample.java
@@ -27,6 +27,7 @@ import java.nio.charset.StandardCharsets;
 import java.sql.Date;
 import java.sql.Time;
 import java.sql.Timestamp;
+import java.time.Instant;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
@@ -50,6 +51,7 @@ import org.apache.fory.json.annotation.JsonAnySetter;
 import org.apache.fory.json.annotation.JsonBase64;
 import org.apache.fory.json.annotation.JsonCodec;
 import org.apache.fory.json.annotation.JsonCreator;
+import org.apache.fory.json.annotation.JsonFormat;
 import org.apache.fory.json.annotation.JsonIgnore;
 import org.apache.fory.json.annotation.JsonMixin;
 import org.apache.fory.json.annotation.JsonProperty;
@@ -105,6 +107,7 @@ public final class ForyJsonExample {
         testMixinCodec();
         testBigDecimal();
         testSqlTypes();
+        testFormatTimezone();
         testClosedPackage();
       } finally {
         System.setOut(originalOut);
@@ -540,6 +543,24 @@ public final class ForyJsonExample {
     Preconditions.checkArgument(decoded.timestamp.getTime() == 3_000L);
   }
 
+  private static void testFormatTimezone() {
+    ForyJson json = ForyJson.builder().build();
+    Instant instant = Instant.parse("2024-01-02T03:04:05Z");
+    FormatTimezoneValues value = new FormatTimezoneValues();
+    value.instant = instant;
+    value.instants = List.of(instant, instant.plusSeconds(3600));
+    String expected =
+        "{\"instant\":\"2024-01-02 11:04:05 +08:00\","
+            + "\"instants\":[\"2024-01-02 11:04:05 +08:00\","
+            + "\"2024-01-02 12:04:05 +08:00\"]}";
+    Preconditions.checkArgument(json.toJson(value).equals(expected));
+    byte[] bytes = json.toJsonBytes(value);
+    Preconditions.checkArgument(new String(bytes, 
StandardCharsets.UTF_8).equals(expected));
+    FormatTimezoneValues decoded = json.fromJson(bytes, 
FormatTimezoneValues.class);
+    Preconditions.checkArgument(decoded.instant.equals(instant));
+    Preconditions.checkArgument(decoded.instants.equals(value.instants));
+  }
+
   public interface InheritedJsonConfig {
     default ForyJson duplicateConfiguration() {
       return newProviderJson();
@@ -1201,6 +1222,15 @@ public final class ForyJsonExample {
     public Timestamp timestamp;
   }
 
+  @JsonType
+  public static final class FormatTimezoneValues {
+    @JsonFormat(pattern = "uuuu-MM-dd HH:mm:ss XXX", timezone = 
"Asia/Shanghai")
+    public Instant instant;
+
+    @JsonFormat(pattern = "uuuu-MM-dd HH:mm:ss XXX", timezone = 
"Asia/Shanghai")
+    public List<Instant> instants;
+  }
+
   @JsonMixin(target = JsonMixinTarget.class)
   @JsonPropertyOrder({"id", "address"})
   public interface JsonMixinModel {
diff --git 
a/java/fory-annotation-processor/src/test/java/org/apache/fory/annotation/processing/JsonTypeProcessorTest.java
 
b/java/fory-annotation-processor/src/test/java/org/apache/fory/annotation/processing/JsonTypeProcessorTest.java
index 80bfa2edb..4eedebd87 100644
--- 
a/java/fory-annotation-processor/src/test/java/org/apache/fory/annotation/processing/JsonTypeProcessorTest.java
+++ 
b/java/fory-annotation-processor/src/test/java/org/apache/fory/annotation/processing/JsonTypeProcessorTest.java
@@ -33,7 +33,7 @@ import java.net.URLClassLoader;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Path;
-import java.time.LocalDate;
+import java.time.Instant;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -790,20 +790,22 @@ public class JsonTypeProcessorTest {
         compile(
             "test.FormatRecord",
             "package test;\n"
-                + "import java.time.LocalDate;\n"
+                + "import java.time.Instant;\n"
                 + "import org.apache.fory.json.annotation.*;\n"
                 + "@JsonType public record FormatRecord(\n"
-                + "    @JsonFormat(pattern = \"dd/MM/uuuu\") LocalDate value) 
{}\n");
+                + "    @JsonFormat(pattern = \"uuuu-MM-dd HH:mm:ss XXX\", "
+                + "timezone = \"Asia/Shanghai\") Instant value) {}\n");
     assertTrue(result.success, result.diagnostics());
     String rules = result.generatedResource(RULE_PREFIX + 
"test.FormatRecord.pro");
     assertTrue(rules.contains("@interface 
org.apache.fory.json.annotation.JsonFormat"), rules);
     ClassLoader loader = result.classLoader();
     Class<?> type = loader.loadClass("test.FormatRecord");
-    Object value = 
type.getConstructor(LocalDate.class).newInstance(LocalDate.of(2024, 1, 2));
+    Instant instant = Instant.parse("2024-01-02T03:04:05Z");
+    Object value = type.getConstructor(Instant.class).newInstance(instant);
     for (ForyJson json : jsonRuntimes(loader)) {
-      assertEquals(json.toJson(value), "{\"value\":\"02/01/2024\"}");
-      Object decoded = json.fromJson("{\"value\":\"03/01/2024\"}", type);
-      assertEquals(type.getMethod("value").invoke(decoded), LocalDate.of(2024, 
1, 3));
+      assertEquals(json.toJson(value), "{\"value\":\"2024-01-02 11:04:05 
+08:00\"}");
+      Object decoded = json.fromJson("{\"value\":\"2024-01-02 12:04:05 
+08:00\"}", type);
+      assertEquals(type.getMethod("value").invoke(decoded), 
instant.plusSeconds(3600));
     }
   }
 
@@ -813,11 +815,12 @@ public class JsonTypeProcessorTest {
         compile(
             "test.FormatTarget",
             "package test;\n"
-                + "import java.time.LocalDate;\n"
+                + "import java.time.Instant;\n"
                 + "import org.apache.fory.json.annotation.*;\n"
-                + "public final class FormatTarget { public LocalDate value; 
}\n"
+                + "public final class FormatTarget { public Instant value; }\n"
                 + "@JsonMixin(target = FormatTarget.class) abstract class 
FormatMixin {\n"
-                + "  @JsonFormat(pattern = \"dd/MM/uuuu\") LocalDate value;\n"
+                + "  @JsonFormat(pattern = \"uuuu-MM-dd HH:mm:ss XXX\", "
+                + "timezone = \"Asia/Shanghai\") Instant value;\n"
                 + "}\n");
     assertTrue(result.success, result.diagnostics());
     String base = "FormatMixin_ForyJsonMixin_test_x2e_FormatTarget";
@@ -828,7 +831,8 @@ public class JsonTypeProcessorTest {
     Class<?> target = loader.loadClass("test.FormatTarget");
     Class<?> mixin = loader.loadClass("test.FormatMixin");
     Object value = target.getConstructor().newInstance();
-    target.getField("value").set(value, LocalDate.of(2024, 1, 2));
+    Instant instant = Instant.parse("2024-01-02T03:04:05Z");
+    target.getField("value").set(value, instant);
     for (boolean codegen : new boolean[] {false, true}) {
       ForyJson json =
           ForyJson.builder()
@@ -837,9 +841,9 @@ public class JsonTypeProcessorTest {
               .withClassLoader(loader)
               .registerMixin(mixin)
               .build();
-      assertEquals(json.toJson(value), "{\"value\":\"02/01/2024\"}");
-      Object decoded = json.fromJson("{\"value\":\"03/01/2024\"}", target);
-      assertEquals(target.getField("value").get(decoded), LocalDate.of(2024, 
1, 3));
+      assertEquals(json.toJson(value), "{\"value\":\"2024-01-02 11:04:05 
+08:00\"}");
+      Object decoded = json.fromJson("{\"value\":\"2024-01-02 12:04:05 
+08:00\"}", target);
+      assertEquals(target.getField("value").get(decoded), 
instant.plusSeconds(3600));
     }
   }
 
diff --git a/java/fory-json/README.md b/java/fory-json/README.md
index 33ba4942c..933fe6970 100644
--- a/java/fory-json/README.md
+++ b/java/fory-json/README.md
@@ -725,6 +725,7 @@ Use `JsonFormat` on a date/time field to select its JSON 
text pattern in both di
 use `DateTimeFormatter` syntax and the root locale:
 
 ```java
+import java.time.Instant;
 import java.time.LocalDate;
 import java.util.List;
 import java.util.Map;
@@ -743,6 +744,9 @@ public final class Schedule {
 
   @JsonFormat(pattern = "dd/MM/uuuu")
   public Map<String, LocalDate> daysByName;
+
+  @JsonFormat(pattern = "uuuu-MM-dd HH:mm:ss XXX", timezone = "Asia/Shanghai")
+  public Instant timestamp;
 }
 ```
 
@@ -759,6 +763,16 @@ Supported values are exact `LocalDate`, `LocalTime`, 
`LocalDateTime`, `Instant`,
 offset carried by the value. The pattern must contain enough information to 
reconstruct the
 declared type.
 
+Set `timezone` to a valid `ZoneId` identifier to format and parse `Instant`, 
`ZonedDateTime`, or
+`OffsetDateTime` in that zone. For example, the `timestamp` field above writes
+`Instant.parse("2024-01-02T03:04:05Z")` as `"2024-01-02 11:04:05 +08:00"`. The 
parsed value keeps
+the same instant for matching timezone text. The configured zone supplies 
missing zone or offset
+information during parsing; an explicit zone or offset in the JSON text 
participates in the usual
+`DateTimeFormatter` resolution. Include an offset in the pattern when an exact 
instant must survive
+a daylight saving time overlap. Omitting `timezone` preserves the default 
behavior described
+above. Invalid zone identifiers and a non-empty `timezone` on other supported 
date/time types are
+rejected.
+
 `JsonFormat` is a field annotation, not a type-use annotation. A record 
component works through its
 generated field. Nested wrappers, Map keys, raw or wildcard direct children, 
JSON Any values, and
 unwrapped values are intentionally rejected. Types with ambiguous formatting 
semantics, including
diff --git 
a/java/fory-json/src/main/java/org/apache/fory/json/annotation/JsonFormat.java 
b/java/fory-json/src/main/java/org/apache/fory/json/annotation/JsonFormat.java
index 329d554ef..691452848 100644
--- 
a/java/fory-json/src/main/java/org/apache/fory/json/annotation/JsonFormat.java
+++ 
b/java/fory-json/src/main/java/org/apache/fory/json/annotation/JsonFormat.java
@@ -36,8 +36,10 @@ import java.lang.annotation.Target;
  * java.time.MonthDay}, {@link java.time.OffsetTime}, {@link 
java.time.OffsetDateTime}, {@link
  * java.time.chrono.HijrahDate}, {@link java.time.chrono.JapaneseDate}, {@link
  * java.time.chrono.MinguoDate}, and {@link 
java.time.chrono.ThaiBuddhistDate}. Instant values use
- * UTC; zoned and offset values use the zone or offset carried by the value. 
The pattern must retain
- * enough information to reconstruct the declared type.
+ * UTC; zoned and offset values use the zone or offset carried by the value. 
An explicit {@link
+ * #timezone()} overrides that behavior for {@code Instant}, {@code 
ZonedDateTime}, and {@code
+ * OffsetDateTime}; it is rejected for other date/time types. The pattern must 
retain enough
+ * information to reconstruct the declared type.
  *
  * <p>Formatting is applied in both JSON directions. Arrays and collections 
apply it to their direct
  * element, maps to their direct value, and optional and atomic-reference 
wrappers to their direct
@@ -50,4 +52,12 @@ import java.lang.annotation.Target;
 public @interface JsonFormat {
   /** Returns the required date/time pattern. */
   String pattern();
+
+  /**
+   * Returns the optional {@link java.time.ZoneId} identifier used for 
formatting and parsing {@link
+   * java.time.Instant}, {@link java.time.ZonedDateTime}, and {@link 
java.time.OffsetDateTime}
+   * values. An empty value keeps the declared value's zone or offset, except 
that {@code Instant}
+   * continues to use UTC.
+   */
+  String timezone() default "";
 }
diff --git 
a/java/fory-json/src/main/java/org/apache/fory/json/codec/DateTimeFormatCodec.java
 
b/java/fory-json/src/main/java/org/apache/fory/json/codec/DateTimeFormatCodec.java
index 49faf931c..0e118451b 100644
--- 
a/java/fory-json/src/main/java/org/apache/fory/json/codec/DateTimeFormatCodec.java
+++ 
b/java/fory-json/src/main/java/org/apache/fory/json/codec/DateTimeFormatCodec.java
@@ -19,6 +19,7 @@
 
 package org.apache.fory.json.codec;
 
+import java.time.DateTimeException;
 import java.time.Instant;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
@@ -28,6 +29,8 @@ import java.time.OffsetDateTime;
 import java.time.OffsetTime;
 import java.time.Year;
 import java.time.YearMonth;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
 import java.time.ZonedDateTime;
 import java.time.chrono.HijrahChronology;
 import java.time.chrono.HijrahDate;
@@ -38,7 +41,9 @@ import java.time.chrono.MinguoDate;
 import java.time.chrono.ThaiBuddhistChronology;
 import java.time.chrono.ThaiBuddhistDate;
 import java.time.format.DateTimeFormatter;
+import java.time.temporal.ChronoField;
 import java.time.temporal.TemporalAccessor;
+import java.time.temporal.TemporalQueries;
 import java.util.Locale;
 import org.apache.fory.json.ForyJsonException;
 import org.apache.fory.json.reader.Latin1JsonReader;
@@ -63,12 +68,13 @@ final class DateTimeFormatCodec implements 
JsonValueCodec<Object> {
   private static final int JAPANESE_DATE = 12;
   private static final int MINGUO_DATE = 13;
   private static final int THAI_BUDDHIST_DATE = 14;
+  private static final int OFFSET_DATE_TIME_WITH_ZONE = 15;
 
   private final Class<?> type;
   private final int kind;
   private final DateTimeFormatter formatter;
 
-  static JsonValueCodec<?> create(Class<?> type, String pattern) {
+  static JsonValueCodec<?> create(Class<?> type, String pattern, String 
timezone) {
     if (pattern.isEmpty()) {
       throw invalidPattern(type, pattern, null);
     }
@@ -79,9 +85,24 @@ final class DateTimeFormatCodec implements 
JsonValueCodec<Object> {
     } catch (IllegalArgumentException e) {
       throw invalidPattern(type, pattern, e);
     }
-    if (kind == INSTANT) {
-      formatter = formatter.withZone(java.time.ZoneOffset.UTC);
-    } else if (kind == HIJRAH_DATE) {
+    if (timezone.isEmpty()) {
+      if (kind == INSTANT) {
+        formatter = formatter.withZone(ZoneOffset.UTC);
+      }
+    } else {
+      if (kind != INSTANT && kind != ZONED_DATE_TIME && kind != 
OFFSET_DATE_TIME) {
+        throw unsupportedTimezone(type);
+      }
+      try {
+        formatter = formatter.withZone(ZoneId.of(timezone));
+      } catch (DateTimeException e) {
+        throw invalidTimezone(type, timezone, e);
+      }
+      if (kind == OFFSET_DATE_TIME) {
+        kind = OFFSET_DATE_TIME_WITH_ZONE;
+      }
+    }
+    if (kind == HIJRAH_DATE) {
       formatter = formatter.withChronology(HijrahChronology.INSTANCE);
     } else if (kind == JAPANESE_DATE) {
       formatter = formatter.withChronology(JapaneseChronology.INSTANCE);
@@ -159,6 +180,8 @@ final class DateTimeFormatCodec implements 
JsonValueCodec<Object> {
           return OffsetTime.from(parsed);
         case OFFSET_DATE_TIME:
           return OffsetDateTime.from(parsed);
+        case OFFSET_DATE_TIME_WITH_ZONE:
+          return parseOffsetDateTime(parsed);
         case HIJRAH_DATE:
           return HijrahDate.from(parsed);
         case JAPANESE_DATE:
@@ -175,6 +198,23 @@ final class DateTimeFormatCodec implements 
JsonValueCodec<Object> {
     }
   }
 
+  private OffsetDateTime parseOffsetDateTime(TemporalAccessor parsed) {
+    LocalDateTime dateTime = LocalDateTime.from(parsed);
+    ZoneOffset offset = parsed.query(TemporalQueries.offset());
+    // An effective region ZoneId does not synthesize OFFSET_SECONDS. Keep an 
explicit offset
+    // authoritative. The rule-owned offset is allocation-free for historical 
second-level offsets;
+    // only reconstruct it when a zone name selected the other offset during 
an overlap.
+    if (offset == null) {
+      offset = 
parsed.query(TemporalQueries.zone()).getRules().getOffset(dateTime);
+      long localEpochSeconds = dateTime.toEpochSecond(ZoneOffset.UTC);
+      long instantSeconds = parsed.getLong(ChronoField.INSTANT_SECONDS);
+      if (localEpochSeconds - offset.getTotalSeconds() != instantSeconds) {
+        offset = ZoneOffset.ofTotalSeconds(Math.toIntExact(localEpochSeconds - 
instantSeconds));
+      }
+    }
+    return OffsetDateTime.of(dateTime, offset);
+  }
+
   static boolean supports(Class<?> type) {
     return type == LocalDate.class
         || type == LocalTime.class
@@ -243,6 +283,16 @@ final class DateTimeFormatCodec implements 
JsonValueCodec<Object> {
         "Invalid @JsonFormat pattern for " + type.getTypeName() + ": " + 
pattern, cause);
   }
 
+  private static ForyJsonException invalidTimezone(
+      Class<?> type, String timezone, Throwable cause) {
+    return new ForyJsonException(
+        "Invalid @JsonFormat timezone for " + type.getTypeName() + ": " + 
timezone, cause);
+  }
+
+  private static ForyJsonException unsupportedTimezone(Class<?> type) {
+    return new ForyJsonException("@JsonFormat timezone is not supported for " 
+ type.getTypeName());
+  }
+
   private static ForyJsonException invalidValue(
       Class<?> type, CharSequence value, Throwable cause) {
     return new ForyJsonException(
diff --git 
a/java/fory-json/src/main/java/org/apache/fory/json/codec/ScalarCodecs.java 
b/java/fory-json/src/main/java/org/apache/fory/json/codec/ScalarCodecs.java
index c71e4113c..75d6f06f5 100644
--- a/java/fory-json/src/main/java/org/apache/fory/json/codec/ScalarCodecs.java
+++ b/java/fory-json/src/main/java/org/apache/fory/json/codec/ScalarCodecs.java
@@ -100,19 +100,6 @@ import org.apache.fory.type.Float16;
  * decimal timestamps.
  */
 public final class ScalarCodecs {
-  private static final DateTimeFormatter YEAR_MONTH_FORMATTER =
-      DateTimeFormatter.ofPattern("uuuu-MM");
-  private static final DateTimeFormatter MONTH_DAY_FORMATTER =
-      DateTimeFormatter.ofPattern("--MM-dd");
-  private static final DateTimeFormatter HIJRAH_DATE_FORMATTER =
-      
DateTimeFormatter.ISO_LOCAL_DATE.withChronology(HijrahChronology.INSTANCE);
-  private static final DateTimeFormatter JAPANESE_DATE_FORMATTER =
-      
DateTimeFormatter.ISO_LOCAL_DATE.withChronology(JapaneseChronology.INSTANCE);
-  private static final DateTimeFormatter MINGUO_DATE_FORMATTER =
-      
DateTimeFormatter.ISO_LOCAL_DATE.withChronology(MinguoChronology.INSTANCE);
-  private static final DateTimeFormatter THAI_BUDDHIST_DATE_FORMATTER =
-      
DateTimeFormatter.ISO_LOCAL_DATE.withChronology(ThaiBuddhistChronology.INSTANCE);
-
   private ScalarCodecs() {}
 
   @Internal
@@ -121,8 +108,9 @@ public final class ScalarCodecs {
   }
 
   @Internal
-  public static JsonValueCodec<?> dateTimeFormatCodec(Class<?> type, String 
pattern) {
-    return DateTimeFormatCodec.create(type, pattern);
+  public static JsonValueCodec<?> dateTimeFormatCodec(
+      Class<?> type, String pattern, String timezone) {
+    return DateTimeFormatCodec.create(type, pattern, timezone);
   }
 
   public static final class NaturalCodec implements JsonValueCodec<Object> {
@@ -2108,12 +2096,16 @@ public final class ScalarCodecs {
   public static final class YearMonthCodec implements 
JsonValueCodec<YearMonth> {
     public static final YearMonthCodec INSTANCE = new YearMonthCodec();
 
+    private static final class Formatter {
+      private static final DateTimeFormatter INSTANCE = 
DateTimeFormatter.ofPattern("uuuu-MM");
+    }
+
     @Override
     public void writeString(StringJsonWriter writer, YearMonth value) {
       if (value == null) {
         writer.writeNull();
       } else {
-        writer.writeTemporal(value, YEAR_MONTH_FORMATTER);
+        writer.writeTemporal(value, Formatter.INSTANCE);
       }
     }
 
@@ -2122,7 +2114,7 @@ public final class ScalarCodecs {
       if (value == null) {
         writer.writeNull();
       } else {
-        writer.writeTemporal(value, YEAR_MONTH_FORMATTER);
+        writer.writeTemporal(value, Formatter.INSTANCE);
       }
     }
 
@@ -2145,12 +2137,16 @@ public final class ScalarCodecs {
   public static final class MonthDayCodec implements JsonValueCodec<MonthDay> {
     public static final MonthDayCodec INSTANCE = new MonthDayCodec();
 
+    private static final class Formatter {
+      private static final DateTimeFormatter INSTANCE = 
DateTimeFormatter.ofPattern("--MM-dd");
+    }
+
     @Override
     public void writeString(StringJsonWriter writer, MonthDay value) {
       if (value == null) {
         writer.writeNull();
       } else {
-        writer.writeTemporal(value, MONTH_DAY_FORMATTER);
+        writer.writeTemporal(value, Formatter.INSTANCE);
       }
     }
 
@@ -2159,7 +2155,7 @@ public final class ScalarCodecs {
       if (value == null) {
         writer.writeNull();
       } else {
-        writer.writeTemporal(value, MONTH_DAY_FORMATTER);
+        writer.writeTemporal(value, Formatter.INSTANCE);
       }
     }
 
@@ -2290,15 +2286,22 @@ public final class ScalarCodecs {
     }
   }
 
+  // Registry publication initializes chronology codec classes. Keep formatter 
setup behind
+  // method-use holders because JDK chronology initialization may require 
runtime environment state.
   public static final class HijrahDateCodec implements 
JsonValueCodec<HijrahDate> {
     public static final HijrahDateCodec INSTANCE = new HijrahDateCodec();
 
+    private static final class Formatter {
+      private static final DateTimeFormatter INSTANCE =
+          
DateTimeFormatter.ISO_LOCAL_DATE.withChronology(HijrahChronology.INSTANCE);
+    }
+
     @Override
     public void writeString(StringJsonWriter writer, HijrahDate value) {
       if (value == null) {
         writer.writeNull();
       } else {
-        writer.writeString(HIJRAH_DATE_FORMATTER.format(value));
+        writer.writeString(Formatter.INSTANCE.format(value));
       }
     }
 
@@ -2307,7 +2310,7 @@ public final class ScalarCodecs {
       if (value == null) {
         writer.writeNull();
       } else {
-        writer.writeString(HIJRAH_DATE_FORMATTER.format(value));
+        writer.writeString(Formatter.INSTANCE.format(value));
       }
     }
 
@@ -2318,7 +2321,7 @@ public final class ScalarCodecs {
         return null;
       }
       try {
-        return HijrahDate.from(HIJRAH_DATE_FORMATTER.parse(value));
+        return HijrahDate.from(Formatter.INSTANCE.parse(value));
       } catch (RuntimeException e) {
         throw invalidString(HijrahDate.class, value, e);
       }
@@ -2331,7 +2334,7 @@ public final class ScalarCodecs {
         return null;
       }
       try {
-        return HijrahDate.from(HIJRAH_DATE_FORMATTER.parse(value));
+        return HijrahDate.from(Formatter.INSTANCE.parse(value));
       } catch (RuntimeException e) {
         throw invalidString(HijrahDate.class, value, e);
       }
@@ -2344,7 +2347,7 @@ public final class ScalarCodecs {
         return null;
       }
       try {
-        return HijrahDate.from(HIJRAH_DATE_FORMATTER.parse(value));
+        return HijrahDate.from(Formatter.INSTANCE.parse(value));
       } catch (RuntimeException e) {
         throw invalidString(HijrahDate.class, value, e);
       }
@@ -2354,12 +2357,17 @@ public final class ScalarCodecs {
   public static final class JapaneseDateCodec implements 
JsonValueCodec<JapaneseDate> {
     public static final JapaneseDateCodec INSTANCE = new JapaneseDateCodec();
 
+    private static final class Formatter {
+      private static final DateTimeFormatter INSTANCE =
+          
DateTimeFormatter.ISO_LOCAL_DATE.withChronology(JapaneseChronology.INSTANCE);
+    }
+
     @Override
     public void writeString(StringJsonWriter writer, JapaneseDate value) {
       if (value == null) {
         writer.writeNull();
       } else {
-        writer.writeString(JAPANESE_DATE_FORMATTER.format(value));
+        writer.writeString(Formatter.INSTANCE.format(value));
       }
     }
 
@@ -2368,7 +2376,7 @@ public final class ScalarCodecs {
       if (value == null) {
         writer.writeNull();
       } else {
-        writer.writeString(JAPANESE_DATE_FORMATTER.format(value));
+        writer.writeString(Formatter.INSTANCE.format(value));
       }
     }
 
@@ -2379,7 +2387,7 @@ public final class ScalarCodecs {
         return null;
       }
       try {
-        return JapaneseDate.from(JAPANESE_DATE_FORMATTER.parse(value));
+        return JapaneseDate.from(Formatter.INSTANCE.parse(value));
       } catch (RuntimeException e) {
         throw invalidString(JapaneseDate.class, value, e);
       }
@@ -2392,7 +2400,7 @@ public final class ScalarCodecs {
         return null;
       }
       try {
-        return JapaneseDate.from(JAPANESE_DATE_FORMATTER.parse(value));
+        return JapaneseDate.from(Formatter.INSTANCE.parse(value));
       } catch (RuntimeException e) {
         throw invalidString(JapaneseDate.class, value, e);
       }
@@ -2405,7 +2413,7 @@ public final class ScalarCodecs {
         return null;
       }
       try {
-        return JapaneseDate.from(JAPANESE_DATE_FORMATTER.parse(value));
+        return JapaneseDate.from(Formatter.INSTANCE.parse(value));
       } catch (RuntimeException e) {
         throw invalidString(JapaneseDate.class, value, e);
       }
@@ -2415,12 +2423,17 @@ public final class ScalarCodecs {
   public static final class MinguoDateCodec implements 
JsonValueCodec<MinguoDate> {
     public static final MinguoDateCodec INSTANCE = new MinguoDateCodec();
 
+    private static final class Formatter {
+      private static final DateTimeFormatter INSTANCE =
+          
DateTimeFormatter.ISO_LOCAL_DATE.withChronology(MinguoChronology.INSTANCE);
+    }
+
     @Override
     public void writeString(StringJsonWriter writer, MinguoDate value) {
       if (value == null) {
         writer.writeNull();
       } else {
-        writer.writeString(MINGUO_DATE_FORMATTER.format(value));
+        writer.writeString(Formatter.INSTANCE.format(value));
       }
     }
 
@@ -2429,7 +2442,7 @@ public final class ScalarCodecs {
       if (value == null) {
         writer.writeNull();
       } else {
-        writer.writeString(MINGUO_DATE_FORMATTER.format(value));
+        writer.writeString(Formatter.INSTANCE.format(value));
       }
     }
 
@@ -2440,7 +2453,7 @@ public final class ScalarCodecs {
         return null;
       }
       try {
-        return MinguoDate.from(MINGUO_DATE_FORMATTER.parse(value));
+        return MinguoDate.from(Formatter.INSTANCE.parse(value));
       } catch (RuntimeException e) {
         throw invalidString(MinguoDate.class, value, e);
       }
@@ -2453,7 +2466,7 @@ public final class ScalarCodecs {
         return null;
       }
       try {
-        return MinguoDate.from(MINGUO_DATE_FORMATTER.parse(value));
+        return MinguoDate.from(Formatter.INSTANCE.parse(value));
       } catch (RuntimeException e) {
         throw invalidString(MinguoDate.class, value, e);
       }
@@ -2466,7 +2479,7 @@ public final class ScalarCodecs {
         return null;
       }
       try {
-        return MinguoDate.from(MINGUO_DATE_FORMATTER.parse(value));
+        return MinguoDate.from(Formatter.INSTANCE.parse(value));
       } catch (RuntimeException e) {
         throw invalidString(MinguoDate.class, value, e);
       }
@@ -2476,12 +2489,17 @@ public final class ScalarCodecs {
   public static final class ThaiBuddhistDateCodec implements 
JsonValueCodec<ThaiBuddhistDate> {
     public static final ThaiBuddhistDateCodec INSTANCE = new 
ThaiBuddhistDateCodec();
 
+    private static final class Formatter {
+      private static final DateTimeFormatter INSTANCE =
+          
DateTimeFormatter.ISO_LOCAL_DATE.withChronology(ThaiBuddhistChronology.INSTANCE);
+    }
+
     @Override
     public void writeString(StringJsonWriter writer, ThaiBuddhistDate value) {
       if (value == null) {
         writer.writeNull();
       } else {
-        writer.writeString(THAI_BUDDHIST_DATE_FORMATTER.format(value));
+        writer.writeString(Formatter.INSTANCE.format(value));
       }
     }
 
@@ -2490,7 +2508,7 @@ public final class ScalarCodecs {
       if (value == null) {
         writer.writeNull();
       } else {
-        writer.writeString(THAI_BUDDHIST_DATE_FORMATTER.format(value));
+        writer.writeString(Formatter.INSTANCE.format(value));
       }
     }
 
@@ -2501,7 +2519,7 @@ public final class ScalarCodecs {
         return null;
       }
       try {
-        return 
ThaiBuddhistDate.from(THAI_BUDDHIST_DATE_FORMATTER.parse(value));
+        return ThaiBuddhistDate.from(Formatter.INSTANCE.parse(value));
       } catch (RuntimeException e) {
         throw invalidString(ThaiBuddhistDate.class, value, e);
       }
@@ -2514,7 +2532,7 @@ public final class ScalarCodecs {
         return null;
       }
       try {
-        return 
ThaiBuddhistDate.from(THAI_BUDDHIST_DATE_FORMATTER.parse(value));
+        return ThaiBuddhistDate.from(Formatter.INSTANCE.parse(value));
       } catch (RuntimeException e) {
         throw invalidString(ThaiBuddhistDate.class, value, e);
       }
@@ -2527,7 +2545,7 @@ public final class ScalarCodecs {
         return null;
       }
       try {
-        return 
ThaiBuddhistDate.from(THAI_BUDDHIST_DATE_FORMATTER.parse(value));
+        return ThaiBuddhistDate.from(Formatter.INSTANCE.parse(value));
       } catch (RuntimeException e) {
         throw invalidString(ThaiBuddhistDate.class, value, e);
       }
diff --git 
a/java/fory-json/src/main/java/org/apache/fory/json/resolver/JsonTypeResolver.java
 
b/java/fory-json/src/main/java/org/apache/fory/json/resolver/JsonTypeResolver.java
index 127557376..b530cf8f1 100644
--- 
a/java/fory-json/src/main/java/org/apache/fory/json/resolver/JsonTypeResolver.java
+++ 
b/java/fory-json/src/main/java/org/apache/fory/json/resolver/JsonTypeResolver.java
@@ -555,7 +555,8 @@ public final class JsonTypeResolver {
 
   private JsonTypeInfo formatTypeInfo(Type type, Class<?> rawType, JsonFormat 
annotation) {
     sharedRegistry.checkSecure(rawType);
-    JsonValueCodec<?> codec = ScalarCodecs.dateTimeFormatCodec(rawType, 
annotation.pattern());
+    JsonValueCodec<?> codec =
+        ScalarCodecs.dateTimeFormatCodec(rawType, annotation.pattern(), 
annotation.timezone());
     return newTypeInfo(type, rawType, JsonFieldKind.OBJECT, codec, true);
   }
 
diff --git 
a/java/fory-json/src/main/resources/META-INF/native-image/org.apache.fory/fory-json/native-image.properties
 
b/java/fory-json/src/main/resources/META-INF/native-image/org.apache.fory/fory-json/native-image.properties
index 7feb4e41f..4879b688d 100644
--- 
a/java/fory-json/src/main/resources/META-INF/native-image/org.apache.fory/fory-json/native-image.properties
+++ 
b/java/fory-json/src/main/resources/META-INF/native-image/org.apache.fory/fory-json/native-image.properties
@@ -16,8 +16,8 @@
 # under the License.
 
 # Hosted codegen initializes only Fory JSON implementation classes. 
Application providers, models,
-# and custom codecs retain their own initialization policy. ScalarCodecs stays 
runtime initialized
-# because its temporal formatters capture JDK chronology state.
+# and custom codecs retain their own initialization policy. Codec-local 
chronology formatter
+# holders stay runtime initialized because their formatters capture JDK 
chronology state.
 Args=--features=org.apache.fory.json.ForyJsonGraalVMFeature \
     --initialize-at-build-time=org.apache.fory.json.ForyJson,\
     org.apache.fory.json.ForyJsonBuilder,\
@@ -29,4 +29,7 @@ Args=--features=org.apache.fory.json.ForyJsonGraalVMFeature \
     org.apache.fory.json.reader,\
     org.apache.fory.json.resolver,\
     org.apache.fory.json.writer \
-    --initialize-at-run-time=org.apache.fory.json.codec.ScalarCodecs
+    
--initialize-at-run-time=org.apache.fory.json.codec.ScalarCodecs$HijrahDateCodec$Formatter,\
+    org.apache.fory.json.codec.ScalarCodecs$JapaneseDateCodec$Formatter,\
+    org.apache.fory.json.codec.ScalarCodecs$MinguoDateCodec$Formatter,\
+    org.apache.fory.json.codec.ScalarCodecs$ThaiBuddhistDateCodec$Formatter
diff --git 
a/java/fory-json/src/test/java/org/apache/fory/json/ForyJsonGraalVMFeatureJarVerifier.java
 
b/java/fory-json/src/test/java/org/apache/fory/json/ForyJsonGraalVMFeatureJarVerifier.java
index 2fee6272d..1f4f713fc 100644
--- 
a/java/fory-json/src/test/java/org/apache/fory/json/ForyJsonGraalVMFeatureJarVerifier.java
+++ 
b/java/fory-json/src/test/java/org/apache/fory/json/ForyJsonGraalVMFeatureJarVerifier.java
@@ -66,7 +66,11 @@ public final class ForyJsonGraalVMFeatureJarVerifier {
           + "org.apache.fory.json.reader,"
           + "org.apache.fory.json.resolver,"
           + "org.apache.fory.json.writer";
-  private static final String RUNTIME_TARGETS = 
"org.apache.fory.json.codec.ScalarCodecs";
+  private static final String RUNTIME_TARGETS =
+      "org.apache.fory.json.codec.ScalarCodecs$HijrahDateCodec$Formatter,"
+          + 
"org.apache.fory.json.codec.ScalarCodecs$JapaneseDateCodec$Formatter,"
+          + 
"org.apache.fory.json.codec.ScalarCodecs$MinguoDateCodec$Formatter,"
+          + 
"org.apache.fory.json.codec.ScalarCodecs$ThaiBuddhistDateCodec$Formatter";
   private static final String NATIVE_IMAGE_ARGS =
       FEATURE_OPTION
           + " "
diff --git 
a/java/fory-json/src/test/java/org/apache/fory/json/JsonAndroidRuntimeTest.java 
b/java/fory-json/src/test/java/org/apache/fory/json/JsonAndroidRuntimeTest.java
index 22d8649be..0d46d828c 100644
--- 
a/java/fory-json/src/test/java/org/apache/fory/json/JsonAndroidRuntimeTest.java
+++ 
b/java/fory-json/src/test/java/org/apache/fory/json/JsonAndroidRuntimeTest.java
@@ -28,6 +28,7 @@ import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
+import java.time.Instant;
 import java.time.LocalDate;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -115,12 +116,21 @@ public class JsonAndroidRuntimeTest {
     AndroidFormat format = new AndroidFormat();
     format.value = LocalDate.of(2024, 1, 2);
     format.values = Arrays.asList(LocalDate.of(2024, 1, 3), LocalDate.of(2024, 
1, 4));
+    format.instant = Instant.parse("2024-01-02T03:04:05Z");
+    format.instants = Arrays.asList(format.instant, 
format.instant.plusSeconds(3600));
     String formatJson = json.toJson(format);
     assertTrue(formatJson.contains("\"value\":\"02/01/2024\""), formatJson);
     
assertTrue(formatJson.contains("\"values\":[\"03/01/2024\",\"04/01/2024\"]"), 
formatJson);
+    assertTrue(formatJson.contains("\"instant\":\"2024-01-02 11:04:05 
+08:00\""), formatJson);
+    assertTrue(
+        formatJson.contains(
+            "\"instants\":[\"2024-01-02 11:04:05 +08:00\"," + "\"2024-01-02 
12:04:05 +08:00\"]"),
+        formatJson);
     AndroidFormat decodedFormat = json.fromJson(formatJson, 
AndroidFormat.class);
     assertEquals(decodedFormat.value, format.value);
     assertEquals(decodedFormat.values, format.values);
+    assertEquals(decodedFormat.instant, format.instant);
+    assertEquals(decodedFormat.instants, format.instants);
   }
 
   private static List<String> javaCommand(String classPath, Class<?> 
mainClass) {
@@ -230,5 +240,11 @@ public class JsonAndroidRuntimeTest {
 
     @JsonFormat(pattern = "dd/MM/uuuu")
     public List<LocalDate> values;
+
+    @JsonFormat(pattern = "uuuu-MM-dd HH:mm:ss XXX", timezone = 
"Asia/Shanghai")
+    public Instant instant;
+
+    @JsonFormat(pattern = "uuuu-MM-dd HH:mm:ss XXX", timezone = 
"Asia/Shanghai")
+    public List<Instant> instants;
   }
 }
diff --git 
a/java/fory-json/src/test/java/org/apache/fory/json/JsonFormatAnnotationTest.java
 
b/java/fory-json/src/test/java/org/apache/fory/json/JsonFormatAnnotationTest.java
index 1eb837d6a..c492e9217 100644
--- 
a/java/fory-json/src/test/java/org/apache/fory/json/JsonFormatAnnotationTest.java
+++ 
b/java/fory-json/src/test/java/org/apache/fory/json/JsonFormatAnnotationTest.java
@@ -43,11 +43,13 @@ import java.time.chrono.HijrahDate;
 import java.time.chrono.JapaneseDate;
 import java.time.chrono.MinguoDate;
 import java.time.chrono.ThaiBuddhistDate;
+import java.time.format.DateTimeFormatter;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
@@ -71,6 +73,10 @@ import org.testng.annotations.Test;
 
 public class JsonFormatAnnotationTest extends ForyJsonTestModels {
   private static final String DATE_PATTERN = "dd/MM/uuuu";
+  private static final String LOCAL_TIMESTAMP_PATTERN = "uuuu-MM-dd HH:mm:ss";
+  private static final String TIMESTAMP_PATTERN = "uuuu-MM-dd HH:mm:ss XXX";
+  private static final String ZONED_TIMESTAMP_PATTERN = "uuuu-MM-dd HH:mm:ss 
VV";
+  private static final String ZONE_NAME_TIMESTAMP_PATTERN = "uuuu-MM-dd 
HH:mm:ss z";
 
   @Factory(dataProvider = "enableCodegen")
   public JsonFormatAnnotationTest(boolean codegen) {
@@ -138,6 +144,57 @@ public class JsonFormatAnnotationTest extends 
ForyJsonTestModels {
     assertGeneratedWhenSupported(json, TemporalFields.class);
   }
 
+  @Test
+  public void timezoneRoundTrip() {
+    ForyJson json = newJson();
+    Instant instant = Instant.parse("2024-01-02T03:04:05Z");
+    TimezoneFields value = new TimezoneFields();
+    value.instant = instant;
+    value.zonedDateTime = instant.atZone(ZoneId.of("Europe/Paris"));
+    value.offsetDateTime = instant.atOffset(ZoneOffset.ofHours(-4));
+    value.values = Arrays.asList(instant, instant.plusSeconds(3600));
+    String expected =
+        "{\"instant\":\"2024-01-02 11:04:05\","
+            + "\"zonedDateTime\":\"2024-01-02 11:04:05 +08:00\","
+            + "\"offsetDateTime\":\"2024-01-02 11:04:05 +08:00\","
+            + "\"values\":[\"2024-01-02 11:04:05 +08:00\","
+            + "\"2024-01-02 12:04:05 +08:00\"]}";
+    assertEquals(json.toJson(value), expected);
+    assertEquals(new String(json.toJsonBytes(value), StandardCharsets.UTF_8), 
expected);
+    assertTimezoneFields(json.fromJson(expected, TimezoneFields.class), 
instant);
+    assertTimezoneFields(
+        json.fromJson(expected.getBytes(StandardCharsets.UTF_8), 
TimezoneFields.class), instant);
+    assertGeneratedWhenSupported(json, TimezoneFields.class);
+  }
+
+  @Test
+  public void offsetTimezoneParsing() {
+    ForyJson json = newJson();
+    String zoneNameText = "2024-11-03 01:30:00 EST";
+    String input =
+        "{\"local\":\"2024-01-02 11:04:05\","
+            + "\"explicit\":\"2024-01-02 07:04:05 +04:00\","
+            + "\"explicitZone\":\"2024-01-02 04:04:05 Europe/Paris\","
+            + "\"zoneName\":\""
+            + zoneNameText
+            + "\"}";
+    OffsetTimezoneFields value = json.fromJson(input, 
OffsetTimezoneFields.class);
+    Instant instant = Instant.parse("2024-01-02T03:04:05Z");
+    assertEquals(value.local.toInstant(), instant);
+    assertEquals(value.local.getOffset(), ZoneOffset.ofHours(8));
+    assertEquals(value.explicit.toInstant(), instant);
+    assertEquals(value.explicit.getOffset(), ZoneOffset.ofHours(4));
+    assertEquals(value.explicitZone.toInstant(), instant);
+    assertEquals(value.explicitZone.getOffset(), ZoneOffset.ofHours(1));
+    ZonedDateTime resolvedZoneName =
+        DateTimeFormatter.ofPattern(ZONE_NAME_TIMESTAMP_PATTERN, Locale.ROOT)
+            .withZone(ZoneId.of("Asia/Shanghai"))
+            .parse(zoneNameText, ZonedDateTime::from);
+    assertEquals(value.zoneName.toInstant(), resolvedZoneName.toInstant());
+    assertEquals(value.zoneName.getOffset(), resolvedZoneName.getOffset());
+    assertGeneratedWhenSupported(json, OffsetTimezoneFields.class);
+  }
+
   @Test
   public void creatorRoundTrip() {
     ForyJson json = newJson();
@@ -158,15 +215,17 @@ public class JsonFormatAnnotationTest extends 
ForyJsonTestModels {
         compileRecordClass(
             "JsonFormatRecord",
             "package org.apache.fory.json.records;\n"
-                + "import java.time.LocalDate;\n"
+                + "import java.time.Instant;\n"
                 + "import org.apache.fory.json.annotation.JsonFormat;\n"
                 + "public record JsonFormatRecord("
-                + "@JsonFormat(pattern = \"dd/MM/uuuu\") LocalDate value) 
{}\n");
-    Object value = 
type.getConstructor(LocalDate.class).newInstance(LocalDate.of(2024, 1, 2));
+                + "@JsonFormat(pattern = \"uuuu-MM-dd HH:mm:ss XXX\", "
+                + "timezone = \"Asia/Shanghai\") Instant value) {}\n");
+    Instant value = Instant.parse("2024-01-02T03:04:05Z");
+    Object record = type.getConstructor(Instant.class).newInstance(value);
     for (ForyJson json : new ForyJson[] {newJson(), 
newJsonBuilder().withFieldMode(true).build()}) {
-      assertEquals(json.toJson(value), "{\"value\":\"02/01/2024\"}");
-      Object decoded = json.fromJson("{\"value\":\"03/01/2024\"}", type);
-      assertEquals(type.getMethod("value").invoke(decoded), LocalDate.of(2024, 
1, 3));
+      assertEquals(json.toJson(record), "{\"value\":\"2024-01-02 11:04:05 
+08:00\"}");
+      Object decoded = json.fromJson("{\"value\":\"2024-01-02 12:04:05 
+08:00\"}", type);
+      assertEquals(type.getMethod("value").invoke(decoded), 
value.plusSeconds(3600));
     }
   }
 
@@ -174,11 +233,11 @@ public class JsonFormatAnnotationTest extends 
ForyJsonTestModels {
   public void mixinRoundTrip() {
     ForyJson mixinJson = 
newJsonBuilder().registerMixin(FormatMixin.class).build();
     MixinTarget value = new MixinTarget();
-    value.value = LocalDate.of(2024, 1, 2);
-    assertEquals(mixinJson.toJson(value), "{\"value\":\"02/01/2024\"}");
+    value.value = Instant.parse("2024-01-02T03:04:05Z");
+    assertEquals(mixinJson.toJson(value), "{\"value\":\"2024-01-02 11:04:05 
+08:00\"}");
     assertEquals(
-        mixinJson.fromJson("{\"value\":\"03/01/2024\"}", 
MixinTarget.class).value,
-        LocalDate.of(2024, 1, 3));
+        mixinJson.fromJson("{\"value\":\"2024-01-02 12:04:05 +08:00\"}", 
MixinTarget.class).value,
+        value.value.plusSeconds(3600));
 
     ForyJson removalJson = 
newJsonBuilder().registerMixin(RemoveFormatMixin.class).build();
     IntrinsicTarget intrinsic = new IntrinsicTarget();
@@ -219,6 +278,11 @@ public class JsonFormatAnnotationTest extends 
ForyJsonTestModels {
     ForyJson json = newJson();
     assertThrows(ForyJsonException.class, () -> json.toJson(new 
EmptyPattern()));
     assertThrows(ForyJsonException.class, () -> json.toJson(new 
InvalidPattern()));
+    ForyJsonException timezoneError =
+        expectThrows(ForyJsonException.class, () -> json.toJson(new 
InvalidTimezone()));
+    assertTrue(timezoneError.getMessage().contains("timezone"), 
timezoneError.getMessage());
+    assertThrows(ForyJsonException.class, () -> json.toJson(new 
LocalTimezone()));
+    assertThrows(ForyJsonException.class, () -> json.toJson(new 
OffsetTimeTimezone()));
     assertThrows(ForyJsonException.class, () -> json.toJson(new 
StringFormat()));
     assertThrows(ForyJsonException.class, () -> json.toJson(new 
DurationFormat()));
     assertThrows(ForyJsonException.class, () -> json.toJson(new 
LegacyDateFormat()));
@@ -254,6 +318,15 @@ public class JsonFormatAnnotationTest extends 
ForyJsonTestModels {
     assertEquals(value.atomicArray.get(1), null);
   }
 
+  private static void assertTimezoneFields(TimezoneFields value, Instant 
instant) {
+    assertEquals(value.instant, instant);
+    assertEquals(value.zonedDateTime.toInstant(), instant);
+    assertEquals(value.zonedDateTime.getZone(), ZoneId.of("Asia/Shanghai"));
+    assertEquals(value.offsetDateTime.toInstant(), instant);
+    assertEquals(value.offsetDateTime.getOffset(), ZoneOffset.ofHours(8));
+    assertEquals(value.values, Arrays.asList(instant, 
instant.plusSeconds(3600)));
+  }
+
   private static TemporalFields temporalFields() {
     TemporalFields value = new TemporalFields();
     Instant instant = Instant.parse("2024-01-02T03:04:05.006Z");
@@ -369,6 +442,34 @@ public class JsonFormatAnnotationTest extends 
ForyJsonTestModels {
     public ThaiBuddhistDate thaiBuddhistDate;
   }
 
+  public static final class TimezoneFields {
+    @JsonFormat(pattern = LOCAL_TIMESTAMP_PATTERN, timezone = "Asia/Shanghai")
+    public Instant instant;
+
+    @JsonFormat(pattern = TIMESTAMP_PATTERN, timezone = "Asia/Shanghai")
+    public ZonedDateTime zonedDateTime;
+
+    @JsonFormat(pattern = TIMESTAMP_PATTERN, timezone = "Asia/Shanghai")
+    public OffsetDateTime offsetDateTime;
+
+    @JsonFormat(pattern = TIMESTAMP_PATTERN, timezone = "Asia/Shanghai")
+    public List<Instant> values;
+  }
+
+  public static final class OffsetTimezoneFields {
+    @JsonFormat(pattern = LOCAL_TIMESTAMP_PATTERN, timezone = "Asia/Shanghai")
+    public OffsetDateTime local;
+
+    @JsonFormat(pattern = TIMESTAMP_PATTERN, timezone = "Asia/Shanghai")
+    public OffsetDateTime explicit;
+
+    @JsonFormat(pattern = ZONED_TIMESTAMP_PATTERN, timezone = "Asia/Shanghai")
+    public OffsetDateTime explicitZone;
+
+    @JsonFormat(pattern = ZONE_NAME_TIMESTAMP_PATTERN, timezone = 
"Asia/Shanghai")
+    public OffsetDateTime zoneName;
+  }
+
   public static final class CreatorField {
     @JsonFormat(pattern = DATE_PATTERN)
     public final LocalDate value;
@@ -380,13 +481,13 @@ public class JsonFormatAnnotationTest extends 
ForyJsonTestModels {
   }
 
   public static final class MixinTarget {
-    public LocalDate value;
+    public Instant value;
   }
 
   @JsonMixin(target = MixinTarget.class)
   public abstract static class FormatMixin {
-    @JsonFormat(pattern = DATE_PATTERN)
-    LocalDate value;
+    @JsonFormat(pattern = TIMESTAMP_PATTERN, timezone = "Asia/Shanghai")
+    Instant value;
   }
 
   public static final class IntrinsicTarget {
@@ -464,6 +565,21 @@ public class JsonFormatAnnotationTest extends 
ForyJsonTestModels {
     public LocalDate value;
   }
 
+  public static final class InvalidTimezone {
+    @JsonFormat(pattern = TIMESTAMP_PATTERN, timezone = "Not/AZone")
+    public Instant value;
+  }
+
+  public static final class LocalTimezone {
+    @JsonFormat(pattern = DATE_PATTERN, timezone = "UTC")
+    public LocalDate value;
+  }
+
+  public static final class OffsetTimeTimezone {
+    @JsonFormat(pattern = "HH:mm:ss XXX", timezone = "UTC")
+    public OffsetTime value;
+  }
+
   public static final class StringFormat {
     @JsonFormat(pattern = DATE_PATTERN)
     public String value;


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to