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:com.alibaba.otter.manager.web.home.module.screen.AnalysisDelayStat.java

public void execute(@Param("d5221") String startTime, @Param("d5222") String endTime,
        @Param("pipelineId") Long pipelineId, HttpSession session, Context context) throws Exception {
    Date end = null;/*w ww  . j a v  a  2 s  .  com*/
    Date start = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    if (StringUtils.isEmpty(startTime) || StringUtils.isEmpty(endTime)) {
        start = new Date(System.currentTimeMillis() / 60000 * 60000 - 24 * 60 * 60 * 1000);
        end = new Date(System.currentTimeMillis() / 60000 * 60000);
    } else {// ?24??
        sdf.setLenient(false);
        if (null != startTime && null != endTime) {
            start = sdf.parse(startTime);
            end = sdf.parse(endTime);
        }
    }

    Channel channel = channelService.findByPipelineId(pipelineId);
    Map<Long, DelayStatInfo> delayStatInfos = new HashMap<Long, DelayStatInfo>();
    if (null != start && null != end) {
        delayStatInfos = delayStatService.listTimelineDelayStat(pipelineId, start, end);
    }

    Double delayAvg = 0.0;
    for (DelayStatInfo info : delayStatInfos.values()) {
        delayAvg += info.getAvgDelayTime();
    }

    if (delayStatInfos.size() != 0) {
        delayAvg = delayAvg / (1.0 * delayStatInfos.size());
    }

    context.put("delayStatInfos", delayStatInfos);
    context.put("delayAvg", delayAvg);
    context.put("channel", channel);
    context.put("pipelineId", pipelineId);
    context.put("start", sdf.format(start));
    context.put("end", sdf.format(end));
}

From source file:alpha.portal.webapp.controller.BaseFormController.java

/**
 * Set up a custom property editor for converting form inputs to real
 * objects./*  w  ww  . j a  v a 2s. com*/
 * 
 * @param request
 *            the current request
 * @param binder
 *            the data binder
 */
@InitBinder
protected void initBinder(final HttpServletRequest request, final ServletRequestDataBinder binder) {
    binder.registerCustomEditor(Integer.class, null, new CustomNumberEditor(Integer.class, null, true));
    binder.registerCustomEditor(Long.class, null, new CustomNumberEditor(Long.class, null, true));
    binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
    final SimpleDateFormat dateFormat = new SimpleDateFormat(this.getText("date.format", request.getLocale()));
    dateFormat.setLenient(false);
    binder.registerCustomEditor(Date.class, null, new CustomDateEditor(dateFormat, true));
}

From source file:com.haulmont.cuba.web.gui.components.WebTimeField.java

public WebTimeField() {
    UserSessionSource uss = AppBeans.get(UserSessionSource.NAME);

    timeFormat = Datatypes.getFormatStringsNN(uss.getLocale()).getTimeFormat();
    resolution = DateField.Resolution.MIN;

    component = new CubaMaskedTextField();
    component.setMaskedMode(true);//ww w.  j  a  v  a2s.  c  o  m
    component.setImmediate(true);
    component.setTimeMask(true);
    setShowSeconds(timeFormat.contains("ss"));

    component.setInvalidAllowed(false);
    component.setInvalidCommitted(true);
    component.addValidator(value -> {
        if (!(!(value instanceof String) || checkStringValue((String) value))) {
            component.markAsDirty();
            throw new com.vaadin.data.Validator.InvalidValueException("Unable to parse value: " + value);
        }
    });
    attachListener(component);

    component.setConverter(new Converter<String, Date>() {
        @Override
        public Date convertToModel(String formattedValue, Class<? extends Date> targetType, Locale locale)
                throws ConversionException {
            if (StringUtils.isNotEmpty(formattedValue) && !formattedValue.equals(placeholder)) {
                try {
                    SimpleDateFormat sdf = new SimpleDateFormat(timeFormat);
                    sdf.setLenient(false);

                    Date date = sdf.parse(formattedValue);
                    if (component.getComponentError() != null)
                        component.setComponentError(null);
                    if (targetType == java.sql.Time.class) {
                        return new Time(date.getTime());
                    }
                    if (targetType == java.sql.Date.class) {
                        LoggerFactory.getLogger(WebTimeField.class)
                                .warn("Do not use java.sql.Date with time field");
                        return new java.sql.Date(date.getTime());
                    }
                    return date;
                } catch (Exception e) {
                    LoggerFactory.getLogger(WebTimeField.class)
                            .debug("Unable to parse value of component {}:\n{}", getId(), e.getMessage());
                    throw new ConversionException("Invalid value");
                }
            } else
                return null;
        }

        @Override
        public String convertToPresentation(Date value, Class<? extends String> targetType, Locale locale)
                throws ConversionException {
            if (value != null) {
                SimpleDateFormat sdf = new SimpleDateFormat(timeFormat);
                return sdf.format(value);
            } else
                return null;
        }

        @Override
        public Class<Date> getModelType() {
            return Date.class;
        }

        @Override
        public Class<String> getPresentationType() {
            return String.class;
        }
    });
}

From source file:net.heroicefforts.viable.android.dao.Issue.java

/**
 * Instantiate the issue state based upon the JIRA JSON format.
 * /*w  w w .  j a  va2s .  co  m*/
 * @param obj JIRA JSON issue object 
 * @throws JSONException if there's an error parsing the JSON.
 */
public Issue(String json) throws JSONException {
    if (Config.LOGV)
        Log.v(TAG, "Parsing issue JSON:  " + json);
    JSONObject issueObj = new JSONObject(json);
    issueId = issueObj.getString("issueId");
    type = issueObj.getString("type");
    if (issueObj.has("priority"))
        priority = issueObj.getString("priority");
    state = issueObj.getString("state");
    appName = issueObj.getString("appName");
    summary = issueObj.getString("summary");
    if (issueObj.has("votes"))
        votes = issueObj.getLong("votes");
    if (issueObj.has("description"))
        description = issueObj.getString("description");
    if (issueObj.has("affectedVersions")) {
        JSONArray affVers = issueObj.getJSONArray("affectedVersions");
        affectedVersions = new String[affVers.length()];
        for (int i = 0; i < affVers.length(); i++)
            affectedVersions[i] = affVers.getString(i);
    }
    if (issueObj.has("hash"))
        hash = issueObj.getString("hash");
    if (issueObj.has("stacktrace"))
        stacktrace = issueObj.getString("stacktrace");
    SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    fmt.setLenient(true);
    try {
        createDate = fmt.parse(issueObj.getString("createDate"));
        modifiedDate = fmt.parse(issueObj.getString("modifiedDate"));
    } catch (ParseException e) {
        Log.e(TAG, "Error parsing JSON dates.", e);
        throw new JSONException("Error parsing JSON dates.");
    }
}

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

@Override
@InitBinder/*from   ww w . ja  v  a  2 s.  co 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));
}

From source file:cn.newcapec.framework.core.utils.dataUtils.DateMorpherEx.java

public Object morph(Object value) {
    if (value == null) {
        return null;
    }//from   w ww. j a va2s .  c o  m

    if (Date.class.isAssignableFrom(value.getClass())) {
        return (Date) value;
    }

    if (!supports(value.getClass())) {
        throw new MorphException(value.getClass() + " is not supported");
    }

    String strValue = (String) value;
    SimpleDateFormat dateParser = null;

    for (int i = 0; i < this.formats.length; ++i) {
        if (dateParser == null)
            dateParser = new SimpleDateFormat(this.formats[i], this.locale);
        else {
            dateParser.applyPattern(this.formats[i]);
        }
        dateParser.setLenient(this.lenient);
        try {
            return dateParser.parse(strValue.toLowerCase());
        } catch (ParseException localParseException) {
        }

    }

    if (super.isUseDefault()) {
        return this.defaultValue;
    }
    throw new MorphException("Unable to parse the date " + value);
}

From source file:org.kordamp.ezmorph.object.DateMorpher.java

public Object morph(Object value) {
    if (value == null) {
        return null;
    }/*from   ww  w .ja va  2  s .  co  m*/

    if (Date.class.isAssignableFrom(value.getClass())) {
        return (Date) value;
    }

    if (!supports(value.getClass())) {
        throw new MorphException(value.getClass() + " is not supported");
    }

    String strValue = (String) value;
    SimpleDateFormat dateParser = null;

    for (String format : formats) {
        if (dateParser == null) {
            dateParser = new SimpleDateFormat(format, locale);
        } else {
            dateParser.applyPattern(format);
        }
        dateParser.setLenient(lenient);
        try {
            return dateParser.parse(strValue.toLowerCase());
        } catch (ParseException pe) {
            // ignore exception, try the next format
        }
    }

    // unable to parse the date
    if (isUseDefault()) {
        return defaultValue;
    } else {
        throw new MorphException("Unable to parse the date " + value);
    }
}

From source file:it.jugpadova.controllers.EventController.java

private EventSearch buildEventSearch(HttpServletRequest req) throws ParseException {
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    df.setLenient(false);
    df.setTimeZone(TimeZone.getTimeZone("GMT"));
    EventSearch eventSearch = new EventSearch();
    eventSearch.setContinent(req.getParameter("continent"));
    eventSearch.setCountry(req.getParameter("country"));
    eventSearch.setJugName(req.getParameter("jugName"));
    eventSearch.setPastEvents(java.lang.Boolean.parseBoolean(req.getParameter("pastEvents")));
    eventSearch.setOrderByDate(req.getParameter("order"));
    String maxResults = req.getParameter("maxResults");
    if (StringUtils.isNotBlank(maxResults)) {
        try {//from w  w  w  . j  a  v a  2  s . c  o  m
            eventSearch.setMaxResults(new Integer(maxResults));
        } catch (NumberFormatException numberFormatException) {
            /* ignore it */
        }
    }
    eventSearch.setFriendlyName(req.getParameter("friendlyName"));
    String startDate = req.getParameter("startDate");
    if (StringUtils.isNotBlank(startDate)) {
        Date date = df.parse(startDate);
        eventSearch.setStartDate(date);
    }
    String endDate = req.getParameter("endDate");
    if (StringUtils.isNotBlank(endDate)) {
        Date date = df.parse(endDate);
        eventSearch.setEndDate(date);
    }
    String startTimestamp = req.getParameter("start");
    if (StringUtils.isNotBlank(startTimestamp)) {
        Date date = new Date(Long.parseLong(startTimestamp) * 1000);
        eventSearch.setStartDate(date);
    }
    String endTimestamp = req.getParameter("end");
    if (StringUtils.isNotBlank(endTimestamp)) {
        Date date = new Date(Long.parseLong(endTimestamp) * 1000);
        eventSearch.setEndDate(date);
    }
    return eventSearch;
}

From source file:com.alibaba.otter.manager.web.home.module.screen.BehaviorHistoryCurve.java

public void execute(@Param("d5221") String startTime, @Param("d5222") String endTime,
        @Param("dataMediaPairId") Long dataMediaPairId, HttpSession session, Context context) throws Exception {
    Date end = null;/*from w  w w  .  ja v  a 2 s. c  o m*/
    Date start = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    if (StringUtils.isEmpty(startTime) || StringUtils.isEmpty(endTime)) {
        start = new Date(System.currentTimeMillis() / 60000 * 60000 - 24 * 60 * 60 * 1000);
        end = new Date(System.currentTimeMillis() / 60000 * 60000);
    } else {// ?24??
        sdf.setLenient(false);
        if (null != startTime && null != endTime) {
            start = sdf.parse(startTime);
            end = sdf.parse(endTime);
        }
    }

    DataMediaPair dataMediaPair = dataMediaPairService.findById(dataMediaPairId);
    Channel channel = channelService.findByPipelineId(dataMediaPair.getPipelineId());

    Map<Long, BehaviorHistoryInfo> behaviourHistoryInfos = new LinkedHashMap<Long, BehaviorHistoryInfo>();

    TimelineBehaviorHistoryCondition condition = new TimelineBehaviorHistoryCondition();

    if (null != start && null != end) {
        condition.setStart(start);
        condition.setEnd(end);
        condition.setPairId(dataMediaPairId);
        behaviourHistoryInfos = tableStatService.listTimelineBehaviorHistory(condition);
    }

    Long totalInsert = 0L;
    Long totalUpdate = 0L;
    Long totalDelete = 0L;
    Long totalFileCount = 0L;
    Long totalFileSize = 0L;
    for (BehaviorHistoryInfo info : behaviourHistoryInfos.values()) {
        totalInsert += info.getInsertNumber();
        totalUpdate += info.getUpdateNumber();
        totalDelete += info.getDeleteNumber();
        totalFileCount += info.getFileNumber();
        totalFileSize += info.getFileSize();
    }

    context.put("totalInsert", totalInsert);
    context.put("totalUpdate", totalUpdate);
    context.put("totalDelete", totalDelete);
    context.put("totalFileCount", totalFileCount);
    context.put("totalFileSize", totalFileSize);
    context.put("behaviourHistoryInfos", behaviourHistoryInfos);
    context.put("start", sdf.format(start));
    context.put("end", sdf.format(end));
    context.put("dataMediaPair", dataMediaPair);
    context.put("channel", channel);
}

From source file:com.alibaba.otter.manager.web.home.module.screen.AnalysisThroughputHistory.java

public void execute(@Param("d5221") String startTime, @Param("d5222") String endTime,
        @Param("pipelineId") Long pipelineId, HttpSession session, Context context) throws Exception {
    Date end = null;//w  w w. j  av a  2  s .  co m
    Date start = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    if (StringUtils.isEmpty(startTime) || StringUtils.isEmpty(endTime)) {
        start = new Date(System.currentTimeMillis() / 60000 * 60000 - 24 * 60 * 60 * 1000);
        end = new Date(System.currentTimeMillis() / 60000 * 60000);
    } else {// ?24??
        sdf.setLenient(false);
        if (null != startTime && null != endTime) {
            start = sdf.parse(startTime);
            end = sdf.parse(endTime);
        }
    }

    Channel channel = channelService.findByPipelineId(pipelineId);
    Map<Long, ThroughputInfo> throughputInfos1 = new LinkedHashMap<Long, ThroughputInfo>();
    Map<Long, ThroughputInfo> throughputInfos2 = new LinkedHashMap<Long, ThroughputInfo>();
    TimelineThroughputCondition condition1 = new TimelineThroughputCondition();
    TimelineThroughputCondition condition2 = new TimelineThroughputCondition();
    if (null != start && null != end) {
        condition1.setStart(start);
        condition1.setEnd(end);
        condition1.setType(ThroughputType.ROW);
        condition1.setPipelineId(pipelineId);
        condition2.setStart(start);
        condition2.setEnd(end);
        condition2.setType(ThroughputType.FILE);
        condition2.setPipelineId(pipelineId);
        throughputInfos1 = throughputStatService.listTimelineThroughput(condition1);
        throughputInfos2 = throughputStatService.listTimelineThroughput(condition2);
    }

    Long totalRecord1 = 0L;
    Long totalRecord2 = 0L;
    Long totalSize1 = 0L;
    Long totalSize2 = 0L;
    for (ThroughputInfo info : throughputInfos1.values()) {
        totalRecord1 += info.getNumber();
        totalSize1 += info.getSize();
    }

    for (ThroughputInfo info : throughputInfos2.values()) {
        totalRecord2 += info.getNumber();
        totalSize2 += info.getSize();
    }

    context.put("throughputInfos1", throughputInfos1);
    context.put("throughputInfos2", throughputInfos2);
    context.put("totalRecord1", totalRecord1);
    context.put("totalRecord2", totalRecord2);
    context.put("totalSize1", totalSize1);
    context.put("totalSize2", totalSize2);
    context.put("channel", channel);
    context.put("pipelineId", pipelineId);
    context.put("start", sdf.format(start));
    context.put("end", sdf.format(end));
}