Skip to content

support convertion to and from String representations of java.time.D… #186

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
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 @@ -19,11 +19,7 @@
import static java.time.LocalDateTime.*;
import static java.time.ZoneId.*;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -66,6 +62,10 @@ public abstract class Jsr310Converters {
converters.add(InstantToDateConverter.INSTANCE);
converters.add(ZoneIdToStringConverter.INSTANCE);
converters.add(StringToZoneIdConverter.INSTANCE);
converters.add(DurationToStringConverter.INSTANCE);
converters.add(StringToDurationConverter.INSTANCE);
converters.add(PeriodToStringConverter.INSTANCE);
converters.add(StringToPeriodConverter.INSTANCE);

return converters;
}
Expand Down Expand Up @@ -181,4 +181,58 @@ public ZoneId convert(String source) {
return ZoneId.of(source);
}
}

/**
* enable jsr-310 {@link java.time.Duration} write
*/
@WritingConverter
public static enum DurationToStringConverter implements Converter<Duration, String> {
INSTANCE;

@Override
public String convert(Duration duration) {
return duration.toString();
}
}

/**
* enable jsr-310 {@link java.time.Duration} read
*/
@ReadingConverter
public static enum StringToDurationConverter implements Converter<String, Duration> {

INSTANCE;

@Override
public Duration convert(String s) {
return Duration.parse(s);
}
}

/**
* enable jsr-310 {@link java.time.Period} write
*/
@WritingConverter
public static enum PeriodToStringConverter implements Converter<Period, String> {
INSTANCE;

@Override
public String convert(Period period) {
return period.toString();
}
}

/**
* enable jsr-310 {@link java.time.Period} read
*/
@ReadingConverter
public static enum StringToPeriodConverter implements Converter<String, Period> {

INSTANCE;

@Override
public Period convert(String s) {
return Period.parse(s);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@
import static org.junit.Assert.*;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -37,7 +33,7 @@
/**
* Unit tests for {@link Jsr310Converters}.
*
* @author Oliver Gierke
* @author Oliver Gierke & Barak Schoster
*/
public class Jsr310ConvertersUnitTests {

Expand Down Expand Up @@ -143,6 +139,38 @@ public void convertsZoneIdToStringAndBack() {
}
}

@Test
public void convertsDurationToStringAndBack() {

Map<String, Duration> ids = new HashMap<String, Duration>();
ids.put("PT240H", Duration.ofDays(10));
ids.put("PT2H", Duration.ofHours(2));
ids.put("PT3M", Duration.ofMinutes(3));
ids.put("PT4S", Duration.ofSeconds(4));
ids.put("PT0.005S", Duration.ofMillis(5));
ids.put("PT0.000000006S", Duration.ofNanos(6));


for (Entry<String, Duration> entry : ids.entrySet()) {
assertThat(CONVERSION_SERVICE.convert(entry.getValue(), String.class), is(entry.getKey()));
assertThat(CONVERSION_SERVICE.convert(entry.getKey(), Duration.class), is(entry.getValue()));
}
}

@Test
public void convertsPeriodToStringAndBack() {

Map<String, Period> ids = new HashMap<String, Period>();
ids.put("P2D", Period.ofDays(2));
ids.put("P21D", Period.ofWeeks(3));
ids.put("P4M", Period.ofMonths(4));
ids.put("P5Y", Period.ofYears(5));

for (Entry<String, Period> entry : ids.entrySet()) {
assertThat(CONVERSION_SERVICE.convert(entry.getValue(), String.class), is(entry.getKey()));
assertThat(CONVERSION_SERVICE.convert(entry.getKey(), Period.class), is(entry.getValue()));
}
}
private static String format(Date date, String format) {
return new SimpleDateFormat(format).format(date);
}
Expand Down