Skip to content

Introduce DurationEditor for java.time.Duration support #28084

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.time.Duration;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -57,6 +58,7 @@
import org.springframework.beans.propertyeditors.CustomCollectionEditor;
import org.springframework.beans.propertyeditors.CustomMapEditor;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.beans.propertyeditors.DurationEditor;
import org.springframework.beans.propertyeditors.FileEditor;
import org.springframework.beans.propertyeditors.InputSourceEditor;
import org.springframework.beans.propertyeditors.InputStreamEditor;
Expand Down Expand Up @@ -216,6 +218,7 @@ private void createDefaultEditors() {
this.defaultEditors.put(Class.class, new ClassEditor());
this.defaultEditors.put(Class[].class, new ClassArrayEditor());
this.defaultEditors.put(Currency.class, new CurrencyEditor());
this.defaultEditors.put(Duration.class, new DurationEditor());
this.defaultEditors.put(File.class, new FileEditor());
this.defaultEditors.put(InputStream.class, new InputStreamEditor());
if (!shouldIgnoreXml) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.beans.propertyeditors;

import java.beans.PropertyEditorSupport;
import java.time.Duration;
import java.util.Objects;

import org.springframework.util.StringUtils;

/**
* Editor for {@code java.time.Duration}, translating durations into
* {@code Duration} objects. Exposes the {@code Duration} ISO as a text
* representation.
*
* @author Davide Angelocola
* @since 3.0
* @see Duration
*/
public class DurationEditor extends PropertyEditorSupport {

@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(StringUtils.parseDuration(text));
}

@Override
public String getAsText() {
Duration value = (Duration) getValue();
return Objects.toString(value, "");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import java.io.ByteArrayOutputStream;
import java.nio.charset.Charset;
import java.time.Duration;
import java.time.format.DateTimeParseException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -37,6 +39,7 @@

import org.springframework.lang.Nullable;


/**
* Miscellaneous {@link String} utility methods.
*
Expand Down Expand Up @@ -891,6 +894,25 @@ public static TimeZone parseTimeZoneString(String timeZoneString) {
return timeZone;
}

/**
* Parse the given {@code durationString} value into a {@link Duration}.
* @param durationString the duration {@code String}, following ISO 8601 format starting with "PT". It is
* also possible to skip PT prefix (i.e. both "PT10s" and "10s" are accepted).
* @return a corresponding {@link Duration} instance or null if @{code durationString} is null.
* @throws DateTimeParseException in case of an invalid duration
*/
@Nullable
public static Duration parseDuration(@Nullable String durationString) {
if (durationString == null) {
return null;
}
else if (durationString.startsWith("PT")) {
return Duration.parse(durationString);
}
else {
return Duration.parse("PT" + durationString);
}
}

//---------------------------------------------------------------------
// Convenience methods for working with String arrays
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.util;

import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
import java.util.Locale;
Expand Down Expand Up @@ -773,4 +774,11 @@ void collectionToDelimitedStringWithNullValuesShouldNotFail() {
assertThat(StringUtils.collectionToCommaDelimitedString(Collections.singletonList(null))).isEqualTo("null");
}

@Test
void parseDuration() {
assertThat(StringUtils.parseDuration(null)).isNull();
assertThat(StringUtils.parseDuration("PT1s")).isEqualTo(Duration.ofSeconds(1));
assertThat(StringUtils.parseDuration("1s")).isEqualTo(Duration.ofSeconds(1));
}

}