自己用的一个需要解析时间格式表达式的小工具类:
/** * <pre> * Copyright CDC [2000-2015] * </pre> */ package org.kanpiaoxue.commons.utils; import org.apache.commons.lang3.StringUtils; import org.joda.time.DateTime; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; import com.google.common.base.Preconditions; import com.google.common.base.Splitter; import com.google.common.collect.Maps; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * <pre> * DateExpressionEngine.java * @author kanpiaoxue<br> * @version 1.0 * Create Time 2015年6月4日 下午7:16:02<br> * Description : 时间表达式引擎 * </pre> */ public class DateExpressionEngine { /** * <pre> * @param needProcessString 需要处理的字符串 * 里面可以含有任意多个时间表达式 * 格式:dateFormat[,offset]} * dateFormat: 符合java的java.text.DateFormat规范的表达式 * DateUnit 时间格式单位:[注意大小写] * y : year * M : Month * d : day * w : week * H : Hour * m : minute * * [重点]这里的格式有两种,一种是一般日期表达式,一种是特殊表达式 * * 一、一般日期表达式: * offset的表达式:(-)?number+DateUnit * offset的正则表达式: ^((?:-)?\\d+?)([y,M,w,d,H,m])$ * * example: * ${MM} ${MMdd} ${MM/dd} ${HH} ${HHmm} * ${yyyyMMdd} ${yyyy-MM-dd} ${yyyy/MM/dd} * ${yyyyMMddHH} ${yyyy-MM-dd HH} ${yyyy/MM/dd HH} * ${yyyyMMddHHmm} ${yyyy-MM-dd HH:mm} ${yyyy/MM/dd HH:mm} * ${yyyyMMdd,-1y} ${yyyy-MM-dd,-1y} ${yyyy/MM/dd,-1y} * ${yyyyMMdd,-1M} ${yyyy-MM-dd,-1M} ${yyyy/MM/dd,-1M} * ${yyyyMMdd,1d} ${yyyy-MM-dd,1d} ${yyyy/MM/dd,1d} * ${yyyyMMddHH,1H} ${yyyy-MM-dd HH,1H} ${yyyy/MM/dd HH,1H} * ${yyyyMMdd,1w} ${yyyy-MM-dd,1w} ${yyyy/MM/dd,1w} * ${yyyyMMddHHmm,10m} ${yyyy-MM-dd HH:mm,10m} ${yyyy/MM/dd HH:mm,10m} * * 二、特殊表达式 * 用来计算:季度初/末,月初/末,周初/末(也就是周一和周日) * offset的表达式:position+DateUnit * offset的正则表达式:^([F,f,E,e])([M,w,W,q,Q])$ * ------------------ * F,f means: first * E,e means: end * ------------------ * M : Month * w,W : Week * q,Q : Quarter * * @param dateValue 时间的字符串,格式要求 yyyyMMdd,yyyyMMddHH,yyyyMMddHHmm * @return 经过计算之后的字符串 * </pre> */ public static String formatDateExpression(String needProcessString, String dateValue) { Preconditions.checkArgument(StringUtils.isNotBlank(needProcessString)); Preconditions.checkArgument(StringUtils.isNotBlank(dateValue)); Preconditions.checkArgument(DATE_PATTERN.matcher(dateValue).matches(), "dateValue is unexpect format:%s. " + "Required min length is 8, like : 20150101. %s's length is : %s", dateValue, dateValue, dateValue.length()); Matcher m = PATTERN.matcher(needProcessString); /** * 如果找到时间表达式则进行替换,如果找不到,则不进行处理 */ while (m.find()) { String expression = m.group(2); /** * <pre> * ${expression} is group(1) , as: ${yyyyMMdd,-1y} , ${yyyyMMdd,FQ} * expression is group(2) , as: yyyyMMdd,-1y , yyyyMMdd,FQ * </pre> */ int start = m.start(1); int end = m.end(1); // expressionWrapper : ${yyyyMMdd,-1y} , ${yyyyMMdd,FQ} String expressionWrapper = needProcessString.substring(start, end); // replaceMent : 20150101 201501012359 // 得到正确的时间字符串 String replaceMent = getCorrectDateString(expression, dateValue); // 替换表达式为正确的时间字符串 needProcessString = StringUtils.replace(needProcessString, expressionWrapper, replaceMent); // find next m = PATTERN.matcher(needProcessString); } return needProcessString; } private static String getCorrectDateString(String express, String dataVersionNo) { // 将表达式切分开 List<String> lst = Splitter.on(',').trimResults().omitEmptyStrings().splitToList(express); int size = lst.size(); Preconditions.checkArgument(size <= 2, "unexpected expression format:%s", express); // 得到世界表达式的格式化部分 String format = lst.get(0); // 将版本号的字符串转换为 DateTime 对象 DateTime dateTime = parseForDataVersionNo(dataVersionNo); DateTime rs = dateTime; // 如果存在复杂的计算表达式 if (lst.size() == 2) { String offsetExpression = lst.get(1); // 处理季度、月、周的第一天和最后一天 Matcher sm = OFFSET_SPECIAL_PATTERN.matcher(offsetExpression); if (sm.matches()) { String str1 = sm.group(1); Preconditions.checkArgument(StringUtils.isNotBlank(str1), "unexpected expression format:%s", express); String unit = sm.group(2); if (QUARTR_STRING.equalsIgnoreCase(unit)) { DateTime startQuarter = dateTime.plusMonths(0 - (dateTime.monthOfYear().get() - 1) % 3).dayOfMonth() .withMinimumValue(); // 季度初 if (FIRST_STRING.equalsIgnoreCase(str1)) { rs = startQuarter; } else if (END_STRING.equalsIgnoreCase(str1)) { rs = startQuarter.plusMonths(3).plusDays(-1); // 季度末 } else { throw new IllegalArgumentException(String.format("unexpected expression format:%s", express)); } } else if (MONTH_STRING.equals(unit)) { if (FIRST_STRING.equalsIgnoreCase(str1)) { rs = dateTime.dayOfMonth().withMinimumValue(); } else if (END_STRING.equalsIgnoreCase(str1)) { rs = dateTime.dayOfMonth().withMaximumValue(); } else { throw new IllegalArgumentException(String.format("unexpected expression format:%s", express)); } } else if (WEEK_STRING.equalsIgnoreCase(unit)) { if (FIRST_STRING.equalsIgnoreCase(str1)) { rs = dateTime.dayOfWeek().withMinimumValue(); } else if (END_STRING.equalsIgnoreCase(str1)) { rs = dateTime.dayOfWeek().withMaximumValue(); } else { throw new IllegalArgumentException(String.format("unexpected expression format:%s", express)); } } return rs.toString(format); } // 处理一般的时间表达式 Matcher m = OFFSET_PATTERN.matcher(offsetExpression); Preconditions.checkArgument(m.matches(), "unexpected expression format:%s", express); String numString = m.group(1); if (StringUtils.isBlank(numString)) { numString = "0"; } int num = Integer.valueOf(numString).intValue(); String unit = m.group(2); if (YEAR_STRING.equalsIgnoreCase(unit)) { // IgnoreCase rs = dateTime.plusYears(num); } else if (MONTH_STRING.equals(unit)) { rs = dateTime.plusMonths(num); } else if (WEEK_STRING.equalsIgnoreCase(unit)) { // IgnoreCase rs = dateTime.plusWeeks(num); } else if (DAY_STRING.equalsIgnoreCase(unit)) { // IgnoreCase rs = dateTime.plusDays(num); } else if (HOUR_STRING.equalsIgnoreCase(unit)) { // IgnoreCase rs = dateTime.plusHours(num); } else if (MINUTE_STRING.equals(unit)) { rs = dateTime.plusMinutes(num); } else { throw new IllegalArgumentException(String.format("unexpected expression format:%s", express)); } } return rs.toString(format); } /** * <pre> * ======================================== start ======================================== * <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> * <html> * <head> * <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> * <title>Insert title here</title> * </head> * <body> * <div> * * 执行命令里面可以含有任意多个时间表达式。<br> * 格式:${formatExpression[,offsetExpression]} * 其中[]方括号表示“可选”。formatExpression是符合Java的DateFormat的任意格式表达式 * <span style="color:red;font-weight:bold;">[注意]区分大小写</span> * <hr> * 表达式中的各个字符解释如下: * <table border=1> * <thead> * <td>字符</td> * <td>时间单位</td> * <td>原文</td> * <td>出现位置</td> * </thead> * <tr> * <td>y</td> * <td>年</td> * <td>year</td> * <td>formatExpression,offsetExpression</td> * </tr> * <tr> * <td>M</td> * <td>月</td> * <td>Month</td> * <td>formatExpression,offsetExpression</td> * </tr> * <tr> * <td>d</td> * <td>天</td> * <td>day</td> * <td>formatExpression,offsetExpression</td> * </tr> * <tr> * <td>H</td> * <td>小时</td> * <td>Hour</td> * <td>formatExpression,offsetExpression</td> * </tr> * <tr> * <td>m</td> * <td>分钟</td> * <td>minute</td> * <td>formatExpression,offsetExpression</td> * </tr> * <tr> * <td>W</td> * <td>周</td> * <td>Week</td> * <td>offsetExpression</td> * </tr> * <tr> * <td>Q</td> * <td>季度</td> * <td>Quarter</td> * <td>offsetExpression</td> * </tr> * <tr> * <td>F</td> * <td>起始</td> * <td>First</td> * <td>offsetExpression</td> * </tr> * <tr> * <td>E</td> * <td>结束</td> * <td>End</td> * <td>offsetExpression</td> * </tr> * </table> * <hr> * 举例如下: * <table border=1> * <tr> * <td>描述</td> * <td>表达式</td> * <td>结果:当前时间是2015-08-10 14:40</td> * </tr> * 2015-08-10 14:40 * * <tr> * <td>天</td> * <td>${yyyyMMdd}<br>${yyyy-MM-dd}<br>${yyyy/MM/dd}<br>${dd}</td> * <td>20150810<br>2015-08-10<br>2015/08/10<br>10</td> * </tr> * <tr> * <td>小时</td> * <td>${yyyyMMddHH}<br>${yyyy-MM-dd HH}<br>${yyyy/MM/dd HH}<br>${HH}</td> * <td>2015081014<br>2015-08-10 14<br>2015/08/10 14<br>14</td> * </tr> * <tr> * <td>分钟</td> * <td>${yyyyMMddHHmm}<br>${yyyy-MM-dd HH:mm}<br>${yyyy/MM/dd HH:mm}<br>${HHmm}</td> * <td>201508101440<br>2015-08-10 14:40<br>2015/08/10 14:40<br>1440</td> * </tr> * <tr> * <td>1年前</td> * <td>${yyyyMMdd,-1y}<br>${yyyy-MM-dd,-1y}<br>${yyyy/MM/dd,-1y}</td> * <td>20140810<br>2014-08-10<br>2014/08/10</td> * </tr> * <tr> * <td>1个月前</td> * <td>${yyyyMMdd,-1M}<br>${yyyy-MM-dd,-1M}<br>${yyyy/MM/dd,-1M}</td> * <td>20150710<br>2015-07-10<br>2015/07/10</td> * </tr> * <tr> * <td>1周前</td> * <td>${yyyyMMdd,-1W}<br>${yyyy-MM-dd,-1W}<br>${yyyy/MM/dd,-1W}</td> * <td>20150803<br>2015-08-03<br>2015/08/03</td> * </tr> * <tr> * <td>1天前</td> * <td>${yyyyMMdd,-1d}<br>${yyyy-MM-dd,-1d}<br>${yyyy/MM/dd,-1d}</td> * <td>20150809<br>2015-08-09<br>2015/08/09</td> * </tr> * <tr> * <td>1小时前</td> * <td>${yyyyMMddHH,1H}<br>${yyyy-MM-dd HH,1H}<br>${yyyy/MM/dd HH,-1H}<br>${HH,-1H}</td> * <td>2015081015<br>2015-08-10 15<br>2015/08/10 13<br>13</td> * </tr> * <tr> * <td>10分钟前</td> * <td>${yyyyMMddHHmm,-10m}<br>${yyyy-MM-dd HH:mm,-10m}<br>${yyyy/MM/dd HH:mm,-10m}<br>${HH:mm,-10m}</td> * <td>201508101430<br>2015-08-10 14:30<br>2015/08/10 14:30<br>14:30</td> * </tr> * <tr> * <td>季度第一天</td> * <td>${yyyyMMdd,FQ}<br>${yyyy-MM-dd,FQ}<br>${yyyy/MM/dd,FQ}</td> * <td>20150701<br>2015-07-01<br>2015/07/01</td> * </tr> * <tr> * <td>季度最后一天</td> * <td>${yyyyMMdd,EQ}<br>${yyyy-MM-dd,EQ}<br>${yyyy/MM/dd,EQ}</td> * <td>20150930<br>2015-09-30<br>2015/09/30</td> * </tr> * <tr> * <td>月第一天</td> * <td>${yyyyMMdd,FM}<br>${yyyy-MM-dd,FM}<br>${yyyy/MM/dd,FM}</td> * <td>20150801<br>2015-08-01<br>2015/08/01</td> * </tr> * <tr> * <td>月最后一天</td> * <td>${yyyyMMdd,EM}<br>${yyyy-MM-dd,EM}<br>${yyyy/MM/dd,EM}</td> * <td>20150831<br>2015-08-31<br>2015/08/31</td> * </tr> * <tr> * <td>周一</td> * <td>${yyyyMMdd,FW}<br>${yyyy-MM-dd,FW}<br>${yyyy/MM/dd,FW}</td> * <td>20150810<br>2015-08-10<br>2015/08/10</td> * </tr> * <tr> * <td>周日</td> * <td>${yyyyMMdd,EW}<br>${yyyy-MM-dd,EW}<br>${yyyy/MM/dd,EW}</td> * <td>20150816<br>2015-08-16<br>2015/08/16</td> * </tr> * * * </table> * </div> * </body> * </html> * ======================================== end ======================================== * </pre> */ public static void main(String[] args) { /** * <pre> * 用于生成说明文档 * </pre> */ Map<String, String> map = Maps.newLinkedHashMap(); map.put("天", "${yyyyMMdd}<br>${yyyy-MM-dd}<br>${yyyy/MM/dd}<br>${dd}"); map.put("小时", "${yyyyMMddHH}<br>${yyyy-MM-dd HH}<br>${yyyy/MM/dd HH}<br>${HH}"); map.put("分钟", "${yyyyMMddHHmm}<br>${yyyy-MM-dd HH:mm}<br>${yyyy/MM/dd HH:mm}<br>${HHmm}"); map.put("1年前", "${yyyyMMdd,-1y}<br>${yyyy-MM-dd,-1y}<br>${yyyy/MM/dd,-1y}"); map.put("1个月前", "${yyyyMMdd,-1M}<br>${yyyy-MM-dd,-1M}<br>${yyyy/MM/dd,-1M}"); map.put("1周前", "${yyyyMMdd,-1W}<br>${yyyy-MM-dd,-1W}<br>${yyyy/MM/dd,-1W}"); map.put("1天前", "${yyyyMMdd,-1d}<br>${yyyy-MM-dd,-1d}<br>${yyyy/MM/dd,-1d}"); map.put("1小时前", "${yyyyMMddHH,1H}<br>${yyyy-MM-dd HH,1H}<br>${yyyy/MM/dd HH,-1H}<br>${HH,-1H}"); map.put("10分钟前", "${yyyyMMddHHmm,-10m}<br>${yyyy-MM-dd HH:mm,-10m}<br>${yyyy/MM/dd HH:mm,-10m}<br>${HH:mm,-10m}"); map.put("季度第一天", "${yyyyMMdd,FQ}<br>${yyyy-MM-dd,FQ}<br>${yyyy/MM/dd,FQ}"); map.put("季度最后一天", "${yyyyMMdd,EQ}<br>${yyyy-MM-dd,EQ}<br>${yyyy/MM/dd,EQ}"); map.put("月第一天", "${yyyyMMdd,FM}<br>${yyyy-MM-dd,FM}<br>${yyyy/MM/dd,FM}"); map.put("月最后一天", "${yyyyMMdd,EM}<br>${yyyy-MM-dd,EM}<br>${yyyy/MM/dd,EM}"); map.put("周一", "${yyyyMMdd,FW}<br>${yyyy-MM-dd,FW}<br>${yyyy/MM/dd,FW}"); map.put("周日", "${yyyyMMdd,EW}<br>${yyyy-MM-dd,EW}<br>${yyyy/MM/dd,EW}"); String dateTime = "2015-08-10 14:40"; System.out.println(dateTime); dateTime = dateTime.replace("-", "").replace(" ", "").replace(":", ""); for (Entry<String, String> entry : map.entrySet()) { String detail = entry.getKey(); String str = entry.getValue(); System.out.println("<tr>"); String rs = DateExpressionEngine.formatDateExpression(str, dateTime); System.out.println("<td>" + detail + "</td>"); System.out.println("<td>" + str + "</td>"); System.out.println("<td>" + rs + "</td>"); System.out.println("</tr>"); } } /** * <pre> * 将版本号的字符串转换为 DateTime 对象 * @param dataVersionNo 20150101 2015010123 201501012359 * @return 被解析之后的DateTime * </pre> */ private static DateTime parseForDataVersionNo(String dataVersionNo) { int size = dataVersionNo.length(); String tempFormat = CONST_DATE_STRING; if (size == CONST_DATE_STRING.length()) { tempFormat = CONST_DATE_STRING; } else if (size == CONST_HOUR_STRING.length()) { tempFormat = CONST_HOUR_STRING; } else if (size == CONST_MINUTE_STRING.length()) { tempFormat = CONST_MINUTE_STRING; } else { throw new IllegalArgumentException(String.format("wrong dataVersionNo:%s", dataVersionNo)); } DateTimeFormatter formatter = DateTimeFormat.forPattern(tempFormat); DateTime dateTime = DateTime.parse(dataVersionNo, formatter); return dateTime; } //private static final String REGEX = "(\\$\\{([^s].*?)\\})"; private static final String REGEX = "(\\$\\{(.*?)\\})"; private static final Pattern PATTERN = Pattern.compile(REGEX); private static final Pattern DATE_PATTERN = Pattern.compile("^\\d{8,}$"); private static final String FIRST_STRING = "F"; private static final String END_STRING = "E"; private static final String YEAR_STRING = "y"; private static final String QUARTR_STRING = "Q"; private static final String MONTH_STRING = "M"; private static final String WEEK_STRING = "w"; private static final String DAY_STRING = "d"; private static final String HOUR_STRING = "H"; private static final String MINUTE_STRING = "m"; private static final String OFFSET_REGEX = "^(-?\\d+?)?([y,Y,M,w,W,d,D,H,h,m])$"; private static final Pattern OFFSET_PATTERN = Pattern.compile(OFFSET_REGEX); private static final String OFFSET_SPECIAL_REGEX = "^([F,f,E,e])([M,w,W,q,Q])$"; private static final Pattern OFFSET_SPECIAL_PATTERN = Pattern.compile(OFFSET_SPECIAL_REGEX); private static final String CONST_DATE_STRING = "yyyyMMdd"; private static final String CONST_HOUR_STRING = "yyyyMMddHH"; private static final String CONST_MINUTE_STRING = "yyyyMMddHHmm"; }
/** * <pre> * Copyright CDC [2000-2015] * </pre> */ package org.kanpiaoxue.commons.utils; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import org.junit.Test; import com.google.common.collect.Lists; import java.util.List; /** * <pre> * DateExpressionEngineTest.java * @author kanpiaoxue<br> * @version 1.0 * Create Time 2015年6月4日 下午7:29:22<br> * Description : DateExpressionEngine测试类 * </pre> */ public class DateExpressionEngineTest { @Test public void testFormatDateExpression() { String needProcessString = null; String dateValue = "20150604"; try { DateExpressionEngine.formatDateExpression(needProcessString, dateValue); fail("error"); } catch (Exception e) { assertTrue(true); } needProcessString = "${yyyyMMdd}"; dateValue = null; try { DateExpressionEngine.formatDateExpression(needProcessString, dateValue); fail("error"); } catch (Exception e) { assertTrue(true); } needProcessString = "${yyyyMMdd}"; dateValue = "2015060"; try { DateExpressionEngine.formatDateExpression(needProcessString, dateValue); fail("error"); } catch (Exception e) { assertTrue(true); } needProcessString = "${yyyyMMdd,-1d,3w}"; dateValue = "2015060"; try { DateExpressionEngine.formatDateExpression(needProcessString, dateValue); fail("error"); } catch (Exception e) { assertTrue(true); } needProcessString = "${yyyyMMdd,-1d,3X}"; dateValue = "2015060"; try { DateExpressionEngine.formatDateExpression(needProcessString, dateValue); fail("error"); } catch (Exception e) { assertTrue(true); } needProcessString = "${yyyyMMdd,-1d}"; dateValue = "20150600000000"; try { DateExpressionEngine.formatDateExpression(needProcessString, dateValue); fail("error"); } catch (Exception e) { assertTrue(true); } needProcessString = "${yyyyMMdd}"; dateValue = "20150604"; String rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("20150604", rs); needProcessString = "${yyyy=MM=dd}"; dateValue = "20150604"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("2015=06=04", rs); needProcessString = "${yyyyMM}"; dateValue = "20150604"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("201506", rs); needProcessString = "${MMdd}"; dateValue = "201506041945"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("0604", rs); needProcessString = "${MMddHH}"; dateValue = "201506041945"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("060419", rs); needProcessString = "${ddHH}"; dateValue = "201506041945"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("0419", rs); needProcessString = "${HH}"; dateValue = "201506041945"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("19", rs); needProcessString = "${HHmm}"; dateValue = "201506041945"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("1945", rs); needProcessString = "${HH:mm}"; dateValue = "201506041945"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("19:45", rs); needProcessString = "${yyyy-MM-dd}"; dateValue = "20150604"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("2015-06-04", rs); needProcessString = "${yyyyMMddHH}"; dateValue = "2015060401"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("2015060401", rs); needProcessString = "${yyyy-MM-dd HH}"; dateValue = "2015060401"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("2015-06-04 01", rs); needProcessString = "${yyyyMMddHHmm}"; dateValue = "201506040101"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("201506040101", rs); needProcessString = "${yyyy/MM/dd HH:mm}"; dateValue = "201506040101"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("2015/06/04 01:01", rs); needProcessString = "${yyyyMMdd,-1y}"; dateValue = "201506040101"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("20140604", rs); needProcessString = "${yyyy-MM-dd,-1y}"; dateValue = "201506040101"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("2014-06-04", rs); needProcessString = "${yyyyMMdd,0y}"; dateValue = "201506040101"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("20150604", rs); needProcessString = "${yyyy-MM-dd,0y}"; dateValue = "201506040101"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("2015-06-04", rs); needProcessString = "${yyyyMMdd,10y}"; dateValue = "201506040101"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("20250604", rs); needProcessString = "${yyyy/MM/dd,10y}"; dateValue = "201506040101"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("2025/06/04", rs); needProcessString = "${yyyyMMdd,10Y}"; dateValue = "201506040101"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("20250604", rs); needProcessString = "${yyyy-MM-dd,10Y}"; dateValue = "201506040101"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("2025-06-04", rs); needProcessString = "${yyyyMMdd,-2M}"; dateValue = "20150604"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("20150404", rs); needProcessString = "${yyyy/MM/dd,-2M}"; dateValue = "20150604"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("2015/04/04", rs); needProcessString = "${yyyyMMdd,2M}"; dateValue = "20150604"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("20150804", rs); needProcessString = "${yyyy-MM-dd,2M}"; dateValue = "20150604"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("2015-08-04", rs); needProcessString = "${yyyyMMdd,1w}"; dateValue = "20150604"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("20150611", rs); needProcessString = "${yyyy-MM-dd,1w}"; dateValue = "20150604"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("2015-06-11", rs); needProcessString = "${yyyyMMdd,-1w}"; dateValue = "20150604"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("20150528", rs); needProcessString = "${yyyy-MM-dd,-1w}"; dateValue = "20150604"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("2015-05-28", rs); needProcessString = "${yyyyMMdd,-3d}"; dateValue = "20150604"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("20150601", rs); needProcessString = "${yyyy-MM-dd,-3d}"; dateValue = "20150604"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("2015-06-01", rs); needProcessString = "${yyyyMMdd,0d}"; dateValue = "20150604"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("20150604", rs); needProcessString = "${yyyy-MM-dd,0d}"; dateValue = "20150604"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("2015-06-04", rs); needProcessString = "${yyyyMMdd,11d}"; dateValue = "20150604"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("20150615", rs); needProcessString = "${yyyy-MM-dd,11d}"; dateValue = "20150604"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("2015-06-15", rs); needProcessString = "${yyyyMMdd,24h}"; dateValue = "20150604"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("20150605", rs); needProcessString = "${yyyy-MM-dd,24h}"; dateValue = "20150604"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("2015-06-05", rs); needProcessString = "${yyyyMMddHHmm,24h}"; dateValue = "201506040101"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("201506050101", rs); needProcessString = "${yyyy-MM-dd HH:mm,24h}"; dateValue = "201506040101"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("2015-06-05 01:01", rs); needProcessString = "${yyyyMMddHHmm,24m}"; dateValue = "201506040101"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("201506040125", rs); needProcessString = "${yyyy-MM-dd HH:mm,24m}"; dateValue = "201506040101"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("2015-06-04 01:25", rs); needProcessString = "${yyyyMMddHHmm,-4m}"; dateValue = "201506040101"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("201506040057", rs); needProcessString = "${yyyy-MM-dd HH:mm,-4m}"; dateValue = "201506040101"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("2015-06-04 00:57", rs); needProcessString = "${yyyyMMddHHmm,m}"; dateValue = "201506040101"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("201506040101", rs); needProcessString = "${yyyy-MM-ddHHmm,m}"; dateValue = "201506040101"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("2015-06-040101", rs); needProcessString = "hello"; dateValue = "201506040101"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("hello", rs); needProcessString = "hello"; dateValue = "201506040101"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("hello", rs); needProcessString = "${yyyyMMdd}hello${yyyyMMdd,11d}"; dateValue = "201506040101"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("20150604hello20150615", rs); needProcessString = "${yyyy-MM/dd}hello${yyyy/MM-dd,11d}"; dateValue = "201506040101"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("2015-06/04hello2015/06-15", rs); needProcessString = "${yyyyMMdd,fq}hello${yyyyMMdd,EQ}"; dateValue = "201506040101"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("20150401hello20150630", rs); needProcessString = "${yyyy-MM-dd,fq}hello${yyyy/MM/dd,EQ}"; dateValue = "201506040101"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("2015-04-01hello2015/06/30", rs); needProcessString = "${yyyyMMdd,Fq}hello${yyyyMMdd,EQ}"; dateValue = "201506040101"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("20150401hello20150630", rs); needProcessString = "${yyyy-MM-dd,Fq}hello${yyyyMMdd,EQ}"; dateValue = "201506040101"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("2015-04-01hello20150630", rs); needProcessString = "${yyyyMMdd,Fq}hello${yyyyMMdd,eQ}"; dateValue = "201506040101"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("20150401hello20150630", rs); needProcessString = "${yyyy MM dd,Fq}hello${yyyyMMdd,eQ}"; dateValue = "201506040101"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("2015 04 01hello20150630", rs); needProcessString = "${yyyy-MM-dd,Fq}hello${yyyy/MM/dd,eQ}"; dateValue = "201506040101"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("2015-04-01hello2015/06/30", rs); needProcessString = "${yyyyMMdd,FM}hello${yyyyMMdd,eM}"; dateValue = "201506040101"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("20150601hello20150630", rs); needProcessString = "${yyyy-MM-dd,FM}hello${yyyyMMdd,eM}"; dateValue = "201506040101"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("2015-06-01hello20150630", rs); needProcessString = "${yyyyMMdd,Fw}hello${yyyyMMdd,eW}"; dateValue = "201506040101"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("20150601hello20150607", rs); needProcessString = "${yyyy-MM-dd,Fw}hello${yyyyMMdd,eW}"; dateValue = "201506040101"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("2015-06-01hello20150607", rs); needProcessString = "${yyyy-MM,Fw}hello${yyyyMM,eW}"; dateValue = "201506040101"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("2015-06hello201506", rs); needProcessString = "${dd,Fw}hello${dd,eW}"; dateValue = "201506040101"; rs = DateExpressionEngine.formatDateExpression(needProcessString, dateValue); System.out.println(rs); assertEquals("01hello07", rs); List<String> cmds = Lists.newArrayList(); cmds.add("cd /home/work/etl/cdc-etl-sme/${yyyyMMdd,-1y}/scripts && sh +x run.sh " + "ods/${yyyyMMdd}/baike/baike_launch_cost_log ${yyyyMMdd,-1d} ${yyyyMMdd,-1y}" + " ${yyyyMMdd,-2d} ${yyyyMMddHHmm,-3H} ${yyyyMMdd,-1w} ${yyyyMMdd,10y} ${yyyyMMdd,8d}"); cmds.add("cd /home/work/etl/cdc-etl-sme/${yyyy-MM-dd,-1y}/scripts && sh +x run.sh " + "ods/${yyyy-MM-dd}/baike/baike_launch_cost_log ${yyyy-MM-dd,-1d} ${yyyy-MM-dd,-1y}" + " ${yyyy-MM-dd,-2d} ${yyyy-MM-dd HH:mm,-3H} ${yyyy/MM/dd,-1w} ${yyyy/MM/dd,10y} ${yyyy/MM/dd,8d}"); cmds.add("cd /home/work/etl/cdc-etl-sme/scripts && sh +x run.sh" + " ods/baike/baike_launch_cost_log ${yyyyMMddHH}"); cmds.add("cd /home/work/etl/cdc-etl-sme/scripts && sh +x run.sh" + " ods/baike/baike_launch_cost_log ${yyyy-MM-dd HH}"); cmds.add("cd /home/work/etl/cdc-etl-sme/scripts && sh +x run.sh" + " ods/baike/baike_launch_cost_log ${yyyyMMddHHmm}"); cmds.add("cd /home/work/etl/cdc-etl-sme/scripts && sh +x run.sh" + " ods/baike/baike_launch_cost_log ${yyyy-MM-dd HH:mm}"); cmds.add("cd /home/work/etl/cdc-etl-sme/scripts && sh +x run.sh" + " ods/baike/baike_launch_cost_log ${yyyyMMdd,-1y}"); cmds.add("cd /home/work/etl/cdc-etl-sme/scripts && sh +x run.sh" + " ods/baike/baike_launch_cost_log ${yyyy-MM-dd,-1y}"); cmds.add("cd /home/work/etl/cdc-etl-sme/scripts && sh +x run.sh" + " ods/baike/baike_launch_cost_log ${yyyyMMdd,-1M}"); cmds.add("cd /home/work/etl/cdc-etl-sme/scripts && sh +x run.sh" + " ods/baike/baike_launch_cost_log ${yyyy-MM-dd,-1M}"); cmds.add("cd /home/work/etl/cdc-etl-sme/scripts && sh +x run.sh" + " ods/baike/baike_launch_cost_log ${yyyyMMdd,1d}"); cmds.add("cd /home/work/etl/cdc-etl-sme/scripts && sh +x run.sh" + " ods/baike/baike_launch_cost_log ${yyyy/MM/dd,1d}"); cmds.add("cd /home/work/etl/cdc-etl-sme/scripts ${yyyyMMddHHmm,-1d} && sh" + " +x run.sh ods/baike/baike_launch_cost_log ${yyyyMMddHH,1H}"); cmds.add("cd /home/work/etl/cdc-etl-sme/scripts ${yyyy-MM-dd HH:mm,-1d} && sh" + " +x run.sh ods/baike/baike_launch_cost_log ${yyyy/MM/dd HH,1H}"); cmds.add("cd /home/work/etl/cdc-etl-sme/scripts && sh +x run.sh" + " ods/baike/baike_launch_cost_log ${yyyyMMddHHmm,10m}"); cmds.add("cd /home/work/etl/cdc-etl-sme/scripts && sh +x run.sh" + " ods/baike/baike_launch_cost_log ${yyyy-MM-dd HH:mm,10m}"); cmds.add("cd /home/work/etl/cdc-etl-sme/scripts && sh +x run.sh" + " ods/baike/baike_launch_cost_log"); cmds.add("cd /home/work/etl/cdc-etl-sme/scripts && sh +x run.sh" + " ods/baike/baike_launch_cost_log.${HH}"); cmds.add("cd /home/work/etl/cdc-etl-sme/scripts && sh +x run.sh" + " ods/baike/baike_launch_cost_log.${HHmm}"); List<String> dataVersionNoList = Lists.newArrayList("20150923","2015092319","201509231923"); // String dataVersionNo = "201501010101"; // String dataVersionNo = "20150101"; for (String dataVersionNo : dataVersionNoList) { System.out.println("*****************************************"); int i = 0; for (String cmd : cmds) { cmd = DateExpressionEngine.formatDateExpression(cmd, dataVersionNo); if (i % 2 == 0) { System.out.println("==================="); } System.out.println(cmd); i++; } } } }
相关推荐
Fel Java表达式引擎是一款轻量级且高效的开源表达式计算引擎。它的全称是FastExpressionLanguage,专门设计用于满足不断变化的功能需求和性能需求。Fel的执行方式主要依赖于函数,运算符如加减乘除等被视为Fel函数,...
### 高速 Fel表达式引擎知识点详解 #### 一、Fel表达式引擎概述 Fel(Fast Expression Language)是一种高性能的表达式引擎,旨在满足企业级应用对于表达式处理的需求。它具备快速执行的能力,与ognl等其他流行...
6. **API集成**:Fel-all-0.9.tar.gz文件包含了一个完整的Fel引擎包,很可能提供了方便的Java API,使得开发者能够轻松地将Fel引擎集成到Java项目中,进行表达式的创建、编译和执行。 7. **扩展性**:Fel引擎可能...
Java表达式引擎是指能够解析和计算表达式的软件组件,它能够从字符串形式的表达式中获取值、执行方法调用等操作。在Java领域中,表达式引擎常常用于处理动态计算需求,如模板引擎、规则引擎等场景。 从提供的文件...
在Java中,我们可以使用RE2J库,它是Google开发的一个高效、安全的正则表达式引擎,支持构造有限状态自动机。虽然RE2J主要设计用于正则表达式的匹配,但通过它的内部机制,我们可以间接地实现字符串生成。 下面是一...
Fel表达式引擎在Java应用中尤其常见,它能够嵌入到各种系统中,提升程序的可扩展性和灵活性。 **一、Fel语法基础** 1. **变量和常量**:Fel支持变量和常量的使用。变量以$开头,例如`$name`,常量则直接书写,如`...
尤其是在Java语言环境中,`java.util.regex`包提供了丰富的API来支持正则表达式的创建与使用。然而,在实际应用中,随着正则表达式的复杂度增加,其执行效率往往会成为性能瓶颈之一。本文将深入探讨Java中正则表达式...
JEXL(Java Expression Language)是Apache Commons项目提供的一种轻量级的表达式语言,它允许我们在Java应用程序中方便地执行JavaScript风格的表达式。本文将深入探讨如何使用JEXL在Java中解析和执行表达式。 首先...
在这个场景中,我们关注的是一个用Java编写的专门用于解析算术表达式的解释器。Java是一种广泛使用的面向对象的编程语言,以其跨平台性和强大的库支持而著名。下面我们将深入探讨Java解释算术表达式这一主题。 首先...
在Java编程语言中,计算数学表达式是一项常见的需求,它涉及到字符串解析、语法分析和运算符优先级处理等多个环节。`jeval-0.9.4`是一个Java库,专门用于解析和评估数学表达式。这个库使得开发人员能够轻松地在程序...
JAVA 正则表达式是 Java 语言中的一种模式匹配技术,用于字符串模式匹配和字符串模式替换。Java 1.40 版本引入了 java.util.regex 包,提供了对正则表达式的支持。 1. 正则表达式的引擎:正则表达式的引擎已经被...
IK Expression 是一个开源的(OpenSource),可扩展的(Extensible), 基于java 语言开发的一个超轻量级(Super lightweight)的公式化语言解析执行 工具包。它是一个采用逆波兰式算法结合指针栈优化的公式解析引擎...
Java正则表达式是Java编程语言中用于处理字符串的强大工具,它允许程序员通过模式匹配来查找、替换或分割文本。正则表达式(Regular Expression,简称regex)是一种由字符、元字符和操作符组成的模式,可以用来匹配...
Java表达式解析是编程中的一个重要概念,特别是在设计编译器、解释器或脚本引擎时。这个程序可能是为了理解并执行简单的数学或逻辑表达式,比如`2 + 3 * (4 - 5)`。在Java中实现这样的解析器,我们需要理解表达式...
Java的BeanShell是一个轻量级、动态的脚本语言,它是专门为Java平台设计的,允许在运行时执行Java代码或者类似JavaScript的语法。BeanShell在Java应用中常被用来进行快速原型开发、测试以及扩展,它提供了一种简单的...
在Java编程语言中实现一个多功能计算器,涉及到许多关键知识点,包括但不限于基础的算术运算、字符串解析、表达式求值、异常处理以及用户界面设计。下面将详细阐述这些知识点。 1. **基础算术运算**:计算器的核心...
7. **使用NetBeans**:NetBeans是一个流行的开源IDE,支持多种编程语言,包括Java。在NetBeans中开发这个项目,开发者可以利用其内置的项目管理、代码编辑、调试和构建工具,使得开发流程更加高效和便捷。 在压缩包...
Java的正则表达式引擎在内部会尝试将NFA转换为DFA,以提高匹配效率。 理解NFA图形算法对于优化Java正则表达式性能至关重要。例如,避免使用前瞻后顾(lookaround)、重复量词(*、+、?)和贪婪匹配等可能导致大量...