Skip to content
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

* Java8 Date/Time Binder Support #1320

Merged
merged 3 commits into from
Nov 29, 2019
Merged
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
4 changes: 4 additions & 0 deletions framework/src/play/data/binding/Binder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;

/**
Expand All @@ -36,6 +38,8 @@ public abstract class Binder {
supportedTypes.put(File.class, new FileBinder());
supportedTypes.put(File[].class, new FileArrayBinder());
supportedTypes.put(LocalDateTime.class, new LocalDateTimeBinder());
supportedTypes.put(LocalDate.class, new LocalDateBinder());
supportedTypes.put(LocalTime.class, new LocalTimeBinder());
supportedTypes.put(Model.BinaryField.class, new BinaryBinder());
supportedTypes.put(Upload.class, new UploadBinder());
supportedTypes.put(Upload[].class, new UploadArrayBinder());
Expand Down
18 changes: 18 additions & 0 deletions framework/src/play/data/binding/types/LocalDateBinder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (C) 2009-2019 Lightbend Inc. <https://www.lightbend.com>.
*/
package play.data.binding.types;

import play.data.binding.TypeBinder;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.time.LocalDate;

public class LocalDateBinder implements TypeBinder<LocalDate> {

@Override
public LocalDate bind(String name, Annotation[] annotations, String value, Class actualClass, Type genericType) {
return value != null && !value.trim().isEmpty() ? LocalDate.parse(value) : null;
}
}
18 changes: 18 additions & 0 deletions framework/src/play/data/binding/types/LocalTimeBinder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (C) 2009-2019 Lightbend Inc. <https://www.lightbend.com>.
*/
package play.data.binding.types;

import play.data.binding.TypeBinder;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.time.LocalTime;

public class LocalTimeBinder implements TypeBinder<LocalTime> {

@Override
public LocalTime bind(String name, Annotation[] annotations, String value, Class actualClass, Type genericType) {
return value != null && !value.trim().isEmpty() ? LocalTime.parse(value) : null;
}
}
17 changes: 17 additions & 0 deletions framework/src/play/data/validation/InFutureCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -53,6 +56,20 @@ public boolean isSatisfied(Object validatedObject, Object value, OValContext con
return false;
}
}
if (value instanceof LocalDate) {
try {
return reference.before(Date.from(((LocalDate) value).atStartOfDay(ZoneId.systemDefault()).toInstant()));
} catch (Exception e) {
return false;
}
}
if (value instanceof LocalDateTime) {
try {
return reference.before(Date.from(((LocalDateTime) value).atZone(ZoneId.systemDefault()).toInstant()));
} catch (Exception e) {
return false;
}
}
return false;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package play.data.binding.types;

import org.junit.Before;
import org.junit.Test;
import play.PlayBuilder;

import java.time.LocalDate;
import java.time.format.DateTimeParseException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

public class LocalDateBinderTest {

private LocalDateBinder binder = new LocalDateBinder();

@Before
public void setup() {
new PlayBuilder().build();
}

@Test
public void nullLocalDate() {
assertNull(binder.bind("event.start", null, null, LocalDate.class, null));
}

@Test
public void emptyLocalDate() {
assertNull(binder.bind("event.start", null, "", LocalDate.class, null));
}

@Test
public void validLocalDate() {
LocalDate expected = LocalDate.parse("2007-12-03");
LocalDate actual = binder.bind("event.start", null, "2007-12-03", LocalDate.class, null);
assertEquals(expected, actual);
}

@Test(expected = DateTimeParseException.class)
public void invalidLocalDate() throws Exception {
binder.bind("event.start", null, "2007-13-03", LocalDate.class, null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package play.data.binding.types;

import org.junit.Before;
import org.junit.Test;
import play.PlayBuilder;

import java.time.LocalTime;
import java.time.format.DateTimeParseException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

public class LocalTimeBinderTest {

private LocalTimeBinder binder = new LocalTimeBinder();

@Before
public void setup() {
new PlayBuilder().build();
}

@Test
public void nullLocalTime() {
assertNull(binder.bind("event.start", null, null, LocalTime.class, null));
}

@Test
public void emptyLocalTime() {
assertNull(binder.bind("event.start", null, "", LocalTime.class, null));
}

@Test
public void validLocalTime() {
LocalTime expected = LocalTime.parse("10:15:30");
LocalTime actual = binder.bind("event.start", null, "10:15:30", LocalTime.class, null);
assertEquals(expected, actual);
}

@Test(expected = DateTimeParseException.class)
public void invalidLocalTime() throws Exception {
binder.bind("event.start", null, "25:15:30", LocalTime.class, null);
}
}