Example usage for java.text SimpleDateFormat setLenient

List of usage examples for java.text SimpleDateFormat setLenient

Introduction

In this page you can find the example usage for java.text SimpleDateFormat setLenient.

Prototype

public void setLenient(boolean lenient) 

Source Link

Document

Specify whether or not date/time parsing is to be lenient.

Usage

From source file:org.motechproject.server.omod.web.controller.PatientController.java

@InitBinder
public void initBinder(WebDataBinder binder) {
    String datePattern = "dd/MM/yyyy";
    SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern);
    dateFormat.setLenient(false);
    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true, datePattern.length()));

    String timePattern = MotechConstants.TIME_FORMAT_DELIVERY_TIME;
    SimpleDateFormat timeFormat = new SimpleDateFormat(timePattern);
    dateFormat.setLenient(false);//w w w . j  ava  2 s.  com
    binder.registerCustomEditor(Date.class, "timeOfDay",
            new CustomDateEditor(timeFormat, true, timePattern.length()));
    binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
}

From source file:com.archsystemsinc.ipms.sec.webapp.controller.RoleController.java

@Override
@InitBinder/*from  w ww . j a v a 2  s . c  o  m*/
public void initBinder(final WebDataBinder binder) {
    final SimpleDateFormat dateFormat = new SimpleDateFormat(GenericConstants.DEFAULT_DATE_FORMAT);
    dateFormat.setLenient(false);
    // true passed to CustomDateEditor constructor means convert empty
    // String to null
    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));

    /*// Custom Change for roles
    binder.registerCustomEditor(Set.class, "privileges",
    new CustomCollectionEditor(Set.class) {
       @Override
       protected Object convertElement(final Object element) {
    if (element != null) {
       final Long id = new Long((String) element);
       final Privilege privilege = privilegeService
             .findOne(id);
       return privilege;
    }
    return null;
       }
    });*/
}

From source file:org.bonitasoft.connectors.salesforce.partner.SalesforceConnector.java

/**
 * @param closedDate/*  w w  w. jav a 2  s.co  m*/
 * @return
 * @throws ParseException
 */
public Calendar convertString2Calendar(final String closedDate) throws ParseException {
    final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    sdf.setLenient(false);// set false for date strict check
    Date date = null;
    date = sdf.parse(closedDate);
    final Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar;
}

From source file:np.com.drose.studentmanagementsystem.controller.ProjectController.java

@InitBinder
public void initBinder(WebDataBinder binder) {
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    sdf.setLenient(true);
    binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
}

From source file:oscar.oscarEncounter.oscarMeasurements.pageUtil.EctValidation.java

public boolean isDate(String inputValue) {

    boolean validation = true;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    sdf.setLenient(false);

    try {/*from   ww w  .java  2s .c o  m*/
        Date d = sdf.parse(inputValue);
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        if (c.get(Calendar.YEAR) > 9999) { // to prevent date parsing of more than 4-digit year
            validation = false;
        }
    } catch (ParseException e) {
        validation = false;
    }

    return validation;
}

From source file:org.enerj.apache.commons.beanutils.locale.converters.DateLocaleConverter.java

/**
 * Gets an appropriate <code>SimpleDateFormat</code> for given locale, 
 * default Date format pattern is not provided.
 *//*from w w w  .  j  a  va2 s .  co  m*/
private SimpleDateFormat getFormatter(String pattern, Locale locale) {
    // This method is a fix for null pattern, which would cause 
    // Null pointer exception when applied
    // Note: that many constructors default the pattern to null, 
    // so it only makes sense to handle nulls gracefully
    if (pattern == null) {
        pattern = locPattern ? new SimpleDateFormat().toLocalizedPattern() : new SimpleDateFormat().toPattern();
        log.warn("Null pattern was provided, defaulting to: " + pattern);
    }
    SimpleDateFormat format = new SimpleDateFormat(pattern, locale);
    format.setLenient(isLenient);
    return format;
}

From source file:org.motechproject.server.omod.web.controller.DemoPatientController.java

@InitBinder
public void initBinder(WebDataBinder binder) {
    String datePattern = "dd/MM/yyyy";
    SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern);
    dateFormat.setLenient(false);
    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true, datePattern.length()));

    String timePattern = MotechConstants.TIME_FORMAT_DELIVERY_TIME;
    SimpleDateFormat timeFormat = new SimpleDateFormat(timePattern);
    dateFormat.setLenient(false);//from w w  w . j a  v a  2s .c  o  m
    binder.registerCustomEditor(Date.class, "timeOfDay",
            new CustomDateEditor(timeFormat, true, timePattern.length()));

    binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
}

From source file:org.jasig.ssp.web.api.reports.EarlyAlertStudentOutcomeReportController.java

@InitBinder
public void initBinder(final WebDataBinder binder) {
    final SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy", Locale.US);
    dateFormat.setLenient(false);
    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}

From source file:fr.paris.lutece.plugins.crm.modules.mydashboard.web.MyDashboardCRMComponent.java

/**
 * Check the format of the filter modification date
 * //ww  w. ja  v  a 2 s.  com
 */
private Date checkFormatModificationDateFilter(String strModificationDate, HttpServletRequest request)

{
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    sdf.setLenient(true);

    Date d = new Date();

    try {
        d = sdf.parse(strModificationDate);
    } catch (Exception e) {
        AppLogService.error(e);
    }

    return d;
}

From source file:org.apache.jmeter.save.CSVSaveService.java

/**
 * Make a SampleResult given a set of tokens
 * //  ww  w.  jav a  2s . c om
 * @param parts
 *            tokens parsed from the input
 * @param saveConfig
 *            the save configuration (may be updated)
 * @param lineNumber the line number (for error reporting)
 * @return the sample result
 * 
 * @throws JMeterError
 */
private static SampleEvent makeResultFromDelimitedString(final String[] parts,
        final SampleSaveConfiguration saveConfig, // may be updated
        final long lineNumber) {

    SampleResult result = null;
    String hostname = "";// $NON-NLS-1$
    long timeStamp = 0;
    long elapsed = 0;
    String text = null;
    String field = null; // Save the name for error reporting
    int i = 0;
    try {
        if (saveConfig.saveTimestamp()) {
            field = TIME_STAMP;
            text = parts[i++];
            if (saveConfig.printMilliseconds()) {
                try {
                    timeStamp = Long.parseLong(text); // see if this works
                } catch (NumberFormatException e) { // it did not, let's try some other formats
                    log.warn(e.toString());
                    boolean foundMatch = false;
                    for (String fmt : DATE_FORMAT_STRINGS) {
                        SimpleDateFormat dateFormat = new SimpleDateFormat(fmt);
                        dateFormat.setLenient(false);
                        try {
                            Date stamp = dateFormat.parse(text);
                            timeStamp = stamp.getTime();
                            // method is only ever called from one thread at a time
                            // so it's OK to use a static DateFormat
                            log.warn("Setting date format to: " + fmt);
                            saveConfig.setFormatter(dateFormat);
                            foundMatch = true;
                            break;
                        } catch (ParseException e1) {
                            log.info(text + " did not match " + fmt);
                        }
                    }
                    if (!foundMatch) {
                        throw new ParseException("No date-time format found matching " + text, -1);
                    }
                }
            } else if (saveConfig.formatter() != null) {
                Date stamp = saveConfig.formatter().parse(text);
                timeStamp = stamp.getTime();
            } else { // can this happen?
                final String msg = "Unknown timestamp format";
                log.warn(msg);
                throw new JMeterError(msg);
            }
        }

        if (saveConfig.saveTime()) {
            field = CSV_ELAPSED;
            text = parts[i++];
            elapsed = Long.parseLong(text);
        }

        if (saveConfig.saveSampleCount()) {
            result = new StatisticalSampleResult(timeStamp, elapsed);
        } else {
            result = new SampleResult(timeStamp, elapsed);
        }

        if (saveConfig.saveLabel()) {
            field = LABEL;
            text = parts[i++];
            result.setSampleLabel(text);
        }
        if (saveConfig.saveCode()) {
            field = RESPONSE_CODE;
            text = parts[i++];
            result.setResponseCode(text);
        }

        if (saveConfig.saveMessage()) {
            field = RESPONSE_MESSAGE;
            text = parts[i++];
            result.setResponseMessage(text);
        }

        if (saveConfig.saveThreadName()) {
            field = THREAD_NAME;
            text = parts[i++];
            result.setThreadName(text);
        }

        if (saveConfig.saveDataType()) {
            field = DATA_TYPE;
            text = parts[i++];
            result.setDataType(text);
        }

        if (saveConfig.saveSuccess()) {
            field = SUCCESSFUL;
            text = parts[i++];
            result.setSuccessful(Boolean.valueOf(text).booleanValue());
        }

        if (saveConfig.saveAssertionResultsFailureMessage()) {
            i++;
            // TODO - should this be restored?
        }

        if (saveConfig.saveBytes()) {
            field = CSV_BYTES;
            text = parts[i++];
            result.setBytes(Integer.parseInt(text));
        }

        if (saveConfig.saveThreadCounts()) {
            field = CSV_THREAD_COUNT1;
            text = parts[i++];
            result.setGroupThreads(Integer.parseInt(text));

            field = CSV_THREAD_COUNT2;
            text = parts[i++];
            result.setAllThreads(Integer.parseInt(text));
        }

        if (saveConfig.saveUrl()) {
            i++;
            // TODO: should this be restored?
        }

        if (saveConfig.saveFileName()) {
            field = CSV_FILENAME;
            text = parts[i++];
            result.setResultFileName(text);
        }
        if (saveConfig.saveLatency()) {
            field = CSV_LATENCY;
            text = parts[i++];
            result.setLatency(Long.parseLong(text));
        }

        if (saveConfig.saveEncoding()) {
            field = CSV_ENCODING;
            text = parts[i++];
            result.setEncodingAndType(text);
        }

        if (saveConfig.saveSampleCount()) {
            field = CSV_SAMPLE_COUNT;
            text = parts[i++];
            result.setSampleCount(Integer.parseInt(text));
            field = CSV_ERROR_COUNT;
            text = parts[i++];
            result.setErrorCount(Integer.parseInt(text));
        }

        if (saveConfig.saveHostname()) {
            field = CSV_HOSTNAME;
            hostname = parts[i++];
        }

        if (saveConfig.saveIdleTime()) {
            field = CSV_IDLETIME;
            text = parts[i++];
            result.setIdleTime(Long.parseLong(text));
        }
        if (saveConfig.saveConnectTime()) {
            field = CSV_CONNECT_TIME;
            text = parts[i++];
            result.setConnectTime(Long.parseLong(text));
        }

        if (i + saveConfig.getVarCount() < parts.length) {
            log.warn("Line: " + lineNumber + ". Found " + parts.length + " fields, expected " + i
                    + ". Extra fields have been ignored.");
        }

    } catch (NumberFormatException | ParseException e) {
        log.warn("Error parsing field '" + field + "' at line " + lineNumber + " " + e);
        throw new JMeterError(e);
    } catch (ArrayIndexOutOfBoundsException e) {
        log.warn("Insufficient columns to parse field '" + field + "' at line " + lineNumber);
        throw new JMeterError(e);
    }
    return new SampleEvent(result, "", hostname);
}