• Custom Date tag


    Custom Date tag: custom date based on pattern format. Default date is current day.
      <CUSTOMDATE[+,-][value][scale],Pattern>
      User can define the date format by customize the parameter Pattern.
      Scale is only recognized within below option:
        “Y” for year, “M” for month, “D” for day
    Sample:
      Suppose today is 16 Dec 2014
        <CUSTOMDATE+1M,MMM yyyy> return as “Jan 2015”
        <CUSTOMDATE-1D,ddMMyyyy> return as “15122015”
        <CUSTOMDATE-2Y,dd*MMMMyyyy> return as “16*December2012”

       /**
         * @param input
         *            <CUSTOMDATE[+,-][number][scale],Pattern>
         * @return custom date based on pattern format. default date is current day
         */
        private String customDate(String input) {
            input = input.substring(1, input.length() - 1);
            String pattern = input.split(",")[1].trim();
            input = input.split(",")[0].trim();
            Date now = new Date();
            SimpleDateFormat ft = new SimpleDateFormat(pattern);
            GregorianCalendar gc = new GregorianCalendar();
            gc.setTime(now);
            int len = "CUSTOMDATE".length();
            if (input.length() == len) {
                return ft.format(now);
            } else if (input.length() < len) {
                return "The format you input is incorrect! Please make sure the format is <CUSTOMDATE[+,-][number][scale],Pattern>";
            } else {
                String math = input.substring(len, len + 1);
                int value = Integer.parseInt(input.substring(len + 1,
                        input.length() - 1));
                String scale = input.substring(input.length() - 1);
                if (math.equals("-")) {
                    value = -value;
                } else if (!math.equals("+")) {
                    return "Math is wrong. Can only use +/-.";
                }
                int field = 0;
                if (scale.equalsIgnoreCase("Y")) {
                    field = 1;
                } else if (scale.equalsIgnoreCase("M")) {
                    field = 2;
                } else if (scale.equalsIgnoreCase("D")) {
                    field = 5;
                }
                if (field == 0) {
                    return "The scale is wrong. Can only use Y/M/D.";
                }
                gc.add(field, value);
            }
            return ft.format(gc.getTime());
        }
  • 相关阅读:
    面向目标的场景设置--Goal-Oriented Scenario
    第二十二课 打造专业的编译环境(下)
    第二十一课 打造专业的编译环境(中)
    第二十课 打造专业的编译环境(上)
    第十九篇 路径搜索的综合示例
    第八十课 最长不下降序列
    第七十九课 最短路径(Floyd)
    第七十八课 最短路径(Dijkstra)
    第七十七课 最小生成树(Kruskal)
    第76课 最小生成树(Prim)
  • 原文地址:https://www.cnblogs.com/ryansunyu/p/4167526.html
Copyright © 2020-2023  润新知