package com.jfinal.club.common.kit; import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import org.junit.Test; import com.jfinal.template.Directive; import com.jfinal.template.Env; import com.jfinal.template.expr.ast.Const; import com.jfinal.template.expr.ast.Expr; import com.jfinal.template.expr.ast.ExprList; import com.jfinal.template.io.Writer; import com.jfinal.template.stat.Scope; public class DateKit extends Directive{ private static final long ONE_MINUTE = 60; private static final long ONE_HOUR = 3600; private static final long ONE_DAY = 86400; private static final long ONE_MONTH = 2592000; private static final long ONE_YEAR = 31104000; public static Calendar calendar = Calendar.getInstance(); private String date; private boolean isSevenDay = false; /** * * @param timestr * @return 距离现在 */ public static String fromToday(String timestr,boolean sevenDay){ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date date = null; try { date = df.parse(timestr); } catch (ParseException e) { e.printStackTrace(); return "未知时间"; } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); long time = date.getTime() / 1000; long now = new Date().getTime() / 1000; long ago = now - time; if(ago <= ONE_HOUR){ return ago / ONE_MINUTE + "分钟前"; } else if(ago <= ONE_DAY){ return ago / ONE_HOUR + "小时" + (ago % ONE_HOUR / ONE_MINUTE)+ "分钟前"; } else if(ago <= ONE_DAY * 2){ return "昨天" + calendar.get(Calendar.HOUR_OF_DAY) + "点"+ calendar.get(Calendar.MINUTE) + "分"; } /*else if (ago <= ONE_DAY * 3){ return "前天" + calendar.get(Calendar.HOUR_OF_DAY) + "点" + calendar.get(Calendar.MINUTE) + "分"; }*/ else if (ago <= ONE_MONTH){ long day = ago / ONE_DAY; if(day < 7) return day + "天前" ; else if (day >= 7 && day <14 && !sevenDay) return "1周前"; else if (day >=14 && day <21 && !sevenDay) return "2周前"; else if (day >= 21 && day <28 && !sevenDay) return "3周前"; else if(sevenDay){ return "4周前"; }else{ return timestr; } } else if (ago <= ONE_YEAR && !sevenDay){ long month = ago / ONE_MONTH; long day = ago % ONE_MONTH / ONE_DAY; return month + "个月" + day + "天前"; } else if(!sevenDay) { long year = ago / ONE_YEAR; int month = calendar.get(Calendar.MONTH) + 1;// JANUARY which is 0 so month+1 return year + "年前" + month + "月" + calendar.get(Calendar.DATE) + "日"; }else{ return timestr; } } @Test public void test(){ // System.out.println(fromToday("2019-01-10 21:59:06",true)); } @Override public void exec(Env env, Scope scope, Writer writer) { try { writer.write(fromToday(date,isSevenDay)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void setExprList(ExprList exprList) { Expr[] exprArray = exprList.getExprArray(); if (exprArray.length == 0) { throw new RuntimeException("#string directive parameter cant not be null"); } if (exprArray.length > 2) { throw new RuntimeException("wrong number of #string directive parameter, two parameters allowed at most"); } System.out.println("exprList: " + exprList); System.out.println("exprArray[0]: " + exprArray[0]); if (!(exprArray[0] instanceof Const && ((Const)exprArray[0]).isStr())) { throw new RuntimeException("#string first parameter must be identifier"); } this.date = ((Const)exprArray[0]).getStr(); if (exprArray.length == 2) { if (exprArray[1] instanceof Const) { if (((Const)exprArray[1]).isBoolean()) { this.isSevenDay = ((Const)exprArray[1]).getBoolean(); } else { throw new RuntimeException("#string sencond parameter must be boolean"); } } } } }
main添加:
me.addDirective("fromToday",DateKit.class);
正常直接使用:
#fromToday("2019-01-11 09:06:38",true)
可是直接这样调用,就出问题了,#date输出的方法应该是String吧,可是我用debug查询,发现不是String(看图), 请指教:
#fromToday(date(replyList.get(0).replyCreateAt, "yyyy-MM-dd HH:mm:ss"),true);
你可以设置成三个参数,(日期值,日期格式,sevenDay)。然后#fromToday(replaList.get(0). replyCreateAt,"yyyy-MM-dd HH:mm:ss",true)
你为什么非要在该使用表达式的地方用指令呢
https://www.jfinal.com/doc/6-4