List of usage examples for java.text SimpleDateFormat setLenient
public void setLenient(boolean lenient)
From source file:org.pentaho.di.core.util.DateDetector.java
/** * //w w w .j a va 2 s . c om * @param dateString * date string for parse * @param dateFormat * format which should be applied for string * @return {@link java.util.Date} converted from dateString by format * @throws ParseException * if we can not parse date string */ public static Date getDateFromStringByFormat(String dateString, String dateFormat) throws ParseException { if (dateFormat == null) { throw new ParseException("Unknown date format. Format is null. ", 0); } if (dateString == null) { throw new ParseException("Unknown date string. Date string is null. ", 0); } SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat); simpleDateFormat.setLenient(false); // Don't automatically convert invalid date. return simpleDateFormat.parse(dateString); }
From source file:org.opentides.util.StringUtil.java
@Deprecated public static Date convertFlexibleDate(String strDate, String[] formats) throws ParseException { if (StringUtil.isEmpty(strDate)) return null; for (int i = 0; i < formats.length; i++) { try {/*from w w w .j a v a2s . c o m*/ SimpleDateFormat dtFormatter = new SimpleDateFormat(formats[i]); dtFormatter.setLenient(false); return dtFormatter.parse(strDate.trim()); } catch (ParseException e) { // do nothing... try other format } } // we are unable to convert throw new ParseException("No matching date format for " + strDate, 0); }
From source file:org.alfresco.util.CachingDateFormat.java
/** * @param pattern//from www .jav a 2 s. co m * the conversion pattern to use * @param lenient * true to allow the parser to extract the date in conceivable * manner * @return Returns a conversion-cacheing formatter for the given pattern, * but the instance itself is not cached */ public static SimpleDateFormat getDateFormat(String pattern, boolean lenient) { // create an alfrescoDateFormat for cacheing purposes SimpleDateFormat dateFormat = new CachingDateFormat(pattern); // set leniency dateFormat.setLenient(lenient); // done return dateFormat; }
From source file:org.akaza.openclinica.core.form.StringUtil.java
private static boolean partialYear(String s, String yearFormat, Locale locale) { int dn = 0;/*from w w w.j a v a2s. c om*/ char[] cyear = s.toCharArray(); for (char c : cyear) { if (!Character.isDigit(c)) { return false; } ++dn; } if (dn != 4) { return false; } String yearformat = parseDateFormat(yearFormat) + "-MM-dd"; SimpleDateFormat sdf_y; if (locale == null) { sdf_y = new SimpleDateFormat(yearformat); } else { sdf_y = new SimpleDateFormat(yearformat, locale); } sdf_y.setLenient(false); String sy = s + "-01-18"; try { sdf_y.parse(sy); return true; } catch (Exception e) { return false; } }
From source file:com.zimbra.cs.util.SoapCLI.java
public static Date parseDatetime(String str) { for (String formatStr : DATETIME_FORMATS) { SimpleDateFormat fmt = new SimpleDateFormat(formatStr); fmt.setLenient(false); ParsePosition pp = new ParsePosition(0); Date d = fmt.parse(str, pp); if (d != null && pp.getIndex() == str.length()) return d; }/*from w ww. j a v a 2 s . c o m*/ return null; }
From source file:jp.terasoluna.fw.util.DateUtil.java
/** * java.util.Date?????//from w w w . jav a2 s . c om * ?? * * <p> * ApplicationResources.properties * ????????? * ???????<br> * ?????????? * ?? * <strong> ApplicationResources.properties ? * ?</strong><br> * <code><pre> * wareki.gengo.0.name = ? * wareki.gengo.0.roman = H * wareki.gengo.0.startDate = 1989/01/08 * wareki.gengo.1.name = * wareki.gengo.1.roman = S * wareki.gengo.1.startDate = 1926/12/25 * wareki.gengo.2.name = * wareki.gengo.2.roman = T * wareki.gengo.2.startDate = 1912/07/30 * wareki.gengo.3.name = * wareki.gengo.3.roman = M * wareki.gengo.3.startDate = 1868/09/04 * </pre></code> * </p> * * <strong></strong><br> * <p>??java.text.SimpleDateFormat ? * <i></i> ????????? * ? SimpleDateFormat ?? * </p> * * <div width="90%" align="center"> * <table border="1"> * <tr> * <th>?</th> * <th><code> SimpleDateFormat</code> </th> * <th><code> dateToWarekiString()</code> </th> * </tr> * <tr> * <td>G</td> * <td align="left"><br><br><br>AD</td> * <td align="left">?<br><br> * <br> * 4???<br> * ????<br> * 3???<br> * M?T?S?H</td> * </tr> * <tr> * <td>y</td> * <td align="left"><br><br><br>2002</td> * <td align="left"><br><br><br>14</td> * </tr> * <tr> * <td>E</td> * <td align="left"><br><br><br>Tuesday</td> * <td align="left"><br><br> * <br> * 4???<br> * ???<br> * 3???<br> * ???</td> * </tr> * </table> * </div> * * <p>?????E????? SimpleDateFotmat * ???? "ja" * ???????</p> * * <p>????????????getWarekiGengoName()? * getWarekiGengoRoman()?getWarekiYear() ????? * ????????AplicationResources ? * ????</p> * * <p><code><pre> * wareki.gengo.<i>ID</i>.name=<i>???</i> * wareki.gengo.<i>ID</i>.roman=<i>??</i> * wareki.gengo.<i>ID</i>.startDate=<i>?:yyyy/MM/dd?</i> * </pre></code></p> * * <p>ID???????????????? * ???</p> * * @param format * @param date ??? * @return ????? */ public static String dateToWarekiString(String format, java.util.Date date) { // SimpleDateFormat?????'G'???'y'? // ??? StringBuilder sb = new StringBuilder(); boolean inQuote = false; // ??????? char prevCh = 0; int count = 0; for (int i = 0; i < format.length(); i++) { char ch = format.charAt(i); if (ch != prevCh && count > 0) { if (prevCh == 'G' && count >= 4) { sb.append(getWarekiGengoName(date)); } else if (prevCh == 'G') { // ??????????? sb.append('\''); sb.append(getWarekiGengoRoman(date)); sb.append('\''); } else if (prevCh == 'y') { sb.append(getWarekiYear(date)); } count = 0; } if (ch == '\'') { sb.append('\''); inQuote = !inQuote; } else if (!inQuote && (ch == 'G' || ch == 'y')) { // ch?????????? // ?? prevCh = ch; ++count; } else { // ???????? sb.append(ch); } } // ???? if (count > 0) { if (prevCh == 'G' && count >= 4) { sb.append(getWarekiGengoName(date)); } else if (prevCh == 'G') { sb.append('\''); sb.append(getWarekiGengoRoman(date)); sb.append('\''); } else if (prevCh == 'y') { sb.append(getWarekiYear(date)); } } SimpleDateFormat sdf = new SimpleDateFormat(sb.toString(), Locale.JAPAN); sdf.getCalendar().setLenient(false); sdf.setLenient(false); return sdf.format(date); }
From source file:com.jeeframework.util.validate.GenericTypeValidator.java
/** * <p>//w ww . j a v a 2 s . c o m * Checks if the field is a valid date. The pattern is used with <code>java.text.SimpleDateFormat</code> * . If strict is true, then the length will be checked so '2/12/1999' will * not pass validation with the format 'MM/dd/yyyy' because the month isn't * two digits. The setLenient method is set to <code>false</code> for all. * </p> * *@param value The value validation is being performed on. *@param datePattern The pattern passed to <code>SimpleDateFormat</code>. *@param strict Whether or not to have an exact match of the * datePattern. *@return the converted Date value. */ public static Date formatDate(String value, String datePattern, boolean strict) { Date date = null; if (value == null || datePattern == null || datePattern.length() == 0) { return null; } try { SimpleDateFormat formatter = new SimpleDateFormat(datePattern); formatter.setLenient(false); date = formatter.parse(value); if (strict) { if (datePattern.length() != value.length()) { date = null; } } } catch (ParseException e) { // Bad date so return null Log log = LogFactory.getLog(GenericTypeValidator.class); if (log.isDebugEnabled()) { log.debug("Date parse failed value=[" + value + "], " + "pattern=[" + datePattern + "], " + "strict=[" + strict + "] " + e); } } return date; }
From source file:jp.co.nemuzuka.utils.DateTimeUtils.java
/** * ??.//from w w w .ja v a 2 s . c o m * ??????Date???? * @param targetYyyyMM ? * @return index 0:??Date index 1:?Date */ public static List<Date> getStartEndDate(String targetYyyyMM) { SimpleDateFormat sdf = createSdf("yyyyMMdd"); sdf.setLenient(false); List<Date> retList = new ArrayList<Date>(); try { Date startDate = sdf.parse(targetYyyyMM + "01"); retList.add(startDate); //1???1???? Date endDate = addMonths(startDate, 1); endDate = addDays(endDate, -1); retList.add(endDate); } catch (ParseException e) { throw new RuntimeException(e); } return retList; }
From source file:org.lingcloud.molva.ocl.util.GenericTypeValidator.java
/** * <p>//www .j a va 2s. co m * Checks if the field is a valid date. The pattern is used with * <code>java.text.SimpleDateFormat</code> . If strict is true, then the * length will be checked so '2/12/1999' will not pass validation with the * format 'MM/dd/yyyy' because the month isn't two digits. The setLenient * method is set to <code>false</code> for all. * </p> * * @param value * The value validation is being performed on. * @param datePattern * The pattern passed to <code>SimpleDateFormat</code>. * @param strict * Whether or not to have an exact match of the datePattern. * @return the converted Date value. */ public static Date formatDate(String value, String datePattern, boolean strict) { Date date = null; if (value == null || datePattern == null || datePattern.length() == 0) { return null; } try { SimpleDateFormat formatter = new SimpleDateFormat(datePattern); formatter.setLenient(false); date = formatter.parse(value); if (strict) { if (datePattern.length() != value.length()) { date = null; } } } catch (ParseException e) { // Bad date so return null if (log.isDebugEnabled()) { log.debug("Date parse failed value=[" + value + "], " + "pattern=[" + datePattern + "], " + "strict=[" + strict + "] " + e); } } return date; }
From source file:org.pentaho.reporting.libraries.designtime.swing.date.DateCellEditor.java
private static DateFormat createDateFormat(final String parameterFormatString, final Locale locale, final TimeZone timeZone) { if (parameterFormatString != null) { try {//w ww .j a va 2 s . com final SimpleDateFormat dateFormat = new SimpleDateFormat(parameterFormatString, new DateFormatSymbols(locale)); dateFormat.setTimeZone(timeZone); dateFormat.setLenient(true); return dateFormat; } catch (Exception e) { // boo! Not a valid pattern ... // its not a show-stopper either, as the pattern is a mere hint, not a mandatory thing logger.warn("Parameter format-string for date-parameter was not a valid date-format-string", e); } } final SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_FORMAT, new DateFormatSymbols(locale)); dateFormat.setTimeZone(timeZone); dateFormat.setLenient(true); return dateFormat; }