Albert Rannetsperger
Albert Rannetsperger

Reputation: 944

How to compare dates using Spring Expression Language?

Can anyone give me any examples of how to compare Dates using Spring Expression Languange (Spel)?

I have searched far and wide and nothing I find seems to fit my purpose. I'm using Java 8 and I have a response Object with a ZonedDateTime field which I somehow need to compare to a string in the form of YYYY-MM-DD -- IE: is before, is after, is equal, etc.

ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.systemDefault());
HashMap<String, Object> testHash = new HashMap<>();
testHash.put("dateField", zonedDateTime);

ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariables(testHash);

Expression expression = parser.parseExpression("#dateField > '2016-01-01'");

The above is obviously not working. Anyone able to show me an example that does?

Upvotes: 2

Views: 11717

Answers (3)

Lokesh Kumar Gaurav
Lokesh Kumar Gaurav

Reputation: 726

Please try this as example I hope that it will help you....

DateFormat destDf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startDate;
Date endDate;
String strDate1="2016-08-09 00:00:00";
String strDate2="2015-11-19 00:00:00";
    startDate = destDf.parse(strDate1);
    endDate = destDf.parse(strDate2);
if(startDate.after(endDate)){
Object Date;
if(startDate.before(endDate))
if(startDate.equals(endDate))

//You can check date in java like this also
Date Date date = new Date();
if(date.getDate()==startDate.getDate() && date.getMonth()==startDate.getMonth() && date.getYear()==startDate.getYear()){
    System.out.println("Date1 is equal Date2");
}

Upvotes: -4

abaghel
abaghel

Reputation: 15317

Below is the code for date comparison using Java 8 java.time package and Spring Expression Language. Hope this helps.

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import org.junit.Test;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import junit.framework.Assert;

public class TestDateSPEL {

@Test
public void testCompareDate() throws Exception {
    ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.systemDefault());
    String otherDate = "2010-12-25 12:00";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm").withZone(ZoneId.systemDefault());
    ZonedDateTime zdtOtherDate = ZonedDateTime.parse(otherDate, formatter);
    //SpEL Context
    EvaluationContext context = new StandardEvaluationContext(new ZonedDateTimeUtil());
    context.setVariable("dateOne", zonedDateTime);
    context.setVariable("dateTwo", zdtOtherDate);
    //SpEL Parser
    ExpressionParser parser = new SpelExpressionParser();
    Expression exp = parser.parseExpression("compareDate(#dateOne, #dateTwo)");
    int value = (Integer) exp.getValue(context);
    //"zonedDateTime" is after "zdtOtherDate"
    Assert.assertEquals(1, value);
  }
}

class ZonedDateTimeUtil {
public int compareDate(ZonedDateTime dateOne, ZonedDateTime dateTwo){
    return dateOne.compareTo(dateTwo);
   }
}

Upvotes: 5

Michael Gantman
Michael Gantman

Reputation: 7808

I would recommend to parse your String to Instant or ZoneDateTime or any TemporalAccessor and then use method compareTo() of interface ChronoZonedDateTime that is extended by TemporalAccessor to compare two dates. See javadoc for compareTo(). Use class java.time.format.DateTimeFormatter to parse a String to TemporalAccessor. (method parse()). Your code may look like:

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
TemporalAccessor ta = dtf.parse(yourDateString);
boolean result = yourZonedDateTime.compareTo(ta);

That's a base to start with. Also you can see this article that explains how to parse a String of unknown format into date. It might be helpful.

Java 8 java.time package: parsing any string to date

Upvotes: 0

Related Questions