Skip to content

Commit 53d78e5

Browse files
committed
Added Java 8 InstantParam with test.
1 parent cebf1d6 commit 53d78e5

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.dropwizard.jersey.params;
2+
3+
import java.time.Instant;
4+
5+
/**
6+
* A parameter encapsulating date/time values. All non-parsable values will return a {@code 400 Bad Request} response.
7+
*/
8+
public class InstantParam extends AbstractParam<Instant> {
9+
public InstantParam(final String input) {
10+
super(input);
11+
}
12+
13+
public InstantParam(final String input, final String parameterName) {
14+
super(input, parameterName);
15+
}
16+
17+
@Override
18+
protected Instant parse(final String input) throws Exception {
19+
return Instant.parse(input);
20+
}
21+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.dropwizard.jersey.params;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.Test;
6+
7+
import java.time.Instant;
8+
import java.time.ZoneOffset;
9+
import java.time.LocalDateTime;
10+
11+
/**
12+
* @author Daniel White
13+
*/
14+
public class InstantParamTest {
15+
@Test
16+
public void parsesDateTimes() throws Exception {
17+
final InstantParam param = new InstantParam("2012-11-19T00:00:00Z");
18+
Instant instant = LocalDateTime.of(2012, 11, 19, 0, 0)
19+
.toInstant(ZoneOffset.UTC);
20+
21+
assertThat(param.get())
22+
.isEqualTo(instant);
23+
}
24+
}

0 commit comments

Comments
 (0)