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

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


The following commit(s) were added to refs/heads/main by this push:
     new 8fa8582c709a CAMEL-27526: camel-base - type converters from String to 
OffsetDateTime, ZonedDateTime and Instant (#30738)
8fa8582c709a is described below

commit 8fa8582c709a2e1b7ac2fe14fbc15afd3682feb3
Author: Claus Ibsen <[email protected]>
AuthorDate: Wed Nov 25 08:09:43 2026 +0100

    CAMEL-27526: camel-base - type converters from String to OffsetDateTime, 
ZonedDateTime and Instant (#30738)
    
    
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Signed-off-by: Claus Ibsen <[email protected]>
---
 .../converter/CamelBaseBulkConverterLoader.java    |  44 +++++-
 .../apache/camel/converter/DateTimeConverter.java  |  52 +++++++
 .../camel/converter/DateTimeConverterTest.java     |  63 ++++++++
 .../converter/JavaTimeEndpointOptionTest.java      | 162 +++++++++++++++++++++
 .../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc    |  11 ++
 5 files changed, 331 insertions(+), 1 deletion(-)

diff --git 
a/core/camel-base/src/generated/java/org/apache/camel/converter/CamelBaseBulkConverterLoader.java
 
b/core/camel-base/src/generated/java/org/apache/camel/converter/CamelBaseBulkConverterLoader.java
index ff3ecc3fad1a..19e1877affb8 100644
--- 
a/core/camel-base/src/generated/java/org/apache/camel/converter/CamelBaseBulkConverterLoader.java
+++ 
b/core/camel-base/src/generated/java/org/apache/camel/converter/CamelBaseBulkConverterLoader.java
@@ -41,7 +41,7 @@ public final class CamelBaseBulkConverterLoader implements 
TypeConverterLoader,
 
     @Override
     public int size() {
-        return 130;
+        return 133;
     }
 
     @Override
@@ -446,6 +446,33 @@ public final class CamelBaseBulkConverterLoader implements 
TypeConverterLoader,
             if (value instanceof java.lang.String) {
                 return 
org.apache.camel.converter.DurationConverter.toDuration((java.lang.String) 
value);
             }
+        } else if (to == java.time.Instant.class) {
+            if (value instanceof java.lang.String) {
+                Object obj = 
org.apache.camel.converter.DateTimeConverter.toInstant((java.lang.String) 
value);
+                if (obj == null) {
+                    return Void.class;
+                } else {
+                    return obj;
+                }
+            }
+        } else if (to == java.time.OffsetDateTime.class) {
+            if (value instanceof java.lang.String) {
+                Object obj = 
org.apache.camel.converter.DateTimeConverter.toOffsetDateTime((java.lang.String)
 value);
+                if (obj == null) {
+                    return Void.class;
+                } else {
+                    return obj;
+                }
+            }
+        } else if (to == java.time.ZonedDateTime.class) {
+            if (value instanceof java.lang.String) {
+                Object obj = 
org.apache.camel.converter.DateTimeConverter.toZonedDateTime((java.lang.String) 
value);
+                if (obj == null) {
+                    return Void.class;
+                } else {
+                    return obj;
+                }
+            }
         } else if (to == java.util.ArrayList.class) {
             if (value instanceof java.util.Iterator) {
                 return 
org.apache.camel.converter.CollectionConverter.toArrayList((java.util.Iterator) 
value);
@@ -640,6 +667,9 @@ public final class CamelBaseBulkConverterLoader implements 
TypeConverterLoader,
         registry.addConverter(new TypeConvertible<>(java.lang.Long.class, 
java.sql.Timestamp.class), this);
         registry.addConverter(new TypeConvertible<>(java.lang.Long.class, 
java.time.Duration.class), this);
         registry.addConverter(new TypeConvertible<>(java.lang.String.class, 
java.time.Duration.class), this);
+        registry.addConverter(new TypeConvertible<>(java.lang.String.class, 
java.time.Instant.class), this);
+        registry.addConverter(new TypeConvertible<>(java.lang.String.class, 
java.time.OffsetDateTime.class), this);
+        registry.addConverter(new TypeConvertible<>(java.lang.String.class, 
java.time.ZonedDateTime.class), this);
         registry.addConverter(new TypeConvertible<>(java.util.Iterator.class, 
java.util.ArrayList.class), this);
         registry.addConverter(new TypeConvertible<>(java.lang.Iterable.class, 
java.util.ArrayList.class), this);
         registry.addConverter(new TypeConvertible<>(java.util.Map.class, 
java.util.Collection.class), this);
@@ -1019,6 +1049,18 @@ public final class CamelBaseBulkConverterLoader 
implements TypeConverterLoader,
             if (from == java.lang.String.class) {
                 return this;
             }
+        } else if (to == java.time.Instant.class) {
+            if (from == java.lang.String.class) {
+                return this;
+            }
+        } else if (to == java.time.OffsetDateTime.class) {
+            if (from == java.lang.String.class) {
+                return this;
+            }
+        } else if (to == java.time.ZonedDateTime.class) {
+            if (from == java.lang.String.class) {
+                return this;
+            }
         } else if (to == java.util.ArrayList.class) {
             if (from == java.util.Iterator.class) {
                 return this;
diff --git 
a/core/camel-base/src/main/java/org/apache/camel/converter/DateTimeConverter.java
 
b/core/camel-base/src/main/java/org/apache/camel/converter/DateTimeConverter.java
index e81c133000c4..ccc8da04f1c9 100644
--- 
a/core/camel-base/src/main/java/org/apache/camel/converter/DateTimeConverter.java
+++ 
b/core/camel-base/src/main/java/org/apache/camel/converter/DateTimeConverter.java
@@ -16,12 +16,16 @@
  */
 package org.apache.camel.converter;
 
+import java.time.Instant;
+import java.time.OffsetDateTime;
+import java.time.ZonedDateTime;
 import java.util.Date;
 import java.util.Locale;
 import java.util.TimeZone;
 import java.util.concurrent.TimeUnit;
 
 import org.apache.camel.Converter;
+import org.apache.camel.util.ObjectHelper;
 
 /**
  * Date and time related converters.
@@ -64,4 +68,52 @@ public final class DateTimeConverter {
             default -> throw new IllegalStateException("Unexpected value: " + 
unit);
         };
     }
+
+    /**
+     * Converts an ISO-8601 date-time with an offset, such as {@code 
2026-11-18T23:00:00+01:00} or
+     * {@code 2026-11-18T22:00:00Z}, so that an option of this type can be set 
in an endpoint URI or as a property. An
+     * empty value converts to {@code null}.
+     * <p/>
+     * An ISO-8601 date-time never contains a space, so a space is read as a 
{@code +}: in an endpoint URI the {@code +}
+     * of a positive offset is decoded to a space, unless the value is written 
as {@code %2B} or in {@code RAW(...)}.
+     */
+    @Converter(order = 5, allowNull = true)
+    public static OffsetDateTime toOffsetDateTime(String value) {
+        String text = toIsoText(value);
+        return text != null ? OffsetDateTime.parse(text) : null;
+    }
+
+    /**
+     * Converts an ISO-8601 date-time with an offset and an optional zone id, 
such as
+     * {@code 2026-11-18T23:00:00+01:00[Europe/Paris]} or {@code 
2026-11-18T22:00:00Z}, so that an option of this type
+     * can be set in an endpoint URI or as a property. An empty value converts 
to {@code null}.
+     * <p/>
+     * An ISO-8601 date-time never contains a space, so a space is read as a 
{@code +}: in an endpoint URI the {@code +}
+     * of a positive offset is decoded to a space, unless the value is written 
as {@code %2B} or in {@code RAW(...)}.
+     */
+    @Converter(order = 6, allowNull = true)
+    public static ZonedDateTime toZonedDateTime(String value) {
+        String text = toIsoText(value);
+        return text != null ? ZonedDateTime.parse(text) : null;
+    }
+
+    /**
+     * Converts an ISO-8601 instant, such as {@code 2026-11-18T22:00:00Z} or 
{@code 2026-11-18T23:00:00+01:00}, so that
+     * an option of this type can be set in an endpoint URI or as a property. 
An empty value converts to {@code null}.
+     * <p/>
+     * An ISO-8601 date-time never contains a space, so a space is read as a 
{@code +}: in an endpoint URI the {@code +}
+     * of a positive offset is decoded to a space, unless the value is written 
as {@code %2B} or in {@code RAW(...)}.
+     */
+    @Converter(order = 7, allowNull = true)
+    public static Instant toInstant(String value) {
+        String text = toIsoText(value);
+        return text != null ? Instant.parse(text) : null;
+    }
+
+    private static String toIsoText(String value) {
+        if (ObjectHelper.isEmpty(value)) {
+            return null;
+        }
+        return value.trim().replace(' ', '+');
+    }
 }
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/converter/DateTimeConverterTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/converter/DateTimeConverterTest.java
index 8c6204d3836b..4273c52e4ab3 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/converter/DateTimeConverterTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/converter/DateTimeConverterTest.java
@@ -16,15 +16,23 @@
  */
 package org.apache.camel.converter;
 
+import java.time.Instant;
+import java.time.OffsetDateTime;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.time.ZonedDateTime;
 import java.util.Date;
 import java.util.TimeZone;
 import java.util.concurrent.TimeUnit;
 
 import org.apache.camel.ContextTestSupport;
+import org.apache.camel.TypeConversionException;
 import org.junit.jupiter.api.Test;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 /**
  *
@@ -61,4 +69,59 @@ public class DateTimeConverterTest extends 
ContextTestSupport {
         assertEquals(TimeUnit.MILLISECONDS, 
context.getTypeConverter().convertTo(TimeUnit.class, "MILLISECONDS"));
         assertEquals(TimeUnit.MICROSECONDS, 
context.getTypeConverter().convertTo(TimeUnit.class, "MICROSECONDS"));
     }
+
+    @Test
+    public void testToOffsetDateTime() {
+        assertEquals(OffsetDateTime.of(2026, 11, 18, 23, 0, 0, 0, 
ZoneOffset.ofHours(1)),
+                context.getTypeConverter().convertTo(OffsetDateTime.class, 
"2026-11-18T23:00:00+01:00"));
+        assertEquals(OffsetDateTime.of(2026, 11, 18, 22, 0, 0, 0, 
ZoneOffset.UTC),
+                context.getTypeConverter().convertTo(OffsetDateTime.class, " 
2026-11-18T22:00:00Z "));
+        assertEquals(OffsetDateTime.of(2026, 11, 18, 17, 0, 0, 0, 
ZoneOffset.ofHours(-5)),
+                context.getTypeConverter().convertTo(OffsetDateTime.class, 
"2026-11-18T17:00:00-05:00"));
+    }
+
+    @Test
+    public void testToOffsetDateTimeSpaceIsPlus() {
+        // the + of a positive offset is decoded to a space in an endpoint uri
+        assertEquals(OffsetDateTime.of(2026, 11, 18, 23, 0, 0, 0, 
ZoneOffset.ofHours(1)),
+                context.getTypeConverter().convertTo(OffsetDateTime.class, 
"2026-11-18T23:00:00 01:00"));
+    }
+
+    @Test
+    public void testToZonedDateTime() {
+        assertEquals(ZonedDateTime.of(2026, 11, 18, 23, 0, 0, 0, 
ZoneOffset.ofHours(1)),
+                context.getTypeConverter().convertTo(ZonedDateTime.class, 
"2026-11-18T23:00:00+01:00"));
+        assertEquals(ZonedDateTime.of(2026, 11, 18, 22, 0, 0, 0, 
ZoneOffset.UTC),
+                context.getTypeConverter().convertTo(ZonedDateTime.class, 
"2026-11-18T22:00:00Z"));
+        assertEquals(ZonedDateTime.of(2026, 11, 18, 23, 0, 0, 0, 
ZoneId.of("Europe/Paris")),
+                context.getTypeConverter().convertTo(ZonedDateTime.class, 
"2026-11-18T23:00:00+01:00[Europe/Paris]"));
+        assertEquals(ZonedDateTime.of(2026, 11, 18, 23, 0, 0, 0, 
ZoneId.of("Europe/Paris")),
+                context.getTypeConverter().convertTo(ZonedDateTime.class, 
"2026-11-18T23:00:00 01:00[Europe/Paris]"));
+    }
+
+    @Test
+    public void testToInstant() {
+        Instant expected = Instant.parse("2026-11-18T22:00:00Z");
+        assertEquals(expected, 
context.getTypeConverter().convertTo(Instant.class, "2026-11-18T22:00:00Z"));
+        assertEquals(expected, 
context.getTypeConverter().convertTo(Instant.class, 
"2026-11-18T23:00:00+01:00"));
+        assertEquals(expected, 
context.getTypeConverter().convertTo(Instant.class, "2026-11-18T23:00:00 
01:00"));
+    }
+
+    @Test
+    public void testJavaTimeEmptyIsNull() {
+        assertNull(context.getTypeConverter().convertTo(OffsetDateTime.class, 
""));
+        assertNull(context.getTypeConverter().convertTo(ZonedDateTime.class, " 
"));
+        assertNull(context.getTypeConverter().convertTo(Instant.class, ""));
+    }
+
+    @Test
+    public void testJavaTimeInvalid() {
+        assertThrows(TypeConversionException.class,
+                () -> 
context.getTypeConverter().convertTo(OffsetDateTime.class, "2026-11-18"));
+        assertThrows(TypeConversionException.class,
+                () -> 
context.getTypeConverter().convertTo(ZonedDateTime.class, "tomorrow"));
+        // an instant needs an offset
+        assertThrows(TypeConversionException.class,
+                () -> context.getTypeConverter().convertTo(Instant.class, 
"2026-11-18T23:00:00"));
+    }
 }
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/converter/JavaTimeEndpointOptionTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/converter/JavaTimeEndpointOptionTest.java
new file mode 100644
index 000000000000..cff79ea4834d
--- /dev/null
+++ 
b/core/camel-core/src/test/java/org/apache/camel/converter/JavaTimeEndpointOptionTest.java
@@ -0,0 +1,162 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.converter;
+
+import java.time.Instant;
+import java.time.OffsetDateTime;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.time.ZonedDateTime;
+import java.util.Map;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Component;
+import org.apache.camel.Consumer;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.Expression;
+import org.apache.camel.Processor;
+import org.apache.camel.Producer;
+import org.apache.camel.TypeConversionException;
+import org.apache.camel.support.DefaultComponent;
+import org.apache.camel.support.DefaultEndpoint;
+import org.apache.camel.support.DefaultExchange;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+/**
+ * Endpoint options of type OffsetDateTime, ZonedDateTime and Instant set in 
an endpoint uri.
+ */
+public class JavaTimeEndpointOptionTest extends ContextTestSupport {
+
+    private static final OffsetDateTime EXPECTED = OffsetDateTime.of(2026, 11, 
18, 23, 0, 0, 0, ZoneOffset.ofHours(1));
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext answer = super.createCamelContext();
+        answer.addComponent("mytime", new MyComponent());
+        return answer;
+    }
+
+    @Test
+    public void testPlusInUri() {
+        // a plain + in the uri is decoded to a space, which the converters 
read as a +
+        MyEndpoint endpoint = 
context.getEndpoint("mytime:plus?offsetDateTime=2026-11-18T23:00:00+01:00"
+                                                  + 
"&zonedDateTime=2026-11-18T23:00:00+01:00"
+                                                  + 
"&instant=2026-11-18T23:00:00+01:00",
+                MyEndpoint.class);
+        assertEquals(EXPECTED, endpoint.getOffsetDateTime());
+        assertEquals(EXPECTED.toZonedDateTime(), endpoint.getZonedDateTime());
+        assertEquals(EXPECTED.toInstant(), endpoint.getInstant());
+    }
+
+    @Test
+    public void testEncodedPlusAndRawInUri() {
+        MyEndpoint endpoint = 
context.getEndpoint("mytime:encoded?offsetDateTime=2026-11-18T23:00:00%2B01:00"
+                                                  + 
"&zonedDateTime=RAW(2026-11-18T23:00:00+01:00)"
+                                                  + 
"&instant=2026-11-18T22:00:00Z",
+                MyEndpoint.class);
+        assertEquals(EXPECTED, endpoint.getOffsetDateTime());
+        assertEquals(EXPECTED.toZonedDateTime(), endpoint.getZonedDateTime());
+        assertEquals(EXPECTED.toInstant(), endpoint.getInstant());
+    }
+
+    @Test
+    public void testZoneIdInUri() {
+        MyEndpoint endpoint = context.getEndpoint(
+                
"mytime:zone?zonedDateTime=2026-11-18T23:00:00+01:00[Europe/Paris]", 
MyEndpoint.class);
+        assertEquals(ZonedDateTime.of(2026, 11, 18, 23, 0, 0, 0, 
ZoneId.of("Europe/Paris")), endpoint.getZonedDateTime());
+        assertNull(endpoint.getOffsetDateTime());
+        assertNull(endpoint.getInstant());
+    }
+
+    @Test
+    public void testHeaderAs() {
+        Exchange exchange = new DefaultExchange(context);
+        exchange.getMessage().setHeader("when", "2026-11-18T22:00:00Z");
+        Object value = 
context.resolveLanguage("simple").createExpression("${headerAs(when, 
java.time.Instant)}")
+                .evaluate(exchange, Object.class);
+        assertEquals(EXPECTED.toInstant(), value);
+    }
+
+    @Test
+    public void testHeaderAsInvalid() {
+        Exchange exchange = new DefaultExchange(context);
+        exchange.getMessage().setHeader("when", "tomorrow");
+        Expression expression = 
context.resolveLanguage("simple").createExpression("${headerAs(when, 
java.time.Instant)}");
+        assertThrows(TypeConversionException.class, () -> 
expression.evaluate(exchange, Object.class));
+    }
+
+    public static final class MyEndpoint extends DefaultEndpoint {
+
+        private OffsetDateTime offsetDateTime;
+        private ZonedDateTime zonedDateTime;
+        private Instant instant;
+
+        private MyEndpoint(String endpointUri, Component component) {
+            super(endpointUri, component);
+        }
+
+        @Override
+        public Producer createProducer() {
+            return null;
+        }
+
+        @Override
+        public Consumer createConsumer(Processor processor) {
+            return null;
+        }
+
+        public OffsetDateTime getOffsetDateTime() {
+            return offsetDateTime;
+        }
+
+        public void setOffsetDateTime(OffsetDateTime offsetDateTime) {
+            this.offsetDateTime = offsetDateTime;
+        }
+
+        public ZonedDateTime getZonedDateTime() {
+            return zonedDateTime;
+        }
+
+        public void setZonedDateTime(ZonedDateTime zonedDateTime) {
+            this.zonedDateTime = zonedDateTime;
+        }
+
+        public Instant getInstant() {
+            return instant;
+        }
+
+        public void setInstant(Instant instant) {
+            this.instant = instant;
+        }
+    }
+
+    public static final class MyComponent extends DefaultComponent {
+
+        @Override
+        protected Endpoint createEndpoint(String uri, String remaining, 
Map<String, Object> parameters) throws Exception {
+            MyEndpoint answer = new MyEndpoint(uri, this);
+            setProperties(answer, parameters);
+            return answer;
+        }
+    }
+}
diff --git 
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc 
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
index dfc8906148fc..b4bd5a962a8f 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
@@ -297,6 +297,17 @@ synchronous step did:
 * With `ignoreInvalidEndpoints`, an endpoint whose producer cannot be created 
is ignored also when it is the last one of
   the slip. Before, the message failed with a 
`FailedToCreateProducerException`.
 
+=== camel-core - String converts to OffsetDateTime, ZonedDateTime and Instant
+
+Camel now has type converters from `String` to `java.time.OffsetDateTime`, 
`java.time.ZonedDateTime` and
+`java.time.Instant`. They read ISO-8601 only, such as 
`2026-11-18T23:00:00+01:00`, `2026-11-18T22:00:00Z` or
+`2026-11-18T23:00:00+01:00[Europe/Paris]` (zone id for `ZonedDateTime` only), 
and an empty string converts to `null`.
+A space is read as `+`, as an endpoint URI decodes the `+` of a positive 
offset to a space; `%2B` and `RAW(...)` work
+too. Endpoint options of these types, such as `scheduledEnqueueTime` of 
`azure-servicebus`, `modifiedSince` of `minio`
+or `timestamp` of `aws2-cw`, can now be set in an endpoint URI or as a 
property; before, this failed with
+`No type converter available`. An expression such as `${headerAs(when, 
java.time.Instant)}` on an ISO-8601 string now
+gives the converted value instead of `null`, and a value that is not ISO-8601 
now fails the conversion.
+
 === camel-kamelet - the output data type of a Kamelet is applied when it hands 
the message on
 
 A Kamelet that declares a default output data type 
(`spec.dataTypes.out.default`) now gives the message that type

Reply via email to