surrey.csv.profile.expression.DateExpressionEvaluator.java Source code

Java tutorial

Introduction

Here is the source code for surrey.csv.profile.expression.DateExpressionEvaluator.java

Source

package surrey.csv.profile.expression;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import surrey.csv.profile.ExpressionEvaluator;

/*
Copyright (c) 2014 Surrey Hughes
    
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
    
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
    
The Software shall be used for Good, not Evil.
    
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/**
 * formats and modifies a date {"date":{exp}} || {"now":{exp}}
 * 
 * date: {"value":"value","in":"pattern",
 * "out":"pattern","op":{"add|sub":{"days|months|years" :"amount"}}}
 * 
 * now: (value is not present)
 * {"out":"pattern","op":{"add|sub":{"days|months|years":"amount"}}}
 * 
 * @author Surrey
 * 
 */
public class DateExpressionEvaluator implements ExpressionEvaluator {

    public static final String PATTERN_SEPARATOR = "|:|";

    @Override
    public String evaluate(JSONObject exp, Map<String, String> valueMap) throws JSONException {
        Date date = new Date();
        if (exp.has("value")) {
            String value = valueMap.get(exp.getString("value"));
            SimpleDateFormat inDf = new SimpleDateFormat(exp.getString("in"));
            try {
                date = inDf.parse(value);
            } catch (ParseException e) {
                throw new JSONException(
                        "Failed to convert value: " + value + " into a date using pattern: " + exp.getString("in"));
            }
        }
        if (exp.has("op")) {
            JSONObject op = exp.getJSONObject("op");
            String opName = JSONObject.getNames(op)[0];
            op = op.getJSONObject(opName);
            String field = JSONObject.getNames(op)[0];
            int amount = op.getInt(field);
            int calField = Calendar.DAY_OF_MONTH;
            if (field.endsWith("s")) {
                field = field.substring(0, field.length() - 1);
            }
            if (field.equals("month")) {
                calField = Calendar.MONTH;
            } else if (field.equals("year")) {
                calField = Calendar.YEAR;
            }
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            if (opName.equals("add")) {
                cal.add(calField, amount);
            } else {
                cal.add(calField, -amount);
            }
            date = cal.getTime();
        }
        SimpleDateFormat df = new SimpleDateFormat();
        if (exp.has("out")) {
            df.applyPattern(exp.getString("out"));
        }
        return df.toPattern() + PATTERN_SEPARATOR + df.format(date);
    }

    public static Date getDate(String input) {
        SimpleDateFormat df = new SimpleDateFormat();
        String pattern = df.toPattern();
        String value = input;
        if (input.indexOf(PATTERN_SEPARATOR) > -1) {
            pattern = input.substring(0, input.indexOf(PATTERN_SEPARATOR));
            value = input.substring(input.indexOf(PATTERN_SEPARATOR) + PATTERN_SEPARATOR.length());
        }
        df.applyPattern(pattern);
        try {
            Date date = df.parse(value);
            return date;
        } catch (ParseException e) {
            return null;
        }
    }
}