Example usage for org.joda.time Interval parse

List of usage examples for org.joda.time Interval parse

Introduction

In this page you can find the example usage for org.joda.time Interval parse.

Prototype

public static Interval parse(String str) 

Source Link

Document

Parses an Interval from the specified string.

Usage

From source file:eu.itesla_project.online.tools.PrintOnlineWorkflowPerformances.java

License:Mozilla Public License

@Override
public void run(CommandLine line) throws Exception {
    Path outputCsvFile = Paths.get(line.getOptionValue("csv-file"));
    OnlineConfig config = OnlineConfig.load();
    OnlineDb onlinedb = config.getOnlineDbFactoryClass().newInstance().create();
    List<String> workflowsIds = new ArrayList<String>();
    if (line.hasOption("workflow"))
        workflowsIds.add(line.getOptionValue("workflow"));
    else if (line.hasOption("workflows"))
        workflowsIds = Arrays.asList(line.getOptionValue("workflows").split(","));
    else if (line.hasOption("basecase")) {
        DateTime basecaseDate = DateTime.parse(line.getOptionValue("basecase"));
        workflowsIds = onlinedb.listWorkflows(basecaseDate).stream().map(OnlineWorkflowDetails::getWorkflowId)
                .collect(Collectors.toList());
    } else if (line.hasOption("basecases-interval")) {
        Interval basecasesInterval = Interval.parse(line.getOptionValue("basecases-interval"));
        workflowsIds = onlinedb.listWorkflows(basecasesInterval).stream()
                .map(OnlineWorkflowDetails::getWorkflowId).collect(Collectors.toList());
    } else {//from w ww .  j  ava2 s  .  com
        System.out.println("You must specify workflow(s) or basecase(s)");
        return;
    }
    Collections.sort(workflowsIds, new Comparator<String>() {
        public int compare(String o1, String o2) {
            return o1.compareTo(o2);
        }
    });
    System.out.println("Printing performances of workflows " + workflowsIds);
    CsvWriter cvsWriter = null;
    try (FileWriter content = new FileWriter(outputCsvFile.toFile())) {
        cvsWriter = new CsvWriter(content, ',');
        String[] headers = new String[25];
        int i = 0;
        headers[i++] = "workflow_id";
        headers[i++] = "basecase";
        headers[i++] = "secure_contingencies";
        headers[i++] = "unsecure_contingencies";
        headers[i++] = "unsecure_contingencies_ratio";
        headers[i++] = "secure_contingencies_ratio";
        headers[i++] = "unsecure_secure_contingencies_ratio";
        headers[i++] = "wca_missed_alarms";
        headers[i++] = "wca_missed_alarms_lst";
        headers[i++] = "wca_false_alarms";
        headers[i++] = "wca_false_alarms_lst";
        headers[i++] = "wca_accuracy";
        headers[i++] = "wca_efficiency";
        headers[i++] = "mcla_missed_alarms";
        headers[i++] = "mcla_missed_alarms_lst";
        headers[i++] = "mcla_false_alarms";
        headers[i++] = "mcla_false_alarms_lst";
        headers[i++] = "mcla_accuracy";
        headers[i++] = "mcla_efficiency";
        headers[i++] = "wf_missed_alarms";
        headers[i++] = "wf_missed_alarms_lst";
        headers[i++] = "wf_false_alarms";
        headers[i++] = "wf_false_alarms_lst";
        headers[i++] = "wf_accuracy";
        headers[i++] = "wf_efficiency";
        cvsWriter.writeRecord(headers);
        // cycle over the workflows
        for (String workflowId : workflowsIds) {
            try {
                System.out.println("\nPrinting performances of workflow " + workflowId);
                OnlineWorkflowParameters parameters = onlinedb.getWorkflowParameters(workflowId);
                if (parameters != null && parameters.validation()) {
                    OnlineWorkflowResults wfResults = onlinedb.getResults(workflowId);
                    Map<String, Boolean> contigencySecure = new HashMap<String, Boolean>();
                    Map<String, Boolean> contigencyWCASecure = new HashMap<String, Boolean>();
                    Map<String, Boolean> contigencyMCLASecure = new HashMap<String, Boolean>();
                    Map<String, Boolean> contigencyWfSecure = new HashMap<String, Boolean>();
                    Map<String, Map<SecurityIndexType, Boolean>> contigencyPhenomenaSecure = new HashMap<String, Map<SecurityIndexType, Boolean>>();
                    Map<String, Map<SecurityIndexType, Boolean>> contigencyPhenomenaMCLASecure = new HashMap<String, Map<SecurityIndexType, Boolean>>();
                    int unsecureContingencies = 0;
                    int secureContingencies = 0;
                    int wcaFalseAlarms = 0;
                    List<String> wcaFalseAlarmsList = new ArrayList<String>();
                    int wcaMissedAlarms = 0;
                    List<String> wcaMissedAlarmsList = new ArrayList<String>();
                    int mclaFalseAlarms = 0;
                    List<String> mclaFalseAlarmsList = new ArrayList<String>();
                    int mclaMissedAlarms = 0;
                    List<String> mclaMissedAlarmsList = new ArrayList<String>();
                    int wfFalseAlarms = 0;
                    List<String> wfFalseAlarmsList = new ArrayList<String>();
                    int wfMissedAlarms = 0;
                    List<String> wfMissedAlarmsList = new ArrayList<String>();
                    if (wfResults != null) {
                        if (!wfResults.getUnsafeContingencies().isEmpty()) {
                            Network basecase = onlinedb.getState(workflowId, 0);
                            String basecaseId = basecase.getId();
                            OnlineWorkflowWcaResults wfWcaResults = onlinedb.getWcaResults(workflowId);
                            OnlineWorkflowRulesResults wfRulesResults = onlinedb.getRulesResults(workflowId);
                            SecurityIndexType[] securityIndexTypes = parameters.getSecurityIndexes() == null
                                    ? SecurityIndexType.values()
                                    : parameters.getSecurityIndexes().toArray(
                                            new SecurityIndexType[parameters.getSecurityIndexes().size()]);
                            for (String contingencyId : wfResults.getUnsafeContingencies()) {
                                // initialize values
                                contigencySecure.put(contingencyId, true);
                                if (wfWcaResults.getClusterIndex(contingencyId) == 1)
                                    contigencyWCASecure.put(contingencyId, true);
                                else
                                    contigencyWCASecure.put(contingencyId, false);
                                contigencyMCLASecure.put(contingencyId, true);
                                Map<SecurityIndexType, Boolean> phenomenaSecure = new HashMap<SecurityIndexType, Boolean>();
                                Map<SecurityIndexType, Boolean> phenomenaMCLASecure = new HashMap<SecurityIndexType, Boolean>();
                                for (SecurityIndexType securityIndexType : securityIndexTypes) {
                                    phenomenaSecure.put(securityIndexType, true);
                                    phenomenaMCLASecure.put(securityIndexType, true);
                                }
                                contigencyPhenomenaSecure.put(contingencyId, phenomenaSecure);
                                contigencyPhenomenaMCLASecure.put(contingencyId, phenomenaMCLASecure);
                                // compute values
                                for (Integer stateId : wfResults.getUnstableStates(contingencyId)) {
                                    Map<String, Boolean> securityIndexes = wfResults
                                            .getIndexesData(contingencyId, stateId);
                                    Map<String, Boolean> securityRules = wfRulesResults
                                            .getStateResults(contingencyId, stateId);
                                    for (SecurityIndexType securityIndexType : securityIndexTypes) {
                                        if (securityIndexes.containsKey(securityIndexType.getLabel())
                                                && !securityIndexes.get(securityIndexType.getLabel())) {
                                            contigencySecure.put(contingencyId, false);
                                            contigencyPhenomenaSecure.get(contingencyId).put(securityIndexType,
                                                    false);
                                        }
                                        if (securityRules.containsKey(securityIndexType.getLabel())
                                                && !securityRules.get(securityIndexType.getLabel())) {
                                            contigencyMCLASecure.put(contingencyId, false);
                                            contigencyPhenomenaMCLASecure.get(contingencyId)
                                                    .put(securityIndexType, false);
                                        }
                                    }
                                }
                                if (contigencyWCASecure.get(contingencyId)
                                        || (!contigencyWCASecure.get(contingencyId)
                                                && contigencyMCLASecure.get(contingencyId)))
                                    contigencyWfSecure.put(contingencyId, true);
                                else
                                    contigencyWfSecure.put(contingencyId, false);
                                // compute data for performances
                                if (contigencySecure.get(contingencyId))
                                    secureContingencies++;
                                else
                                    unsecureContingencies++;
                                if (!contigencySecure.get(contingencyId)
                                        && contigencyWCASecure.get(contingencyId)) {
                                    wcaMissedAlarms++;
                                    wcaMissedAlarmsList.add(contingencyId);
                                }
                                if (contigencySecure.get(contingencyId)
                                        && !contigencyWCASecure.get(contingencyId)) {
                                    wcaFalseAlarms++;
                                    wcaFalseAlarmsList.add(contingencyId);
                                }
                                if (!contigencySecure.get(contingencyId)
                                        && contigencyMCLASecure.get(contingencyId)) {
                                    mclaMissedAlarms++;
                                    mclaMissedAlarmsList.add(contingencyId);
                                }
                                if (contigencySecure.get(contingencyId)
                                        && !contigencyMCLASecure.get(contingencyId)) {
                                    mclaFalseAlarms++;
                                    mclaFalseAlarmsList.add(contingencyId);
                                }
                                if (!contigencySecure.get(contingencyId)
                                        && contigencyWfSecure.get(contingencyId)) {
                                    wfMissedAlarms++;
                                    wfMissedAlarmsList.add(contingencyId);
                                }
                                if (contigencySecure.get(contingencyId)
                                        && !contigencyWfSecure.get(contingencyId)) {
                                    wfFalseAlarms++;
                                    wfFalseAlarmsList.add(contingencyId);
                                }
                            }
                            // compute performances
                            float wcaAccuracy = (unsecureContingencies == 0) ? 100
                                    : (1f - ((float) wcaMissedAlarms / (float) unsecureContingencies)) * 100f;
                            float wcaEfficiency = (secureContingencies == 0) ? 100
                                    : (1f - ((float) wcaFalseAlarms / (float) secureContingencies)) * 100f;
                            float mclaAccuracy = (unsecureContingencies == 0) ? 100
                                    : (1f - ((float) mclaMissedAlarms / (float) unsecureContingencies)) * 100f;
                            float mclaEfficiency = (secureContingencies == 0) ? 100
                                    : (1f - ((float) mclaFalseAlarms / (float) secureContingencies)) * 100f;
                            float wfAccuracy = (unsecureContingencies == 0) ? 100
                                    : (1f - ((float) wfMissedAlarms / (float) unsecureContingencies)) * 100f;
                            float wfEfficiency = (secureContingencies == 0) ? 100
                                    : (1f - ((float) wfFalseAlarms / (float) secureContingencies)) * 100f;
                            float unsecureRatio = (float) unsecureContingencies
                                    / (float) (secureContingencies + unsecureContingencies);
                            float secureRatio = (float) secureContingencies
                                    / (float) (secureContingencies + unsecureContingencies);
                            float secureUnsecureRatio = (secureContingencies == 0) ? Float.NaN
                                    : (float) unsecureContingencies / (float) secureContingencies;
                            //                     System.out.println("contigencySecure: " + contigencySecure);
                            //                     System.out.println("contigencyWCASecure: " + contigencyWCASecure);
                            //                     System.out.println("contigencyMCLASecure: " + contigencyMCLASecure);
                            //                     System.out.println("contigencyWfSecure: " + contigencyWfSecure);
                            //                     System.out.println("contigencyPhenomenaSecure: " + contigencyPhenomenaSecure);
                            //                     System.out.println("contigencyPhenomenaMCLASecure: " + contigencyPhenomenaMCLASecure);
                            // print performances
                            String[] values = new String[25];
                            i = 0;
                            values[i++] = workflowId;
                            values[i++] = basecaseId;
                            values[i++] = Integer.toString(secureContingencies);
                            values[i++] = Integer.toString(unsecureContingencies);
                            values[i++] = Float.toString(unsecureRatio);
                            values[i++] = Float.toString(secureRatio);
                            values[i++] = Float.toString(secureUnsecureRatio);
                            values[i++] = Integer.toString(wcaMissedAlarms);
                            values[i++] = wcaMissedAlarmsList.toString();
                            values[i++] = Integer.toString(wcaFalseAlarms);
                            values[i++] = wcaFalseAlarmsList.toString();
                            values[i++] = Float.toString(wcaAccuracy);
                            values[i++] = Float.toString(wcaEfficiency);
                            values[i++] = Integer.toString(mclaMissedAlarms);
                            values[i++] = mclaMissedAlarmsList.toString();
                            values[i++] = Integer.toString(mclaFalseAlarms);
                            values[i++] = mclaFalseAlarmsList.toString();
                            values[i++] = Float.toString(mclaAccuracy);
                            values[i++] = Float.toString(mclaEfficiency);
                            values[i++] = Integer.toString(wfMissedAlarms);
                            values[i++] = wfMissedAlarmsList.toString();
                            values[i++] = Integer.toString(wfFalseAlarms);
                            values[i++] = wfFalseAlarmsList.toString();
                            values[i++] = Float.toString(wfAccuracy);
                            values[i++] = Float.toString(wfEfficiency);
                            cvsWriter.writeRecord(values);
                            cvsWriter.flush();
                        } else
                            System.out.println("No data for benchmark: skipping wf " + workflowId);
                    } else {
                        System.out.println("No results: skipping wf " + workflowId);
                    }
                } else
                    System.out.println("No data for validation: skipping wf " + workflowId);
            } catch (IOException e1) {
            }
        }
    } finally {
        if (cvsWriter != null)
            cvsWriter.close();
        onlinedb.close();
    }
}

From source file:eu.itesla_project.online.tools.PrintOnlineWorkflowSummaryTable.java

License:Mozilla Public License

@Override
public void run(CommandLine line) throws Exception {
    OnlineConfig config = OnlineConfig.load();
    try (OnlineDb onlinedb = config.getOnlineDbFactoryClass().newInstance().create()) {
        List<String> workflowsIds = new ArrayList<String>();
        if (line.hasOption("workflow"))
            workflowsIds.add(line.getOptionValue("workflow"));
        else if (line.hasOption("workflows"))
            workflowsIds = Arrays.asList(line.getOptionValue("workflows").split(","));
        else if (line.hasOption("basecase")) {
            DateTime basecaseDate = DateTime.parse(line.getOptionValue("basecase"));
            workflowsIds = onlinedb.listWorkflows(basecaseDate).stream()
                    .map(OnlineWorkflowDetails::getWorkflowId).collect(Collectors.toList());
        } else if (line.hasOption("basecases-interval")) {
            Interval basecasesInterval = Interval.parse(line.getOptionValue("basecases-interval"));
            workflowsIds = onlinedb.listWorkflows(basecasesInterval).stream()
                    .map(OnlineWorkflowDetails::getWorkflowId).collect(Collectors.toList());
        } else {//from w w w .  j  a  v  a  2 s.  c om
            System.out.println("You must specify workflow(s) or basecase(s)");
            return;
        }
        TableFormatterConfig tableFormatterConfig = TableFormatterConfig.load();
        try (TableFormatter formatter = PrintOnlineWorkflowUtils.createFormatter(tableFormatterConfig,
                (line.hasOption("output-file")) ? Paths.get(line.getOptionValue("output-file")) : null,
                TABLE_TITLE, new Column("WorkflowId"), new Column("Basecase"), new Column("Contingency"),
                new Column("State"), new Column("FailureStep"), new Column("FailureDescription"),
                new Column("ViolationType"), new Column("Violation"), new Column("ViolationStep"),
                new Column("Equipment"), new Column("Value"), new Column("Limit"))) {

            workflowsIds.sort((o1, o2) -> (o1.compareTo(o2)));
            System.out.println("Printing violations and failues of workflows " + workflowsIds);
            workflowsIds.forEach(workflowId -> {
                System.out.println("Printing violations and failures of workflow " + workflowId);
                Network basecase = onlinedb.getState(workflowId, 0);
                String basecaseId = basecase.getId();
                printPrecontingencyViolations(workflowId, basecaseId, onlinedb, formatter);
                printContingenciesViolations(workflowId, basecaseId, onlinedb, formatter);
            });
        }
    }
}

From source file:eu.itesla_project.online.tools.RunForecastErrorsAnalysisMpiTool.java

License:Mozilla Public License

@Override
public void run(CommandLine line) throws Exception {

    OnlineWorkflowStartParameters startconfig = OnlineWorkflowStartParameters.loadDefault();

    String host = line.getOptionValue(OnlineWorkflowCommand.HOST);
    String port = line.getOptionValue(OnlineWorkflowCommand.PORT);
    String threads = line.getOptionValue(OnlineWorkflowCommand.THREADS);
    if (host != null)
        startconfig.setJmxHost(host);/*from  ww w  .  j  a va 2s.c o m*/
    if (port != null)
        startconfig.setJmxPort(Integer.valueOf(port));
    if (threads != null)
        startconfig.setThreads(Integer.valueOf(threads));

    String analysisId = line.getOptionValue("analysis");
    DateTime baseCaseDate = line.hasOption("base-case-date")
            ? DateTime.parse(line.getOptionValue("base-case-date"))
            : getDefaultParameters().getBaseCaseDate();
    Interval histoInterval = line.hasOption("history-interval")
            ? Interval.parse(line.getOptionValue("history-interval"))
            : getDefaultParameters().getHistoInterval();
    double ir = line.hasOption("ir") ? Double.parseDouble(line.getOptionValue("ir"))
            : getDefaultParameters().getIr();
    int flagPQ = line.hasOption("flagPQ") ? Integer.parseInt(line.getOptionValue("flagPQ"))
            : getDefaultParameters().getFlagPQ();
    int method = line.hasOption("method") ? Integer.parseInt(line.getOptionValue("method"))
            : getDefaultParameters().getMethod();
    Integer nClusters = line.hasOption("nClusters") ? Integer.parseInt(line.getOptionValue("nClusters"))
            : getDefaultParameters().getnClusters();
    double percentileHistorical = line.hasOption("percentileHistorical")
            ? Double.parseDouble(line.getOptionValue("percentileHistorical"))
            : getDefaultParameters().getPercentileHistorical();
    Integer modalityGaussian = line.hasOption("modalityGaussian")
            ? Integer.parseInt(line.getOptionValue("modalityGaussian"))
            : getDefaultParameters().getModalityGaussian();
    Integer outliers = line.hasOption("outliers") ? Integer.parseInt(line.getOptionValue("outliers"))
            : getDefaultParameters().getOutliers();
    Integer conditionalSampling = line.hasOption("conditionalSampling")
            ? Integer.parseInt(line.getOptionValue("conditionalSampling"))
            : getDefaultParameters().getConditionalSampling();
    Integer nSamples = line.hasOption("nSamples") ? Integer.parseInt(line.getOptionValue("nSamples"))
            : getDefaultParameters().getnSamples();
    Set<Country> countries = line.hasOption("countries")
            ? Arrays.stream(line.getOptionValue("countries").split(",")).map(Country::valueOf).collect(
                    Collectors.toSet())
            : getDefaultParameters().getCountries();
    CaseType caseType = line.hasOption("case-type") ? CaseType.valueOf(line.getOptionValue("case-type"))
            : getDefaultParameters().getCaseType();

    ForecastErrorsAnalysisParameters parameters = new ForecastErrorsAnalysisParameters(baseCaseDate,
            histoInterval, analysisId, ir, flagPQ, method, nClusters, percentileHistorical, modalityGaussian,
            outliers, conditionalSampling, nSamples, countries, caseType);

    String urlString = "service:jmx:rmi:///jndi/rmi://" + startconfig.getJmxHost() + ":"
            + startconfig.getJmxPort() + "/jmxrmi";

    JMXServiceURL serviceURL = new JMXServiceURL(urlString);
    Map<String, String> jmxEnv = new HashMap<>();
    JMXConnector connector = JMXConnectorFactory.connect(serviceURL, jmxEnv);
    MBeanServerConnection mbsc = connector.getMBeanServerConnection();

    ObjectName name = new ObjectName(LocalOnlineApplicationMBean.BEAN_NAME);
    LocalOnlineApplicationMBean application = MBeanServerInvocationHandler.newProxyInstance(mbsc, name,
            LocalOnlineApplicationMBean.class, false);
    String timeHorizonS = "";
    if (line.hasOption("time-horizon")) {
        timeHorizonS = line.getOptionValue("time-horizon");
    }
    application.runFeaAnalysis(startconfig, parameters, timeHorizonS);

}

From source file:eu.itesla_project.online.tools.RunForecastErrorsAnalysisTool.java

License:Mozilla Public License

@Override
public void run(CommandLine line) throws Exception {
    ComputationManager computationManager = new LocalComputationManager();
    String analysisId = line.getOptionValue("analysis");
    DateTime baseCaseDate = line.hasOption("base-case-date")
            ? DateTime.parse(line.getOptionValue("base-case-date"))
            : getDefaultParameters().getBaseCaseDate();
    Interval histoInterval = line.hasOption("history-interval")
            ? Interval.parse(line.getOptionValue("history-interval"))
            : getDefaultParameters().getHistoInterval();
    double ir = line.hasOption("ir") ? Double.parseDouble(line.getOptionValue("ir"))
            : getDefaultParameters().getIr();
    int flagPQ = line.hasOption("flagPQ") ? Integer.parseInt(line.getOptionValue("flagPQ"))
            : getDefaultParameters().getFlagPQ();
    int method = line.hasOption("method") ? Integer.parseInt(line.getOptionValue("method"))
            : getDefaultParameters().getMethod();
    Integer nClusters = line.hasOption("nClusters") ? Integer.parseInt(line.getOptionValue("nClusters"))
            : getDefaultParameters().getnClusters();
    double percentileHistorical = line.hasOption("percentileHistorical")
            ? Double.parseDouble(line.getOptionValue("percentileHistorical"))
            : getDefaultParameters().getPercentileHistorical();
    Integer modalityGaussian = line.hasOption("modalityGaussian")
            ? Integer.parseInt(line.getOptionValue("modalityGaussian"))
            : getDefaultParameters().getModalityGaussian();
    Integer outliers = line.hasOption("outliers") ? Integer.parseInt(line.getOptionValue("outliers"))
            : getDefaultParameters().getOutliers();
    Integer conditionalSampling = line.hasOption("conditionalSampling")
            ? Integer.parseInt(line.getOptionValue("conditionalSampling"))
            : getDefaultParameters().getConditionalSampling();
    Integer nSamples = line.hasOption("nSamples") ? Integer.parseInt(line.getOptionValue("nSamples"))
            : getDefaultParameters().getnSamples();
    Set<Country> countries = line.hasOption("countries")
            ? Arrays.stream(line.getOptionValue("countries").split(",")).map(Country::valueOf).collect(
                    Collectors.toSet())
            : getDefaultParameters().getCountries();
    CaseType caseType = line.hasOption("case-type") ? CaseType.valueOf(line.getOptionValue("case-type"))
            : getDefaultParameters().getCaseType();

    ForecastErrorsAnalysisParameters parameters = new ForecastErrorsAnalysisParameters(baseCaseDate,
            histoInterval, analysisId, ir, flagPQ, method, nClusters, percentileHistorical, modalityGaussian,
            outliers, conditionalSampling, nSamples, countries, caseType);
    ForecastErrorsAnalysis feAnalysis = new ForecastErrorsAnalysis(computationManager,
            ForecastErrorsAnalysisConfig.load(), parameters);
    System.out.println("Starting Forecast Errors Analysis");
    if (line.hasOption("time-horizon")) {
        TimeHorizon timeHorizon = TimeHorizon.fromName(line.getOptionValue("time-horizon"));
        feAnalysis.start(timeHorizon);//  ww w. ja  v a2s.  c  o m
    } else
        feAnalysis.start();
    System.out.println("Forecast Errors Analysis Terminated");
}

From source file:eu.itesla_project.online.tools.RunWcaOnStateTool.java

License:Mozilla Public License

@Override
public void run(CommandLine line) throws Exception {
    String workflowId = line.getOptionValue("workflow");
    Integer stateId = Integer.valueOf(line.getOptionValue("state"));
    System.out.println("loading state " + stateId + " of workflow " + workflowId + " from the online db ...");
    OnlineConfig config = OnlineConfig.load();
    OnlineDb onlinedb = config.getOnlineDbFactoryClass().newInstance().create();
    // load the network
    Network network = onlinedb.getState(workflowId, stateId);
    if (network != null) {
        OnlineWorkflowParameters parameters = onlinedb.getWorkflowParameters(workflowId);
        String offlineWorkflowId = parameters.getOfflineWorkflowId();
        if (line.hasOption("offline-workflow"))
            offlineWorkflowId = line.getOptionValue("offline-workflow");
        Interval histoInterval = parameters.getHistoInterval();
        if (line.hasOption("history-interval"))
            histoInterval = Interval.parse(line.getOptionValue("history-interval"));
        double purityThreshold = parameters.getRulesPurityThreshold();
        if (line.hasOption("purity-threshold"))
            purityThreshold = Double.parseDouble(line.getOptionValue("purity-threshold"));
        Set<SecurityIndexType> securityIndexTypes = parameters.getSecurityIndexes();
        if (line.hasOption("security-index-types")) {
            securityIndexTypes = Arrays.stream(line.getOptionValue("security-index-types").split(","))
                    .map(SecurityIndexType::valueOf).collect(Collectors.toSet());
        }//from   w  ww .ja  v  a  2s. com
        boolean stopWcaOnViolations = DEFAULT_STOP_WCA_ON_VIOLATIONS;
        if (line.hasOption("stop-on-violations")) {
            stopWcaOnViolations = Boolean.parseBoolean(line.getOptionValue("stop-on-violations"));
        }
        ComputationManager computationManager = new LocalComputationManager();
        network.getStateManager().allowStateMultiThreadAccess(true);
        WCAParameters wcaParameters = new WCAParameters(histoInterval, offlineWorkflowId, securityIndexTypes,
                purityThreshold, stopWcaOnViolations);
        ContingenciesAndActionsDatabaseClient contingenciesDb = config.getContingencyDbClientFactoryClass()
                .newInstance().create();
        LoadFlowFactory loadFlowFactory = config.getLoadFlowFactoryClass().newInstance();
        try (HistoDbClient histoDbClient = config.getHistoDbClientFactoryClass().newInstance().create();
                RulesDbClient rulesDbClient = config.getRulesDbClientFactoryClass().newInstance()
                        .create("rulesdb")) {
            UncertaintiesAnalyserFactory uncertaintiesAnalyserFactory = config
                    .getUncertaintiesAnalyserFactoryClass().newInstance();
            WCA wca = config.getWcaFactoryClass().newInstance().create(network, computationManager,
                    histoDbClient, rulesDbClient, uncertaintiesAnalyserFactory, contingenciesDb,
                    loadFlowFactory);
            WCAResult result = wca.run(wcaParameters);
            Table table = new Table(7, BorderStyle.CLASSIC_WIDE);
            table.addCell("Contingency", new CellStyle(CellStyle.HorizontalAlign.center));
            table.addCell("Cluster 1", new CellStyle(CellStyle.HorizontalAlign.center));
            table.addCell("Cluster 2", new CellStyle(CellStyle.HorizontalAlign.center));
            table.addCell("Cluster 3", new CellStyle(CellStyle.HorizontalAlign.center));
            table.addCell("Cluster 4", new CellStyle(CellStyle.HorizontalAlign.center));
            table.addCell("Undefined", new CellStyle(CellStyle.HorizontalAlign.center));
            table.addCell("Cause", new CellStyle(CellStyle.HorizontalAlign.center));
            for (WCACluster cluster : result.getClusters()) {
                table.addCell(cluster.getContingency().getId());
                int[] clusterIndexes = new int[] { 1, 2, 3, 4, -1 };
                for (int k = 0; k < clusterIndexes.length; k++) {
                    if (clusterIndexes[k] == cluster.getNum().toIntValue()) {
                        table.addCell("X", new CellStyle(CellStyle.HorizontalAlign.center));
                    } else {
                        table.addCell("-", new CellStyle(CellStyle.HorizontalAlign.center));
                    }
                }
                table.addCell(Objects.toString(cluster.getCauses(), ""),
                        new CellStyle(CellStyle.HorizontalAlign.center));
            }
            System.out.println(table.render());
        }
    }
}

From source file:google.registry.tools.params.IntervalParameter.java

License:Open Source License

@Override
public Interval convert(String value) {
    // Interval.parse(null) creates an interval with both start and end times set to now.
    // Do something a little more reasonable.
    if (value == null) {
        throw new NullPointerException();
    }/*from   ww  w.ja  v a  2  s . com*/
    Interval interval = Interval.parse(value);
    // Interval does not have a way to set the time zone, so create a new interval with the
    // start and end times of the parsed interval converted to UTC.
    return new Interval(interval.getStart().withZone(DateTimeZone.UTC),
            interval.getEnd().withZone(DateTimeZone.UTC));
}

From source file:io.coala.time.TimeSlot.java

License:Apache License

public static TimeSlot valueOf(final String value) {
    return new TimeSlot(Interval.parse(value));
}

From source file:org.apache.abdera2.activities.io.gson.IntervalAdapter.java

License:Apache License

protected Interval deserialize(String v) {
    return Interval.parse(v);
}

From source file:org.filteredpush.qc.date.DateUtils.java

License:Apache License

/**
 * Given a string that may represent a date or range of dates, or date time or range of date times,
 * attempt to extract a standard date from that string.
 * //w w  w. j  a va  2 s. com
 * @param verbatimEventDate a string containing a verbatim event date.
 * @param yearsBeforeSuspect  Dates that parse to a year prior to this year are marked as suspect.
 * @param assumemmddyyyy if true, assume that dates in the form nn-nn-nnnn are mm-dd-yyyy, if false, assume 
 *       that these are dd-mm-yyyy, if null, such dates are tested for ambiguity.  
 * 
 * @return an EventResult with a resultState for the nature of the match and result for the resulting date. 
 */
public static EventResult extractDateFromVerbatimER(String verbatimEventDate, int yearsBeforeSuspect,
        Boolean assumemmddyyyy) {
    EventResult result = new EventResult();
    String resultDate = null;

    // Remove some common no data comments
    if (verbatimEventDate != null && verbatimEventDate.contains("[no date]")) {
        verbatimEventDate = verbatimEventDate.replace("[no date]", "");
    }
    if (verbatimEventDate != null && verbatimEventDate.contains("[no year]")) {
        verbatimEventDate = verbatimEventDate.replace("[no year]", "");
    }

    // Strip off leading and trailing []
    if (verbatimEventDate != null && verbatimEventDate.startsWith("[") && verbatimEventDate.endsWith("]")) {
        verbatimEventDate = verbatimEventDate.substring(1);
        verbatimEventDate = verbatimEventDate.substring(0, verbatimEventDate.length() - 1);
    }

    if (verbatimEventDate != null && verbatimEventDate.matches(".*\\[[0-9]+\\].*")) {
        verbatimEventDate = verbatimEventDate.replace("[", "").replace("]", "");
    }

    // Strip off leading and trailing quotation marks
    if (verbatimEventDate != null && verbatimEventDate.startsWith("\"") && verbatimEventDate != null
            && verbatimEventDate.endsWith("\"")) {
        verbatimEventDate = verbatimEventDate.substring(1, verbatimEventDate.length() - 1);
    }

    // strip off leading and trailing whitespace
    if (verbatimEventDate != null && (verbatimEventDate.startsWith(" ") || verbatimEventDate.endsWith(" "))) {
        verbatimEventDate = verbatimEventDate.trim();
    }
    // strip off trailing period after number
    if (verbatimEventDate != null && verbatimEventDate.endsWith(".")
            && verbatimEventDate.matches(".*[0-9]\\.$")) {
        verbatimEventDate = verbatimEventDate.substring(0, verbatimEventDate.length() - 1);
        logger.debug(verbatimEventDate);
    }

    // Stop before doing work if provided verbatim string is null.
    if (isEmpty(verbatimEventDate)) {
        return result;
    }

    if (verbatimEventDate.matches("^[0-9]{4}[-][0-9]{2}[-][0-9]{2}/[0-9]{4}[-][0-9]{2}[-][0-9]{2}$")) {
        // if verbatim date is a ISO formatted range with identical first and last dates (/), use just one.
        // Example: 1982-12-11/1982-12-11  changed to 1982-12-11
        String[] bits = verbatimEventDate.split("/");
        if (bits.length == 2 && bits[0].equals(bits[1])) {
            verbatimEventDate = bits[0];
        }
    }
    if (verbatimEventDate.matches("^[0-9]{4}[/][0-9]{2}[/][0-9]{2}-[0-9]{4}[/][0-9]{2}[/][0-9]{2}$")) {
        // if verbatim date is a range with identical first and last dates (-), use just one.
        // Example: 1982/12/11-1982/12/11  changed to 1982/12/11
        String[] bits = verbatimEventDate.split("-");
        if (bits.length == 2 && bits[0].equals(bits[1])) {
            verbatimEventDate = bits[0];
        }
    }
    if (verbatimEventDate
            .matches("^[0-9]{1,2}[-. ][0-9]{1,2}[-. ][0-9]{4}/[0-9]{1,2}[-. ][0-9]{1,2}[-. ][0-9]{4}$")) {
        // if verbatim date is a range with identical first and last dates (/), use just one.
        // Example: 12-11-1982/12-11-1982  changed to 12-11-1982
        String[] bits = verbatimEventDate.split("/");
        if (bits.length == 2 && bits[0].equals(bits[1])) {
            verbatimEventDate = bits[0];
        }
    }
    if (verbatimEventDate
            .matches("^[0-9]{1,2}[./ ][0-9]{1,2}[./ ][0-9]{4}[-][0-9]{1,2}[./ ][0-9]{1,2}[./ ][0-9]{4}$")) {
        // if verbatim date is a range with identical first and last dates (-), use just one.
        // Example: 12/11/1982-12/11/1982  changed to 12/11/1982
        String[] bits = verbatimEventDate.split("-");
        if (bits.length == 2 && bits[0].equals(bits[1])) {
            verbatimEventDate = bits[0];
        }
    }
    if (verbatimEventDate.matches("^[0-9]{4}[-]([0-9]{1,2}|[A-Za-z]+)[-][0-9]{1,2}.*")) {
        // Both separators are the same.
        // Example 1982-02-05
        // Example 1982-Feb-05
        // Example 1982-02-05
        // Example 1982-02-05T05:03:06
        try {
            DateTimeParser[] parsers = { DateTimeFormat.forPattern("yyyy/MM/dd").getParser(),
                    DateTimeFormat.forPattern("yyyy/MMM/dd").getParser(),
                    DateTimeFormat.forPattern("yyyy-MMM-dd").getParser(),
                    ISODateTimeFormat.dateOptionalTimeParser().getParser() };
            DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers).toFormatter();
            DateMidnight parseDate = LocalDate.parse(verbatimEventDate, formatter).toDateMidnight();
            resultDate = parseDate.toString("yyyy-MM-dd");
            result.setResultState(EventResult.EventQCResultState.DATE);
            result.setResult(resultDate);
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
    }
    if (verbatimEventDate.matches("^[0-9]{4}[/]([0-9]{1,2}|[A-Za-z]+)[/][0-9]{1,2}.*")) {
        // Both separators are the same.
        // Example 1982/02/05
        // Example 1982/Feb/05
        // Example 1982-02-05
        // Example 1982/02/05T05:03:06
        try {
            DateTimeParser[] parsers = { DateTimeFormat.forPattern("yyyy/MM/dd").getParser(),
                    DateTimeFormat.forPattern("yyyy/MMM/dd").getParser(),
                    DateTimeFormat.forPattern("yyyy-MMM-dd").getParser(),
                    ISODateTimeFormat.dateOptionalTimeParser().getParser() };
            DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers).toFormatter();
            DateMidnight parseDate = LocalDate.parse(verbatimEventDate, formatter).toDateMidnight();
            resultDate = parseDate.toString("yyyy-MM-dd");
            result.setResultState(EventResult.EventQCResultState.DATE);
            result.setResult(resultDate);
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
    }
    if (verbatimEventDate.matches("^[0-9]{4}[.,][0-9]{1,2}[.,][0-9]{1,2}$")) {
        // Example 1982.02.05
        // Example 1982,02,05
        // Cases where the 1-2 digit numbers are both smaller than 12 are treated as ambiguous.
        String resultDateMD = null;
        String resultDateDM = null;
        DateMidnight parseDate1 = null;
        DateMidnight parseDate2 = null;
        try {
            DateTimeParser[] parsers = { DateTimeFormat.forPattern("yyyy.MM.dd").getParser(),
                    DateTimeFormat.forPattern("yyyy,MM,dd").getParser(),
                    DateTimeFormat.forPattern("yyyy,MM.dd").getParser(),
                    DateTimeFormat.forPattern("yyyy.MM,dd").getParser() };
            DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers).toFormatter();
            parseDate1 = LocalDate.parse(verbatimEventDate, formatter).toDateMidnight();
            resultDateMD = parseDate1.toString("yyyy-MM-dd");
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
        try {
            DateTimeParser[] parsers = { DateTimeFormat.forPattern("yyyy.dd.MM").getParser(),
                    DateTimeFormat.forPattern("yyyy,dd,MM").getParser(),
                    DateTimeFormat.forPattern("yyyy,dd.MM").getParser(),
                    DateTimeFormat.forPattern("yyyy.dd,MM").getParser() };
            DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers).toFormatter();
            parseDate2 = LocalDate.parse(verbatimEventDate, formatter).toDateMidnight();
            resultDateDM = parseDate2.toString("yyyy-MM-dd");
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
        if (resultDateMD != null && resultDateDM == null) {
            result.setResultState(EventResult.EventQCResultState.DATE);
            result.setResult(resultDateMD);
        } else if (resultDateMD == null && resultDateDM != null) {
            result.setResultState(EventResult.EventQCResultState.DATE);
            result.setResult(resultDateDM);
        } else if (resultDateMD != null && resultDateDM != null) {
            if (resultDateMD.equals(resultDateDM)) {
                result.setResultState(EventResult.EventQCResultState.DATE);
                result.setResult(resultDateDM);
            } else {
                result.setResultState(EventResult.EventQCResultState.AMBIGUOUS);
                Interval range = null;
                if (parseDate1.isBefore(parseDate2)) {
                    result.setResult(resultDateMD + "/" + resultDateDM);
                } else {
                    result.setResult(resultDateDM + "/" + resultDateMD);
                }
            }
        }

    }
    if (verbatimEventDate.matches("^[0-9]{1,2}[-/ ][0-9]{4}")) {
        // Example 02/1982
        try {
            DateTimeParser[] parsers = { DateTimeFormat.forPattern("MM-yyyy").getParser(),
                    DateTimeFormat.forPattern("MM/yyyy").getParser(),
                    DateTimeFormat.forPattern("MM yyyy").getParser() };
            DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers).toFormatter();
            DateMidnight parseDate = LocalDate.parse(verbatimEventDate, formatter).toDateMidnight();
            resultDate = parseDate.toString("yyyy-MM");
            result.setResultState(EventResult.EventQCResultState.RANGE);
            result.setResult(resultDate);
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
    }
    if (verbatimEventDate.matches("^[0-9]{4}[0-9]{1,2}[0-9]{1,2}[?]$")) {
        // Example: 19720325
        try {
            DateTimeParser[] parsers = { DateTimeFormat.forPattern("yyyyMMdd").getParser(),
                    DateTimeFormat.forPattern("yyyyMMdd?").getParser(),
                    ISODateTimeFormat.dateOptionalTimeParser().getParser() };
            DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers).toFormatter()
                    .withLocale(Locale.CHINESE);
            DateMidnight parseDate = LocalDate.parse(verbatimEventDate, formatter).toDateMidnight();
            resultDate = parseDate.toString("yyyy-MM-dd");
            result.setResultState(EventResult.EventQCResultState.DATE);
            result.setResult(resultDate);
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
    }
    if (verbatimEventDate.matches("^[0-9]{4}[-][0-9]{3}/[0-9]{4}[-][0-9]{3}$")) {
        // Example: 1982-145
        try {
            String[] bits = verbatimEventDate.split("/");
            DateTimeParser[] parsers = { DateTimeFormat.forPattern("yyyy-D").getParser() };
            DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers).toFormatter();
            LocalDate parseStartDate = LocalDate.parse(bits[0], formatter);
            LocalDate parseEndDate = LocalDate.parse(bits[1], formatter);
            resultDate = parseStartDate.toString("yyyy-MM-dd") + "/" + parseEndDate.toString("yyyy-MM-dd");
            logger.debug(resultDate);
            result.setResultState(EventResult.EventQCResultState.RANGE);
            result.setResult(resultDate);
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
    }
    if (result.getResultState().equals(EventResult.EventQCResultState.NOT_RUN)
            && verbatimEventDate.matches("^[0-9]{4}0000$")) {
        // case 19800000
        verbatimEventDate = verbatimEventDate.substring(0, 4);
    }
    if (result.getResultState().equals(EventResult.EventQCResultState.NOT_RUN)
            && verbatimEventDate.matches("^[0-9]{4}$")) {
        // Example: 1962 
        try {
            DateTimeParser[] parsers = { DateTimeFormat.forPattern("yyyy").getParser(), };
            DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers).toFormatter();
            DateMidnight parseDate = LocalDate.parse(verbatimEventDate, formatter).toDateMidnight();
            resultDate = parseDate.toString("yyyy");
            result.setResultState(EventResult.EventQCResultState.RANGE);
            result.setResult(resultDate);
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
    }
    if (result.getResultState().equals(EventResult.EventQCResultState.NOT_RUN)
            && verbatimEventDate.matches("^[12][0-9]{1}00[']{0,1}s$")) {
        // Example: 1900s 
        try {
            String verbatimEventDateDelta = verbatimEventDate.replace("'s", "s");
            DateTimeParser[] parsers = { DateTimeFormat.forPattern("yyyy's").getParser(), };
            DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers).toFormatter();
            DateMidnight parseDate = LocalDate.parse(verbatimEventDateDelta, formatter).toDateMidnight();
            DateMidnight endDate = parseDate.plusYears(100).minusDays(1);
            resultDate = parseDate.toString("yyyy") + "-01-01/" + endDate.toString("yyyy") + "-12-31";
            result.setResultState(EventResult.EventQCResultState.RANGE);
            result.setResult(resultDate);
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
    }
    if (result.getResultState().equals(EventResult.EventQCResultState.NOT_RUN)
            && verbatimEventDate.matches("^[12][0-9]{2}0[']{0,1}s$")) {
        // Example: 1970s 
        try {
            String verbatimEventDateDelta = verbatimEventDate.replace("'s", "s");
            DateTimeParser[] parsers = { DateTimeFormat.forPattern("yyyy's").getParser(), };
            DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers).toFormatter();
            DateMidnight parseDate = LocalDate.parse(verbatimEventDateDelta, formatter).toDateMidnight();
            DateMidnight endDate = parseDate.plusYears(10).minusDays(1);
            resultDate = parseDate.toString("yyyy") + "-01-01/" + endDate.toString("yyyy") + "-12-31";
            result.setResultState(EventResult.EventQCResultState.RANGE);
            result.setResult(resultDate);
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
    }
    if (result.getResultState().equals(EventResult.EventQCResultState.NOT_RUN)
            && verbatimEventDate.matches("^[A-Za-z]{3,9}[.]{0,1}[ ]{0,1}[-/ ][0-9]{4}$")) {
        // Example: Jan-1980
        // Example: Jan./1980
        // Example: January 1980
        try {
            DateTimeParser[] parsers = { DateTimeFormat.forPattern("MMM-yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM/yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM /yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM yyyy").getParser() };
            DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers).toFormatter();
            String cleaned = verbatimEventDate.replace(".", "");
            DateMidnight parseDate = LocalDate.parse(cleaned, formatter).toDateMidnight();
            resultDate = parseDate.toString("yyyy-MM");
            result.setResultState(EventResult.EventQCResultState.RANGE);
            result.setResult(resultDate);
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
    }
    if (result.getResultState().equals(EventResult.EventQCResultState.NOT_RUN)) {
        // Example: 04/03/1994  (ambiguous)
        // Example: 04/20/1994
        // Example: 20/04/1994
        String resultDateMD = null;
        String resultDateDM = null;
        DateMidnight parseDate1 = null;
        DateMidnight parseDate2 = null;
        if (assumemmddyyyy == null || assumemmddyyyy) {
            try {
                DateTimeParser[] parsers = { DateTimeFormat.forPattern("MM/dd/yyyy").getParser(),
                        DateTimeFormat.forPattern("MM/dd yyyy").getParser(),
                        DateTimeFormat.forPattern("MM/dd-yyyy").getParser(),
                        DateTimeFormat.forPattern("MM/dd, yyyy").getParser(),
                        DateTimeFormat.forPattern("MM/dd,yyyy").getParser(),
                        DateTimeFormat.forPattern("MM dd yyyy").getParser(),
                        DateTimeFormat.forPattern("MM-dd-yyyy").getParser(),
                        DateTimeFormat.forPattern("MM.dd.yyyy").getParser(),
                        DateTimeFormat.forPattern("MM. dd. yyyy").getParser(),
                        DateTimeFormat.forPattern("MM. dd. yyyy.").getParser() };
                DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers)
                        .toFormatter();
                parseDate1 = LocalDate.parse(verbatimEventDate, formatter).toDateMidnight();
                resultDateMD = parseDate1.toString("yyyy-MM-dd");
            } catch (Exception e) {
                logger.debug(e.getMessage());
            }
        }
        if (assumemmddyyyy == null || !assumemmddyyyy) {
            try {
                DateTimeParser[] parsers = { DateTimeFormat.forPattern("dd/MM/yyyy").getParser(),
                        DateTimeFormat.forPattern("dd/MM yyyy").getParser(),
                        DateTimeFormat.forPattern("dd/MM-yyyy").getParser(),
                        DateTimeFormat.forPattern("dd/MM, yyyy").getParser(),
                        DateTimeFormat.forPattern("dd/MM,yyyy").getParser(),
                        DateTimeFormat.forPattern("dd MM yyyy").getParser(),
                        DateTimeFormat.forPattern("dd-MM-yyyy").getParser(),
                        DateTimeFormat.forPattern("dd.MM.yyyy").getParser(),
                        DateTimeFormat.forPattern("dd. MM. yyyy").getParser(),
                        DateTimeFormat.forPattern("dd. MM. yyyy.").getParser() };
                DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers)
                        .toFormatter();
                parseDate2 = LocalDate.parse(verbatimEventDate, formatter).toDateMidnight();
                resultDateDM = parseDate2.toString("yyyy-MM-dd");
            } catch (Exception e) {
                logger.debug(e.getMessage());
            }
        }
        if (resultDateMD != null && resultDateDM == null) {
            result.setResultState(EventResult.EventQCResultState.DATE);
            result.setResult(resultDateMD);
        } else if (resultDateMD == null && resultDateDM != null) {
            result.setResultState(EventResult.EventQCResultState.DATE);
            result.setResult(resultDateDM);
        } else if (resultDateMD != null && resultDateDM != null) {
            if (resultDateMD.equals(resultDateDM)) {
                result.setResultState(EventResult.EventQCResultState.DATE);
                result.setResult(resultDateDM);
            } else {
                result.setResultState(EventResult.EventQCResultState.AMBIGUOUS);
                Interval range = null;
                if (parseDate1.isBefore(parseDate2)) {
                    result.setResult(resultDateMD + "/" + resultDateDM);
                } else {
                    result.setResult(resultDateDM + "/" + resultDateMD);
                }
            }
        }
    }
    if (result.getResultState().equals(EventResult.EventQCResultState.NOT_RUN)
            && verbatimEventDate.matches("^([0-9]{1,2}|[A-Za-z]+)[-/.]([0-9]{1,2}|[A-Za-z]+)[-/. ][0-9]{4}$")) {
        // Example: 03/Jan/1982
        // Example: Jan-03-1982
        try {
            DateTimeParser[] parsers = { DateTimeFormat.forPattern("MMM/dd/yyyy").getParser(),
                    DateTimeFormat.forPattern("dd/MMM/yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM/dd yyyy").getParser(),
                    DateTimeFormat.forPattern("dd/MMM yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM-dd-yyyy").getParser(),
                    DateTimeFormat.forPattern("dd-MMM-yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM-dd yyyy").getParser(),
                    DateTimeFormat.forPattern("dd-MMM yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM.dd.yyyy").getParser(),
                    DateTimeFormat.forPattern("dd.MMM.yyyy").getParser(),
                    DateTimeFormat.forPattern("MM.dd.yyyy").getParser(),
                    DateTimeFormat.forPattern("dd.MM.yyyy").getParser() };
            DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers).toFormatter();

            DateMidnight parseDate = LocalDate.parse(verbatimEventDate, formatter.withLocale(Locale.ENGLISH))
                    .toDateMidnight();
            resultDate = parseDate.toString("yyyy-MM-dd");
            result.setResultState(EventResult.EventQCResultState.DATE);
            result.setResult(resultDate);
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
    }
    if (result.getResultState().equals(EventResult.EventQCResultState.NOT_RUN)
            && verbatimEventDate.matches("^[X*]{2}[-/. ]([0-9]{1,2}|[A-Za-z]+)[-/. ][0-9]{4}$")) {
        // Example: XX-04-1982   (XX for day)
        // Example: XX-Jan-1995
        try {
            DateTimeParser[] parsers = { DateTimeFormat.forPattern("MMM/yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM-yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM.yyyy").getParser(),
                    DateTimeFormat.forPattern("MM.yyyy").getParser() };
            DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers).toFormatter();

            DateMidnight parseDate = LocalDate
                    .parse(verbatimEventDate.substring(3), formatter.withLocale(Locale.ENGLISH))
                    .toDateMidnight();
            resultDate = parseDate.toString("yyyy-MM");
            result.setResultState(EventResult.EventQCResultState.RANGE);
            result.setResult(resultDate);
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
    }
    if (result.getResultState().equals(EventResult.EventQCResultState.NOT_RUN)
            && verbatimEventDate.matches("^[X*]{2}[-/. ][X*]{2,3}[-/. ][0-9]{4}$")) {
        // Example: XX-XXX-1995
        // Example: **-**-1995
        try {
            DateTimeParser[] parsers = { DateTimeFormat.forPattern("yyyy").getParser(), };
            DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers).toFormatter();
            String yearBit = verbatimEventDate.substring(verbatimEventDate.length() - 4);
            DateMidnight parseDate = LocalDate.parse(yearBit, formatter.withLocale(Locale.ENGLISH))
                    .toDateMidnight();
            resultDate = parseDate.toString("yyyy");
            result.setResultState(EventResult.EventQCResultState.RANGE);
            result.setResult(resultDate);
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
    }
    if (verbatimEventDate.matches("^[0-9]{4}[-][0-9]{3}$")) {
        // Example: 1994-128  (three digits after year = day of year).
        if (result.getResultState().equals(EventResult.EventQCResultState.NOT_RUN)) {
            try {
                DateTimeParser[] parsers = { DateTimeFormat.forPattern("yyyy-D").getParser() };
                DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers)
                        .toFormatter();
                LocalDate parseDate = LocalDate.parse(verbatimEventDate, formatter);
                resultDate = parseDate.toString("yyyy-MM-dd");
                logger.debug(resultDate);
                result.setResultState(EventResult.EventQCResultState.DATE);
                result.setResult(resultDate);
            } catch (Exception e) {
                logger.debug(e.getMessage());
            }

        }
    }

    if (result.getResultState().equals(EventResult.EventQCResultState.NOT_RUN)) {
        try {
            // Example: 1983-15  (two digits after year may fall into subsequent blocks).
            // Example: 1933-Mar
            DateTimeParser[] parsers = { DateTimeFormat.forPattern("yyyy/M").getParser(),
                    DateTimeFormat.forPattern("yyyy-M").getParser(),
                    DateTimeFormat.forPattern("yyyy-MMM").getParser(),
                    DateTimeFormat.forPattern("yyyy.MMM").getParser(),
                    DateTimeFormat.forPattern("yyyy.MMM.").getParser(),
                    DateTimeFormat.forPattern("yyyy MMM.").getParser(),
                    DateTimeFormat.forPattern("yyyy MMM").getParser(),
                    DateTimeFormat.forPattern("yyyy. MMM.").getParser(),
                    DateTimeFormat.forPattern("yyyy. MMM").getParser(),
                    DateTimeFormat.forPattern("yyyy/MMM").getParser() };
            DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers).toFormatter();
            String cleaned = cleanMonth(verbatimEventDate);
            LocalDate parseDate = LocalDate.parse(cleaned, formatter.withLocale(Locale.ENGLISH));
            resultDate = parseDate.toString("yyyy-MM");
            // resultDate =  parseDate.dayOfMonth().withMinimumValue() + "/" + parseDate.dayOfMonth().withMaximumValue();
            logger.debug(resultDate);
            if (verbatimEventDate.matches("^[0-9]{4}[-][0-9]{2}$")) {
                String century = verbatimEventDate.substring(0, 2);
                String startBit = verbatimEventDate.substring(0, 4);
                String endBit = verbatimEventDate.substring(5, 7);
                // 1815-16  won't parse here, passes to next block
                // 1805-06  could be month or abbreviated year
                // 1805-03  should to be month
                if (Integer.parseInt(startBit) >= Integer.parseInt(century + endBit)) {
                    result.setResultState(EventResult.EventQCResultState.RANGE);
                    result.setResult(resultDate);
                } else {
                    result.setResultState(EventResult.EventQCResultState.SUSPECT);
                    result.setResult(resultDate);
                }
            } else {
                result.setResultState(EventResult.EventQCResultState.RANGE);
                result.setResult(resultDate);
            }
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
    }
    if (result.getResultState().equals(EventResult.EventQCResultState.NOT_RUN)
            && verbatimEventDate.matches("^[0-9]{4}[-][0-9]{2}$")) {
        // Example: 1884-85   (two digits look like year later in century).
        try {
            String century = verbatimEventDate.substring(0, 2);
            String startBit = verbatimEventDate.substring(0, 4);
            String endBit = verbatimEventDate.substring(5, 7);
            String assembly = startBit + "/" + century + endBit;
            logger.debug(assembly);
            Interval parseDate = Interval.parse(assembly);
            logger.debug(parseDate);
            resultDate = parseDate.getStart().toString("yyyy") + "/" + parseDate.getEnd().toString("yyyy");
            result.setResultState(EventResult.EventQCResultState.RANGE);
            result.setResult(resultDate);
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
    }
    if (result.getResultState().equals(EventResult.EventQCResultState.NOT_RUN)
            && verbatimEventDate.matches("^[0-9]{4}[0-9]{2}[0-9]{2}$") && !verbatimEventDate.endsWith("0000")) {
        // Example: 19950315
        try {
            DateTimeParser[] parsers = { DateTimeFormat.forPattern("yyyyMMdd").getParser() };
            DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers).toFormatter();
            DateMidnight parseDate = LocalDate.parse(verbatimEventDate, formatter.withLocale(Locale.ENGLISH))
                    .toDateMidnight();
            resultDate = parseDate.toString("yyyy-MM-dd");
            result.setResultState(EventResult.EventQCResultState.DATE);
            result.setResult(resultDate);
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
    }
    if (result.getResultState().equals(EventResult.EventQCResultState.NOT_RUN)) {
        // Example: 1845 
        try {
            DateTimeParser[] parsers = { DateTimeFormat.forPattern("yyyy").getParser() };
            DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers).toFormatter();
            LocalDate parseDate = LocalDate.parse(verbatimEventDate, formatter);
            resultDate = parseDate.dayOfYear().withMinimumValue() + "/"
                    + parseDate.dayOfYear().withMaximumValue();
            logger.debug(resultDate);
            result.setResultState(EventResult.EventQCResultState.RANGE);
            result.setResult(resultDate);
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
    }
    if (result.getResultState().equals(EventResult.EventQCResultState.NOT_RUN)) {
        // Multiple yyyy-mmm-ddd, mmm-dd-yyyy, dd-mmm-yyyy patterns.
        try {
            DateTimeParser[] parsers = { DateTimeFormat.forPattern("yyyy MMM dd").getParser(),
                    DateTimeFormat.forPattern("yyyy MMM. dd").getParser(),
                    DateTimeFormat.forPattern("yyyy, MMM dd").getParser(),
                    DateTimeFormat.forPattern("yyyy, MMM. dd").getParser(),
                    DateTimeFormat.forPattern("yyyy.MMM.dd").getParser(),
                    DateTimeFormat.forPattern("yyyy.MMM.dd.").getParser(),
                    DateTimeFormat.forPattern("yyyy. MMM. dd").getParser(),
                    DateTimeFormat.forPattern("yyyy. MMM. dd.").getParser(),
                    DateTimeFormat.forPattern("yyyy. MMM dd.").getParser(),
                    DateTimeFormat.forPattern("yyyy. MMM dd").getParser(),
                    DateTimeFormat.forPattern("yyyy MMM. dd.").getParser(),
                    DateTimeFormat.forPattern("yyyy: MMM. dd.").getParser(),
                    DateTimeFormat.forPattern("yyyy: MMM. dd").getParser(),
                    DateTimeFormat.forPattern("yyyy: MMM dd").getParser(),
                    DateTimeFormat.forPattern("yyyy:MMM dd").getParser(),
                    DateTimeFormat.forPattern("yyyy:MMM. dd").getParser(),
                    DateTimeFormat.forPattern("yyyy:MMM.dd").getParser(),

                    DateTimeFormat.forPattern("yyyy MMM dd'st'").getParser(),
                    DateTimeFormat.forPattern("yyyy MMM. dd'st'").getParser(),
                    DateTimeFormat.forPattern("yyyy MMM dd'nd'").getParser(),
                    DateTimeFormat.forPattern("yyyy MMM. dd'nd'").getParser(),
                    DateTimeFormat.forPattern("yyyy MMM dd'rd'").getParser(),
                    DateTimeFormat.forPattern("yyyy MMM. dd'rd'").getParser(),
                    DateTimeFormat.forPattern("yyyy MMM dd'th'").getParser(),
                    DateTimeFormat.forPattern("yyyy MMM. dd'th'").getParser(),

                    DateTimeFormat.forPattern("MMM dd, yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM dd., yyyy").getParser(),
                    DateTimeFormat.forPattern("MMMdd, yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM dd'st', yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM dd'nd', yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM dd'rd', yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM dd'd', yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM dd'th', yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd, yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM.dd, yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd'st', yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd'nd', yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd'rd', yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd'th', yyyy").getParser(),

                    DateTimeFormat.forPattern("MMM.dd,yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM.dd'st',yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM.dd'nd',yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM.dd'rd',yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM.dd'd',yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM.dd'th',yyyy").getParser(),

                    DateTimeFormat.forPattern("MMM.dd.yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM.dd'st'.yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM.dd'nd'.yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM.dd'rd'.yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM.dd'd'.yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM.dd'th'.yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd'st'. yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd'nd'. yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd'rd'. yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd'd'. yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd'th'. yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM dd'st'. yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM dd'nd'. yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM dd'rd'. yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM dd'd'. yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM dd'th'. yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM dd'st'.yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM dd'nd'.yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM dd'rd'.yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM dd'd'.yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM dd'th'.yyyy").getParser(),

                    DateTimeFormat.forPattern("MMM-dd-yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM-dd yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM-dd, yyyy").getParser(),
                    DateTimeFormat.forPattern("dd-MMM-yyyy").getParser(),
                    DateTimeFormat.forPattern("dd.MMM.yyyy").getParser(),
                    DateTimeFormat.forPattern("dd,MMM,yyyy").getParser(),
                    DateTimeFormat.forPattern("dd.MMM.,yyyy").getParser(),
                    DateTimeFormat.forPattern("dd. MMM.,yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd. yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM, dd yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM, dd. yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM, dd, yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM, dd., yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd, yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd/yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM dd,yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM dd, yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd,yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd-yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM.dd-yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd, yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd., yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM., dd, yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM.,dd, yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd'' yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM dd'' yyyy").getParser(),
                    DateTimeFormat.forPattern("dd. MMM. yyyy").getParser(),
                    DateTimeFormat.forPattern("dd. MMM.yyyy").getParser(),
                    DateTimeFormat.forPattern("dd MMM., yyyy").getParser(),
                    DateTimeFormat.forPattern("dd MMM.,yyyy").getParser(),
                    DateTimeFormat.forPattern("dd MMM,.yyyy").getParser(),
                    DateTimeFormat.forPattern("dd MMM,. yyyy").getParser(),
                    DateTimeFormat.forPattern("dd MMM..yyyy").getParser(),

                    DateTimeFormat.forPattern("dd MMM, yyyy").getParser(),
                    DateTimeFormat.forPattern("dd MMM yyyy").getParser(),
                    DateTimeFormat.forPattern("dd MMM,yyyy").getParser(),
                    DateTimeFormat.forPattern("dd MMM.yyyy").getParser(),
                    DateTimeFormat.forPattern("ddMMM.yyyy").getParser(),
                    DateTimeFormat.forPattern("ddMMM. yyyy").getParser(),
                    DateTimeFormat.forPattern("dd.MMM-yyyy").getParser(),
                    DateTimeFormat.forPattern("dd-MMM-yyyy").getParser(),
                    DateTimeFormat.forPattern("dd.MMM yyyy").getParser(),
                    DateTimeFormat.forPattern("dd. MMM yyyy").getParser(),
                    DateTimeFormat.forPattern("dd, MMM, yyyy").getParser(),
                    DateTimeFormat.forPattern("dd, MMM; yyyy").getParser(),
                    DateTimeFormat.forPattern("dd. MMM; yyyy").getParser(),
                    DateTimeFormat.forPattern("dd MMM-yyyy").getParser(),
                    DateTimeFormat.forPattern("dd-MMM yyyy").getParser(),
                    DateTimeFormat.forPattern("ddMMMyyyy").getParser(),

                    DateTimeFormat.forPattern("MMM dd yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM dd/yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM dd'st' yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM dd'nd' yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM dd'rd' yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM dd'd' yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM dd'th' yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd'st' yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd'nd' yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd'rd' yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd'd' yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd'th' yyyy").getParser(),
                    DateTimeFormat.forPattern("MMMdd yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM.dd yyyy").getParser(),

                    DateTimeFormat.forPattern("dd MMM, yyyy").getParser(),
                    DateTimeFormat.forPattern("dd'st' MMM, yyyy").getParser(),
                    DateTimeFormat.forPattern("dd'nd' MMM, yyyy").getParser(),
                    DateTimeFormat.forPattern("dd'rd' MMM, yyyy").getParser(),
                    DateTimeFormat.forPattern("dd'd' MMM, yyyy").getParser(),
                    DateTimeFormat.forPattern("dd'th MMM', yyyy").getParser(),
                    DateTimeFormat.forPattern("dd MMM., yyyy").getParser(),
                    DateTimeFormat.forPattern("dd'st' MMM., yyyy").getParser(),
                    DateTimeFormat.forPattern("dd'nd' MMM., yyyy").getParser(),
                    DateTimeFormat.forPattern("dd'rd' MMM., yyyy").getParser(),
                    DateTimeFormat.forPattern("dd'th' MMM., yyyy").getParser(),

                    DateTimeFormat.forPattern("dd MMM yyyy").getParser(),
                    DateTimeFormat.forPattern("dd'st' MMM yyyy").getParser(),
                    DateTimeFormat.forPattern("dd'nd' MMM yyyy").getParser(),
                    DateTimeFormat.forPattern("dd'rd' MMM yyyy").getParser(),
                    DateTimeFormat.forPattern("dd'd' MMM yyyy").getParser(),
                    DateTimeFormat.forPattern("dd'th' MMM yyyy").getParser(),
                    DateTimeFormat.forPattern("dd MMM. yyyy").getParser(),
                    DateTimeFormat.forPattern("dd'st' MMM. yyyy").getParser(),
                    DateTimeFormat.forPattern("dd'nd' MMM. yyyy").getParser(),
                    DateTimeFormat.forPattern("dd'rd' MMM. yyyy").getParser(),
                    DateTimeFormat.forPattern("dd'd' MMM. yyyy").getParser(),
                    DateTimeFormat.forPattern("dd'th' MMM. yyyy").getParser(),
                    DateTimeFormat.forPattern("dd'st' MMM, yyyy").getParser(),
                    DateTimeFormat.forPattern("dd'nd' MMM, yyyy").getParser(),
                    DateTimeFormat.forPattern("dd'rd' MMM, yyyy").getParser(),
                    DateTimeFormat.forPattern("dd'd' MMM, yyyy").getParser(),
                    DateTimeFormat.forPattern("dd'th' MMM, yyyy").getParser(),

                    DateTimeFormat.forPattern("dd/MMM/yyyy").getParser(),
                    DateTimeFormat.forPattern("dd/MMM yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM/dd yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM/dd/yyyy").getParser(),

                    DateTimeFormat.forPattern("MMM dd. yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM dd'st'. yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM dd'nd'. yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM dd'rd'. yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM dd'th'. yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd. yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd'st'. yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd'nd'. yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd'rd'. yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd'th'. yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM dd.yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd.yyyy").getParser(),

                    DateTimeFormat.forPattern("MMM. dd-yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd'st'-yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd'nd'-yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd'rd'-yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM. dd'th'-yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM dd-yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM dd'st'-yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM dd'nd'-yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM dd'rd'-yyyy").getParser(),
                    DateTimeFormat.forPattern("MMM dd'th'-yyyy").getParser(),

                    DateTimeFormat.forPattern("yyyy-MMM-dd").getParser() };
            DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers).toFormatter();
            String cleaned = cleanMonth(verbatimEventDate);
            cleaned = cleaned.replace("''", "'");
            try {
                // Specify English locale, or local default will be used
                LocalDate parseDate = LocalDate.parse(cleaned, formatter.withLocale(Locale.ENGLISH));
                resultDate = parseDate.toString("yyyy-MM-dd");
            } catch (Exception e) {
                try {
                    logger.debug(e.getMessage());
                    LocalDate parseDate = LocalDate.parse(cleaned, formatter.withLocale(Locale.FRENCH));
                    resultDate = parseDate.toString("yyyy-MM-dd");
                } catch (Exception e1) {
                    try {
                        logger.debug(e1.getMessage());
                        LocalDate parseDate = LocalDate.parse(cleaned, formatter.withLocale(Locale.ITALIAN));
                        resultDate = parseDate.toString("yyyy-MM-dd");
                    } catch (Exception e2) {
                        try {
                            logger.debug(e2.getMessage());
                            LocalDate parseDate = LocalDate.parse(cleaned, formatter.withLocale(Locale.GERMAN));
                            resultDate = parseDate.toString("yyyy-MM-dd");
                        } catch (Exception e3) {
                            try {
                                logger.debug(e2.getMessage());
                                LocalDate parseDate = LocalDate.parse(cleaned,
                                        formatter.withLocale(Locale.forLanguageTag("es")));
                                resultDate = parseDate.toString("yyyy-MM-dd");
                            } catch (Exception e4) {
                                logger.debug(e2.getMessage());
                                LocalDate parseDate = LocalDate.parse(cleaned,
                                        formatter.withLocale(Locale.forLanguageTag("pt")));
                                resultDate = parseDate.toString("yyyy-MM-dd");
                            }
                        }
                    }
                }
            }
            logger.debug(resultDate);
            result.setResultState(EventResult.EventQCResultState.DATE);
            result.setResult(resultDate);
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
    }
    logger.debug(result.getResultState());
    if (result.getResultState().equals(EventResult.EventQCResultState.NOT_RUN)) {
        // Example: jan.-1992
        // Example: January 1992
        if (verbatimEventDate.matches(".*[0-9]{4}.*")) {
            try {
                DateTimeParser[] parsers = { DateTimeFormat.forPattern("MMM, yyyy").getParser(),
                        DateTimeFormat.forPattern("MMM., yyyy").getParser(),
                        DateTimeFormat.forPattern("MMM.,yyyy").getParser(),
                        DateTimeFormat.forPattern("MMM.-yyyy").getParser(),
                        DateTimeFormat.forPattern("MMM.yyyy").getParser(),
                        DateTimeFormat.forPattern("MMM. yyyy").getParser(),
                        DateTimeFormat.forPattern("MMM-yyyy").getParser(),
                        DateTimeFormat.forPattern("MMM -yyyy").getParser(),
                        DateTimeFormat.forPattern("MMM yyyy").getParser(),
                        DateTimeFormat.forPattern("MMM/yyyy").getParser() };
                DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers)
                        .toFormatter();
                String cleaned = cleanMonth(verbatimEventDate);
                // Strip off a trailing period after a final year
                if (cleaned.matches("^.*[0-9]{4}[.]$")) {
                    cleaned = cleaned.replaceAll("[.]$", "");
                }
                LocalDate parseDate = LocalDate.parse(cleaned, formatter.withLocale(Locale.ENGLISH));
                resultDate = parseDate.toString("yyyy-MM");
                logger.debug(resultDate);
                result.setResultState(EventResult.EventQCResultState.RANGE);
                result.setResult(resultDate);
            } catch (Exception e) {
                logger.debug(e.getMessage());
            }
        }
    }
    if (result.getResultState().equals(EventResult.EventQCResultState.NOT_RUN)
            && verbatimEventDate.matches("^[0-9]{4}([- ]+| to |[/ ]+)[0-9]{4}$")) {
        // Example:  1882-1995
        // Example:  1882 to 1885
        // Example:  1882/1885
        try {
            String cleaned = verbatimEventDate.replace(" ", "");
            cleaned = cleaned.replace("-", "/");
            if (cleaned.matches("^[0-9]{4}to[0-9]{4}$")) {
                int len = verbatimEventDate.length();
                int lastYear = len - 4;
                cleaned = verbatimEventDate.substring(0, 4) + "/" + verbatimEventDate.substring(lastYear, len);
            }
            logger.debug(cleaned);
            Interval parseDate = Interval.parse(cleaned);
            logger.debug(parseDate);
            resultDate = parseDate.getStart().toString("yyyy") + "/" + parseDate.getEnd().toString("yyyy");
            result.setResultState(EventResult.EventQCResultState.RANGE);
            result.setResult(resultDate);
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
    }
    if (result.getResultState().equals(EventResult.EventQCResultState.NOT_RUN) && verbatimEventDate
            .matches("^[A-Za-z]+[.]{0,1}( and | to |[-][ ]{0,1}| [-] )[A-Za-z]+[.]{0,1}(, |[/ .])[0-9]{4}$")) {
        logger.debug(verbatimEventDate);
        // Example: Jan to Feb 1882
        // Example: Jan-Feb/1882
        verbatimEventDate = verbatimEventDate.replace(", ", " ");
        if (verbatimEventDate.matches("^[A-Za-z]+[.]{0,1}[-][A-Za-z]+[.]{0,1}[.][0-9]{4}$")) {
            // transform case with multiple periods to slash before year.
            verbatimEventDate = verbatimEventDate.substring(0, verbatimEventDate.length() - 5) + "/"
                    + verbatimEventDate.substring(verbatimEventDate.length() - 4);
            logger.debug(verbatimEventDate);
        }
        if (verbatimEventDate.matches("^[A-Za-z]+[.]{0,1}[ ][-][ ]{1}[A-Za-z]+[.]{0,1}[/ .][0-9]{4}$")) {
            // remove space around dash.
            verbatimEventDate = verbatimEventDate.replace(" - ", "-");
            logger.debug(verbatimEventDate);
        }
        if (verbatimEventDate.matches("^[A-Za-z]+[.]{0,1}[-][ ]{1}[A-Za-z]+[.]{0,1}[/ .][0-9]{4}$")) {
            // remove space trailing after dash.
            verbatimEventDate = verbatimEventDate.replace("- ", "-");
            logger.debug(verbatimEventDate);
        }
        if (verbatimEventDate.matches("^[A-Za-z]+[.]{0,1} and {1}[A-Za-z]+[.]{0,1}[/ .][0-9]{4}$")) {
            // replace and with dash
            verbatimEventDate = verbatimEventDate.replace(" and ", "-");
            logger.debug(verbatimEventDate);
        }
        if (verbatimEventDate.matches("^[A-Za-z]+[.]{0,1} to {1}[A-Za-z]+[.]{0,1}[/ .][0-9]{4}$")) {
            // replace to with dash
            verbatimEventDate = verbatimEventDate.replace(" to ", "-");
            logger.debug(verbatimEventDate);
        }
        try {
            String[] bits = verbatimEventDate.replace(" ", "/").split("-");
            if (bits != null && bits.length == 2) {
                String year = verbatimEventDate.substring(verbatimEventDate.length() - 4,
                        verbatimEventDate.length());
                String startBit = bits[0] + "/" + year;
                DateTimeParser[] parsers = { DateTimeFormat.forPattern("MMM/yyyy").getParser(),
                        DateTimeFormat.forPattern("MMM./yyyy").getParser() };
                DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers)
                        .toFormatter();
                LocalDate parseStartDate = LocalDate.parse(cleanMonth(startBit),
                        formatter.withLocale(Locale.ENGLISH));
                LocalDate parseEndDate = LocalDate.parse(cleanMonth(bits[1]),
                        formatter.withLocale(Locale.ENGLISH));
                resultDate = parseStartDate.toString("yyyy-MM") + "/" + parseEndDate.toString("yyyy-MM");
                logger.debug(resultDate);
                result.setResultState(EventResult.EventQCResultState.RANGE);
                result.setResult(resultDate);
            }
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
    }
    if (result.getResultState().equals(EventResult.EventQCResultState.NOT_RUN) && verbatimEventDate.matches(
            "^[0-9]{1,2}[ /.]{0,1}[A-Za-z]+[.]{0,1}( - |[-])[0-9]{1,2}[ /.]{0,1}[A-Za-z]+[.]{0,1}[/ -.][0-9]{4}$")) {
        logger.debug(verbatimEventDate);
        // Example: 05/Jan/1882-03/Feb/1885
        if (verbatimEventDate.matches(
                "^[0-9]{1,2}[ /.]{0,1}[A-Za-z]+[.]{0,1}[-][0-9]{1,2}[ /.]{0,1}[A-Za-z]+[.]{0,1}[-][0-9]{4}$")) {
            // transform case with multiple dashes to slash before year.
            verbatimEventDate = verbatimEventDate.substring(0, verbatimEventDate.length() - 5) + "/"
                    + verbatimEventDate.substring(verbatimEventDate.length() - 4);
            logger.debug(verbatimEventDate);
        }
        if (verbatimEventDate.matches(
                "^[0-9]{1,2}[ /.]{0,1}[A-Za-z]+[.]{0,1}[-][0-9]{1,2}[ /.]{0,1}[A-Za-z]+[.]{0,1}[.][0-9]{4}$")) {
            // transform case with multiple periods to slash before year.
            verbatimEventDate = verbatimEventDate.substring(0, verbatimEventDate.length() - 5) + "/"
                    + verbatimEventDate.substring(verbatimEventDate.length() - 4);
            logger.debug(verbatimEventDate);
        }
        try {
            String[] bits = verbatimEventDate.replace(" - ", "-").replace(" ", "/").split("-");
            if (bits != null && bits.length == 2) {
                String year = verbatimEventDate.substring(verbatimEventDate.length() - 4,
                        verbatimEventDate.length());
                String startBit = bits[0] + "/" + year;
                logger.debug(cleanMonth(startBit));
                logger.debug(cleanMonth(bits[1]));
                DateTimeParser[] parsers = { DateTimeFormat.forPattern("dd MMM/yyyy").getParser(),
                        DateTimeFormat.forPattern("dd.MMM/yyyy").getParser(),
                        DateTimeFormat.forPattern("dd/MMM/yyyy").getParser(),
                        DateTimeFormat.forPattern("ddMMM/yyyy").getParser(),
                        DateTimeFormat.forPattern("dd MMM./yyyy").getParser(),
                        DateTimeFormat.forPattern("dd.MMM./yyyy").getParser(),
                        DateTimeFormat.forPattern("dd/MMM./yyyy").getParser(),
                        DateTimeFormat.forPattern("ddMMM./yyyy").getParser() };
                DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers)
                        .toFormatter();
                LocalDate parseStartDate = LocalDate.parse(cleanMonth(startBit),
                        formatter.withLocale(Locale.ENGLISH));
                LocalDate parseEndDate = LocalDate.parse(cleanMonth(bits[1]),
                        formatter.withLocale(Locale.ENGLISH));
                resultDate = parseStartDate.toString("yyyy-MM-dd") + "/" + parseEndDate.toString("yyyy-MM-dd");
                logger.debug(resultDate);
                result.setResultState(EventResult.EventQCResultState.RANGE);
                result.setResult(resultDate);
            }
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
    }
    if (result.getResultState().equals(EventResult.EventQCResultState.NOT_RUN) && verbatimEventDate.matches(
            "^[A-Za-z]+[.]{0,1}[ ]{0,1}[0-9]{1,2}( - |[-]| to | and | et )[A-Za-z]+[.]{0,1}[ ]{0,1}[0-9]{1,2}[/ .,][ ]{0,1}[0-9]{4}$")) {
        logger.debug(verbatimEventDate);
        // Example: Aug. 5 - Sept. 8, 1943
        try {
            String[] bits = verbatimEventDate.replace(" to ", "-").replace(" - ", "-").replace(" and ", "-")
                    .replace(" et ", "-").replace(", ", " ").replace(" ", "/").split("-");
            if (bits != null && bits.length == 2) {
                String year = verbatimEventDate.substring(verbatimEventDate.length() - 4,
                        verbatimEventDate.length());
                String startBit = bits[0] + "/" + year;
                logger.debug(cleanMonth(startBit));
                logger.debug(cleanMonth(bits[1]));
                DateTimeParser[] parsers = { DateTimeFormat.forPattern("MMM/dd/yyyy").getParser(),
                        DateTimeFormat.forPattern("MMM./dd/yyyy").getParser(),
                        DateTimeFormat.forPattern("MMM.dd/yyyy").getParser() };
                DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers)
                        .toFormatter();
                LocalDate parseStartDate = LocalDate.parse(cleanMonth(startBit),
                        formatter.withLocale(Locale.ENGLISH));
                LocalDate parseEndDate = LocalDate.parse(cleanMonth(bits[1]),
                        formatter.withLocale(Locale.ENGLISH));
                resultDate = parseStartDate.toString("yyyy-MM-dd") + "/" + parseEndDate.toString("yyyy-MM-dd");
                logger.debug(resultDate);
                result.setResultState(EventResult.EventQCResultState.RANGE);
                result.setResult(resultDate);
            }
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
    }
    if (result.getResultState().equals(EventResult.EventQCResultState.NOT_RUN) && verbatimEventDate.matches(
            "^[0-9]{1,2}([ ]{0,1}[-][ ]{0,1}| and | et | to )[0-9]{1,2}[ /.]{0,1}[A-Za-z]+[.]{0,1}[/ -.][0-9]{4}$")) {
        // Example: 11 et 14 VII 1910
        // Example: 05-02 Jan./1992
        String toCheck = verbatimEventDate;
        toCheck = toCheck.replace(" - ", "-").replace(" et ", "-").replace(" and ", "-").replace(" to ", "-");
        // Note: "and" has different semantics than "to", may imply that a specimen record
        // represents two occurrences (e.g. flower on one date, fruit on another) rather than
        // a range, but dwc:eventDate representation for both forms on one event is a range.
        if (toCheck.matches("^[0-9]{1,2}[-][0-9]{1,2}[ /.]{0,1}[A-Za-z]+[.]{0,1}[-][0-9]{4}$")) {
            // transform case with multiple dashes to slash before year.
            toCheck = toCheck.substring(0, toCheck.length() - 5) + "/"
                    + toCheck.substring(toCheck.length() - 4);
            logger.debug(toCheck);
        }
        if (toCheck.matches("^[0-9]{1,2}[-][0-9]{1,2}[ /.]{0,1}[A-Za-z]+[.]{0,1}[.][0-9]{4}$")) {
            // transform case with multiple periods to slash before year.
            toCheck = toCheck.substring(0, toCheck.length() - 5) + "/"
                    + toCheck.substring(toCheck.length() - 4);
            logger.debug(toCheck);
        }
        try {
            String[] bits = toCheck.replace(" ", "/").split("-");
            if (bits != null && bits.length == 2) {
                String year = toCheck.substring(toCheck.length() - 4, toCheck.length());
                logger.debug(cleanMonth(bits[1]));
                DateTimeParser[] parsers = { DateTimeFormat.forPattern("dd MMM/yyyy").getParser(),
                        DateTimeFormat.forPattern("dd.MMM/yyyy").getParser(),
                        DateTimeFormat.forPattern("dd/MMM/yyyy").getParser(),
                        DateTimeFormat.forPattern("ddMMM/yyyy").getParser(),
                        DateTimeFormat.forPattern("dd MMM./yyyy").getParser(),
                        DateTimeFormat.forPattern("dd.MMM./yyyy").getParser(),
                        DateTimeFormat.forPattern("dd/MMM./yyyy").getParser(),
                        DateTimeFormat.forPattern("ddMMM./yyyy").getParser() };
                DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers)
                        .toFormatter();
                LocalDate parseEndDate = LocalDate.parse(cleanMonth(bits[1]),
                        formatter.withLocale(Locale.ENGLISH));
                String startMonthYear = parseEndDate.toString("MMM/yyyy");
                String startBit = bits[0] + "/" + startMonthYear;
                logger.debug(startBit);
                LocalDate parseStartDate = LocalDate.parse(startBit, formatter.withLocale(Locale.ENGLISH));
                resultDate = parseStartDate.toString("yyyy-MM-dd") + "/" + parseEndDate.toString("yyyy-MM-dd");
                logger.debug(resultDate);
                result.setResultState(EventResult.EventQCResultState.RANGE);
                result.setResult(resultDate);
            }
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
    }
    if (result.getResultState().equals(EventResult.EventQCResultState.NOT_RUN)
            && verbatimEventDate.matches("^[0-9]{2}[-. ]XXX[-. ][0-9]{4}$")) {
        // Example: 05-XXX-1884
        try {
            String start = verbatimEventDate.substring(verbatimEventDate.length() - 4) + "-01-"
                    + verbatimEventDate.substring(0, 2);
            String end = verbatimEventDate.substring(verbatimEventDate.length() - 4) + "-12-"
                    + verbatimEventDate.substring(0, 2);
            EventResult compositeResult = DateUtils.extractDateFromVerbatimER(start + "/" + end,
                    yearsBeforeSuspect, assumemmddyyyy);
            logger.debug(compositeResult.getResultState());
            if (compositeResult.getResultState().equals(EventResult.EventQCResultState.RANGE)) {
                result.setResultState(EventResult.EventQCResultState.RANGE);
                result.setResult(compositeResult.getResult());
                logger.debug(result.getResult());
            }
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
    }
    if (result.getResultState().equals(EventResult.EventQCResultState.NOT_RUN)
            && verbatimEventDate.matches("^[0-9]{4}-[0-9]{2}/[0-9]{4}-[0-9]{2}$")) {
        // Example: 1885-03/1886-04
        try {
            Interval parseDate = Interval.parse(verbatimEventDate);
            logger.debug(parseDate);
            resultDate = parseDate.getStart().toString("yyyy-MM") + "/"
                    + parseDate.getEnd().toString("yyyy-MM");
            result.setResultState(EventResult.EventQCResultState.RANGE);
            result.setResult(resultDate);
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
    }
    if (result.getResultState().equals(EventResult.EventQCResultState.NOT_RUN)) {
        // Example: 1995-03-05/1996-05-08
        try {
            Interval parseDate = Interval.parse(verbatimEventDate);
            logger.debug(parseDate);
            resultDate = parseDate.getStart().toString("yyyy-MM-dd") + "/"
                    + parseDate.getEnd().toString("yyyy-MM-dd");
            result.setResultState(EventResult.EventQCResultState.RANGE);
            result.setResult(resultDate);
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
    }
    if (result.getResultState().equals(EventResult.EventQCResultState.NOT_RUN)) {
        // Example: Jan,15-18 1882
        // Example: Jan. 17 and 18 1882
        String cleaned = verbatimEventDate.trim();
        if (verbatimEventDate.matches("^[A-Za-z.]+[ ,]+[0-9]{1,2} and [0-9]{0,2}[ ,]+[0-9]{4}$")) {
            // Note: "and" has different semantics than "to", may imply that a specimen record
            // represents two occurrences (e.g. flower on one date, fruit on another) rather than
            // a range, but dwc:eventDate representation for both forms on one event is a range.
            cleaned = cleaned.replace(" and ", " to ");
        }
        if (verbatimEventDate.matches("^[A-Za-z.]+[ ,]+[0-9]{1,2}-[0-9]{0,2}[ ,]+[0-9]{4}$")) {
            cleaned = cleaned.replace("-", " to ");
        }
        if (cleaned.contains(" to ")) {
            String[] bits = cleaned.split(" to ");
            String yearRegex = ".*([0-9]{4}).*";
            Matcher yearMatcher = Pattern.compile(yearRegex).matcher(cleaned);
            String monthRegex = "([A-Za-z.]+).*";
            Matcher monthMatcher = Pattern.compile(monthRegex).matcher(cleaned);
            if (yearMatcher.matches() && monthMatcher.matches()) {
                String year = yearMatcher.group(1);
                String month = monthMatcher.group(1);
                if (bits.length == 2) {
                    if (!bits[0].contains(year)) {
                        bits[0] = bits[0] + " " + year;
                    }
                    if (!bits[1].contains(year)) {
                        bits[1] = bits[1] + " " + year;
                    }
                    if (!bits[1].contains(month)) {
                        bits[1] = month + " " + bits[1];
                    }
                    Map<String, String> resultBit0 = DateUtils.extractDateFromVerbatim(bits[0]);
                    if (resultBit0.size() > 0 && resultBit0.get("resultState").equals("date")) {
                        Map<String, String> resultBit1 = DateUtils.extractDateFromVerbatim(bits[1]);
                        if (resultBit1.size() > 0 && resultBit1.get("resultState").equals("date")) {
                            result.setResultState(EventResult.EventQCResultState.RANGE);
                            result.setResult(resultBit0.get("result") + "/" + resultBit1.get("result"));
                        }
                    }
                    logger.debug(bits[0]);
                    logger.debug(bits[1]);
                }
            }
        }
    }

    // Now test to see if result is sane.
    if (result != null && !result.getResultState().equals(EventResult.EventQCResultState.NOT_RUN)) {
        Interval testExtract = DateUtils.extractDateInterval(result.getResult());
        if (testExtract == null || testExtract.getStart().getYear() < yearsBeforeSuspect) {
            result.setResultState(EventResult.EventQCResultState.SUSPECT);
            logger.debug(result.getResult());
            logger.debug(testExtract);
        } else {
            logger.debug(result.getResult());
        }
        if (!verbatimEventDate.matches(".*[0-9]{4}.*") && yearsBeforeSuspect > 999) {
            result = new EventResult();
            logger.debug(result.getResult());
        }
    }

    return result;
}

From source file:org.forgerock.openidm.util.DateUtil.java

License:CDDL license

/**
 * Returns true if the current (now) timestamp is within the specified time interval.  The supplied interval string 
 * should contain an ISO 8601 formatted interval string and may be of the formats 'datetime/datetime', 
 * 'datetime/period' or 'period/datetime'
 * // w  ww. jav  a 2s.  c om
 * @param intervalString a {@link String} object representing an ISO 8601 time interval.
 * @return true if the instant is within the interval, false otherwise.
 * @throws IllegalArgumentException if an error occurs while parsing the intervalString.
 */
public boolean isNowWithinInterval(String intervalString) throws IllegalArgumentException {
    Interval interval = Interval.parse(intervalString);
    return interval.contains(DateTime.now());
}