Reputation: 309
I have problem with the code below:
public DatePicker data = new DatePicker();
String date = "2014-08-15";
public void verify() {
if (data.getValue().equals(date)) {
System.out.println("Success!!!");
}
}
If date selected in the DatePicker
(by mouse-click) is equal to the date of the String
"date" variable, than I want that message to appear (actualy I want some coordinates to change but for you to understand I put a message).
Upvotes: 0
Views: 2097
Reputation: 4306
From the DatePicker docs getValue() returns a LocalDate
.
And you can create a proper LocalDate
for comparison with the LocalDate.of(int year, int month, int dayOfMonth)
(docs) method. Your specified date can be created with this code:
LocalDate date = LocalDate.of(2014, 8, 15);
And compared with the exact same code:
public void verify() {
if (data.getValue().equals(date)) {
System.out.println("Success!!!");
}
}
Also, the equals
method of LocalDate
only works on other LocalDate
s.
Upvotes: 1
Reputation: 338644
The answer by nejinx is correct.
If you meant "DatePicker" to be the JavaFX DatePicker
class that returns a java.time.LocalDate object, then you need to make your comparison with another java.time.LocalDate object.
java.time.LocalDate targetLocalDate = LocalDate.of( 2014, 8, 15 );
// OR… LocalDate target = LocalDate.parse( "2014-08-15" );
LocalDate localDatePicked = datePicker.getValue();
if ( localDatePicked.equals( targetLocalDate )
{
System.out.println("Success!!!");
}
Upvotes: 1
Reputation: 349
data.getValue().equals() only compares LocalDate objects and returns false for all other comparisons:
Checks if this date is equal to another date. Compares this LocalDate with another ensuring that the date is the same.
Only objects of type LocalDate are compared, other types return false.
REF: http://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html#equals-java.lang.Object-
If you want to compare your data DatePicker with date string you would need to perform a string compare or create a LocalDate object to compare with the DatePicker's LocalDate object returned by data.getValue()
Upvotes: 1