Java tutorial
/* * Copyright (C) 2017 Baifendian Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.baifendian.swordfish.execserver.parameter.placeholder; import com.baifendian.swordfish.common.utils.DateUtils; import com.baifendian.swordfish.execserver.parameter.SystemParamManager; import java.text.MessageFormat; import java.util.AbstractMap; import java.util.Calendar; import java.util.Date; import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.util.PropertyPlaceholderHelper; /** * ?? <p> * * @see <a href="https://github.com/baifendian/swordfish/wiki/parameter-desc">?</a> */ public class TimePlaceholderUtil { /** * LOGGER */ private static final Logger LOGGER = LoggerFactory.getLogger(TimePlaceholderUtil.class); /** * ??? : "$[" */ public static final String PLACEHOLDER_PREFIX = "$["; /** * ??? :"]" */ public static final String PLACEHOLDER_SUFFIX = "]"; /** * null ?? */ public static final String VALUE_SEPARATOR = null; /** * ???? */ private static final PropertyPlaceholderHelper strictHelper = new PropertyPlaceholderHelper(PLACEHOLDER_PREFIX, PLACEHOLDER_SUFFIX, VALUE_SEPARATOR, false); /** * ????????? */ private static final PropertyPlaceholderHelper nonStrictHelper = new PropertyPlaceholderHelper( PLACEHOLDER_PREFIX, PLACEHOLDER_SUFFIX, VALUE_SEPARATOR, true); /** * add_months */ private static final String ADD_MONTHS = "add_months"; /** * month_begin */ private static final String MONTH_BEGIN = "month_begin"; /** * month_end */ private static final String MONTH_END = "month_end"; /** * week_begin */ private static final String WEEK_BEGIN = "week_begin"; /** * week_end */ private static final String WEEK_END = "week_end"; /** * timestamp */ private static final String TIMESTAMP = "timestamp"; /** * ??? <p> * * @param text ? * @param date ? * @param ignoreUnresolvablePlaceholders ???? * @return ?? */ public static String resolvePlaceholders(String text, Date date, boolean ignoreUnresolvablePlaceholders) { PropertyPlaceholderHelper helper = (ignoreUnresolvablePlaceholders ? nonStrictHelper : strictHelper); return helper.replacePlaceholders(text, new TimePlaceholderResolver(text, date)); } /** * ???? <p> * * @return ?? */ public static String resolvePlaceholdersConst(String text, String constValue) { return nonStrictHelper.replacePlaceholders(text, new ConstPlaceholderResolver(constValue)); } /** * ???? <p> */ private static class TimePlaceholderResolver implements PropertyPlaceholderHelper.PlaceholderResolver { private final String text; private final Date date; public TimePlaceholderResolver(String text, Date date) { this.text = text; this.date = date; } @Override public String resolvePlaceholder(String placeholderName) { try { String propVal = customTime(placeholderName, date); return propVal; } catch (Throwable ex) { LOGGER.error("Could not resolve placeholder '" + placeholderName + "' in [" + text + "]", ex); return null; } } } /** * ??????? <p> */ private static class ConstPlaceholderResolver implements PropertyPlaceholderHelper.PlaceholderResolver { private final String constValue; public ConstPlaceholderResolver(String constValue) { this.constValue = constValue; } @Override public String resolvePlaceholder(String placeholderName) { return constValue; } } /** * <p> * * @param expr ???, {@link https://github.com/baifendian/swordfish/wiki/parameter-desc#toc0|swordfish} * @param date * @return */ private static String customTime(String expr, Date date) { // ? N $[add_months(yyyyMMdd,12*N)], ? N $[add_months(yyyyMMdd,-N)] String value; try { // timestamp , ?? if (expr.startsWith(TIMESTAMP)) { String timeExpr = expr.substring(TIMESTAMP.length() + 1, expr.length() - 1); // , ?? Map.Entry<Date, String> entry = calcTimeExpr(timeExpr, date); // ? format String dateStr = DateUtils.format(entry.getKey(), entry.getValue()); // timestamp, ??? Date timestamp = DateUtils.parse(dateStr, SystemParamManager.TIME_FORMAT); value = String.valueOf(timestamp.getTime() / 1000); // ?s } else { // ?, ?? Map.Entry<Date, String> entry = calcTimeExpr(expr, date); value = DateUtils.format(entry.getKey(), entry.getValue()); } } catch (Exception e) { LOGGER.error(e.getMessage(), e); throw e; } return value; } /** * ? <p> * * @return <, ?> */ private static Map.Entry<Date, String> calcTimeExpr(String expr, Date date) { Map.Entry<Date, String> resultEntry; if (expr.startsWith(ADD_MONTHS)) { resultEntry = addMonthCalc(expr, date); } else if (expr.startsWith(MONTH_BEGIN)) { resultEntry = monthBeginCalc(expr, date); } else if (expr.startsWith(MONTH_END)) { resultEntry = monthEndCalc(expr, date); } else if (expr.startsWith(WEEK_BEGIN)) { resultEntry = weekStartCalc(expr, date); } else if (expr.startsWith(WEEK_END)) { resultEntry = weekEndCalc(expr, date); } else { // ????$[yyyyMMdd+7*N] resultEntry = addMinuteCalc(expr, date); } return resultEntry; } /** * ?? <p> * * @return ? */ private static Map.Entry<Date, String> monthBeginCalc(String expr, Date cycTime) { String addMonthExpr = expr.substring(MONTH_BEGIN.length() + 1, expr.length() - 1); String[] params = addMonthExpr.split(","); if (params.length == 2) { String dateFormat = params[0]; String dayExpr = params[1]; Integer day = CalculateUtil.calc(dayExpr); Date targetDate = getFirstDayOfMonth(cycTime); targetDate = org.apache.commons.lang.time.DateUtils.addDays(targetDate, day); return new AbstractMap.SimpleImmutableEntry<>(targetDate, dateFormat); } throw new RuntimeException("Expression not valid"); } /** * ? <p> * * @return */ private static Map.Entry<Date, String> monthEndCalc(String expr, Date cycTime) { String addMonthExpr = expr.substring(MONTH_END.length() + 1, expr.length() - 1); String[] params = addMonthExpr.split(","); if (params.length == 2) { String dateFormat = params[0]; String dayExpr = params[1]; Integer day = CalculateUtil.calc(dayExpr); Date targetDate = getLastDayOfMonth(cycTime); targetDate = org.apache.commons.lang.time.DateUtils.addDays(targetDate, day); return new AbstractMap.SimpleImmutableEntry<>(targetDate, dateFormat); } throw new RuntimeException("Expression not valid"); } /** * ? <p> * * @return */ private static Map.Entry<Date, String> weekStartCalc(String expr, Date cycTime) { String addMonthExpr = expr.substring(WEEK_BEGIN.length() + 1, expr.length() - 1); String[] params = addMonthExpr.split(","); if (params.length == 2) { String dateFormat = params[0]; String dayExpr = params[1]; Integer day = CalculateUtil.calc(dayExpr); Date targetDate = getMonday(cycTime); targetDate = org.apache.commons.lang.time.DateUtils.addDays(targetDate, day); return new AbstractMap.SimpleImmutableEntry<>(targetDate, dateFormat); } throw new RuntimeException("Expression not valid"); } /** * ? <p> * * @return */ private static Map.Entry<Date, String> weekEndCalc(String expr, Date cycTime) { String addMonthExpr = expr.substring(WEEK_END.length() + 1, expr.length() - 1); String[] params = addMonthExpr.split(","); if (params.length == 2) { String dateFormat = params[0]; String dayExpr = params[1]; Integer day = CalculateUtil.calc(dayExpr); Date targetDate = getSunday(cycTime); targetDate = org.apache.commons.lang.time.DateUtils.addDays(targetDate, day); return new AbstractMap.SimpleImmutableEntry<>(targetDate, dateFormat); } throw new RuntimeException("Expression not valid"); } /** * ? <p> * * @return <, ?> */ private static Map.Entry<Date, String> addMonthCalc(String expr, Date cycTime) { String addMonthExpr = expr.substring(ADD_MONTHS.length() + 1, expr.length() - 1); String[] params = addMonthExpr.split(","); if (params.length == 2) { String dateFormat = params[0]; String monthExpr = params[1]; Integer addMonth = CalculateUtil.calc(monthExpr); Date targetDate = org.apache.commons.lang.time.DateUtils.addMonths(cycTime, addMonth); return new AbstractMap.SimpleImmutableEntry<>(targetDate, dateFormat); } throw new RuntimeException("Expression not valid"); } /** * ? <p> * * @return <, ?> */ private static Map.Entry<Date, String> addMinuteCalc(String expr, Date cycTime) { if (expr.contains("+")) { int index = expr.lastIndexOf('+'); // HHmmss+N/24/60 if (Character.isDigit(expr.charAt(index + 1))) { String addMinuteExpr = expr.substring(index + 1); Date targetDate = org.apache.commons.lang.time.DateUtils.addMinutes(cycTime, calcAddMinute(addMinuteExpr)); String dateFormat = expr.substring(0, index); return new AbstractMap.SimpleImmutableEntry<>(targetDate, dateFormat); } } else if (expr.contains("-")) { int index = expr.lastIndexOf('-'); // HHmmss-N/24/60 if (Character.isDigit(expr.charAt(index + 1))) { String addMinuteExpr = expr.substring(index + 1); Date targetDate = org.apache.commons.lang.time.DateUtils.addMinutes(cycTime, 0 - calcAddMinute(addMinuteExpr)); String dateFormat = expr.substring(0, index); return new AbstractMap.SimpleImmutableEntry<>(targetDate, dateFormat); } // yyyy-MM-dd/HH:mm:ss return new AbstractMap.SimpleImmutableEntry<>(cycTime, expr); } // $[HHmmss] return new AbstractMap.SimpleImmutableEntry<>(cycTime, expr); } /** * ? add <p> */ private static Integer calcAddMinute(String addMinuteExpr) { int index = addMinuteExpr.indexOf("/"); String calcExpr; if (index == -1) { calcExpr = MessageFormat.format("60*24*({0})", addMinuteExpr); } else { calcExpr = MessageFormat.format("60*24*({0}){1}", addMinuteExpr.substring(0, index), addMinuteExpr.substring(index)); } return CalculateUtil.calc(calcExpr); } /** * ? <p> */ public static Date getMonday(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); // ? cal.setFirstDayOfWeek(Calendar.MONDAY); cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); return cal.getTime(); } /** * ? <p> */ public static Date getSunday(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); // ? cal.setFirstDayOfWeek(Calendar.MONDAY); cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); return cal.getTime(); } /** * ?? <p> */ public static Date getFirstDayOfMonth(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.DAY_OF_MONTH, 1); return cal.getTime(); } /** * ??? <p> */ public static Date getLastDayOfMonth(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); // ? 1 ?? cal.add(Calendar.MONTH, 1); cal.set(Calendar.DAY_OF_MONTH, 1); cal.add(Calendar.DAY_OF_MONTH, -1); return cal.getTime(); } public static void main(String[] args) { System.out.println(addMonthCalc("add_months(yyyyMMdd,12*1)", new Date())); System.out.println(addMonthCalc("add_months(yyyyMMdd,-12*1)", new Date())); System.out.println(addMinuteCalc("yyyy", new Date())); System.out.println(addMinuteCalc("yyyyMMdd+7*1", new Date())); System.out.println(addMinuteCalc("yyyyMMdd-7*1", new Date())); System.out.println(addMinuteCalc("yyyyMMdd+1", new Date())); System.out.println(addMinuteCalc("yyyyMMdd-1", new Date())); System.out.println(addMinuteCalc("yyyyMMddHH+1/24", new Date())); System.out.println(addMinuteCalc("yyyyMMddHH-1/24", new Date())); System.out.println(addMinuteCalc("yyyyMMddHHmm+1/24/60", new Date())); System.out.println(addMinuteCalc("yyyyMMddHHmm-1/24/60", new Date())); System.out.println(addMinuteCalc("yyyy-MM-dd/HH:mm:ss", new Date())); System.out.println(addMinuteCalc("yyyy-MM-dd/HH:mm:ss-1/24", new Date())); String test1 = TimePlaceholderUtil.resolvePlaceholders( "$[yyyy]test1$[yyyy:***]$[yyyy-MM-dd-1],$[month_begin(yyyyMMdd, 1)],$[month_end(yyyyMMdd, -1)],$[week_begin(yyyyMMdd, 1)],$[week_end(yyyyMMdd, -1)]", new Date(), true); System.out.println(test1); String test2 = TimePlaceholderUtil.resolvePlaceholdersConst("$[test1]test1$[parm1:***]$[test1]", "NULL"); System.out.println(test2); String test3 = TimePlaceholderUtil.resolvePlaceholders( "$[timestamp(yyyyMMdd00mmss)]," + "$[timestamp(month_begin(yyyyMMddHHmmss, 1))]," + "$[timestamp(month_end(yyyyMMddHHmmss, -1))]," + "$[timestamp(week_begin(yyyyMMddHHmmss, 1))]," + "$[timestamp(week_end(yyyyMMdd000000, -1))]," + "$[timestamp(yyyyMMddHHmmss)]", new Date(), true); System.out.println(test3); } }