未验证 提交 e46d0697 编写于 作者: I Igor Dianov 提交者: GitHub

fix: add support for ISO date/time format with offset in string to date converter (#3802)

* fix: add support for ISO date/time format in string to date converter

* fix: better test name
上级 096fb2ce
......@@ -15,16 +15,29 @@
*/
package org.activiti.api.runtime.model.impl;
import java.time.Instant;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.TimeZone;
import org.springframework.core.convert.converter.Converter;
@ProcessVariableTypeConverter
public class StringToDateConverter implements Converter<String, Date> {
private static SimpleDateFormat ISO_DATE_TIME = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
static {
ISO_DATE_TIME.setTimeZone(TimeZone.getTimeZone("UTC"));
}
@Override
public Date convert(String source) {
return Date.from(Instant.parse(source));
try {
return ISO_DATE_TIME.parse(source);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}
/*
* Copyright 2010-2020 Alfresco Software, Ltd.
*
* 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
*
* 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.activiti.api.runtime.model.impl;
import org.junit.jupiter.api.Test;
import java.time.Instant;
import java.util.Date;
import static org.assertj.core.api.Assertions.assertThat;
class StringToDateConverterTest {
private StringToDateConverter subject = new StringToDateConverter();
@Test
void convertISODateTimeUTC() {
//given
String source = "2022-01-17T00:00:00.000Z";
//when
Date result = subject.convert(source);
//then
assertThat(result).isEqualTo(Instant.parse(source));
}
@Test
void convertISODateTimeOffset() {
//given
String source = "2022-01-17T00:00:00.000-00:00";
//when
Date result = subject.convert(source);
//then
assertThat(result).isEqualTo(Date.from(Instant.parse("2022-01-17T00:00:00.000Z")));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册