如何使用 Spring 表达式语言比较日期?

How to compare dates using Spring Expression Language?

任何人都可以给我任何示例,说明如何使用 Spring Expression Languange (Spel) 比较日期吗?

我进行了广泛的搜索,但似乎没有找到符合我目的的东西。我正在使用 Java 8 并且我有一个带有 ZonedDateTime 字段的响应对象,我需要以某种方式将其与 YYYY-MM-DD 形式的字符串进行比较 - IE:之前,之后,等于等

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'");

以上显然是行不通的。谁能给我举个例子?

我建议将您的字符串解析为 Instant 或 ZoneDateTime 或任何 TemporalAccessor,然后使用由 TemporalAccessor 扩展的接口 ChronoZonedDateTime 的方法 compareTo() 来比较两个日期。 See javadoc for compareTo(). Use class java.time.format.DateTimeFormatter 将字符串解析为 TemporalAccessor。 (方法解析())。您的代码可能如下所示:

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

这是一个基础。您还可以看到这篇文章,它解释了如何将未知格式的字符串解析为日期。可能会有帮助。

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

下面是使用 Java 8 java.time 包和 Spring 表达式语言进行日期比较的代码。希望这有帮助。

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);
   }
}

请试试这个例子,希望对你有帮助....

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");
}