/** * Checa a data e a hora * * @param data A data * @param hora A hora * @throws InvalidAttributeValueException */ public static void checaDataHora(String data, String hora) throws InvalidAttributeValueException { if (data == null) throw new InvalidAttributeValueException("Data inválida"); if (hora == null) throw new InvalidAttributeValueException("Hora inválida"); SimpleDateFormat format = null; String dataHora = data + " " + hora; Date date = null; format = new SimpleDateFormat("dd/MM/yyyy"); format.setLenient(false); try { date = format.parse(data); } catch (Exception e) { throw new InvalidAttributeValueException("Data inválida"); } format = new SimpleDateFormat("HH:mm"); format.setLenient(false); try { date = format.parse(hora); } catch (Exception e) { throw new InvalidAttributeValueException("Hora inválida"); } format = new SimpleDateFormat("dd/MM/yyyy HH:mm"); format.setLenient(false); try { date = format.parse(dataHora); } catch (ParseException e) { e.printStackTrace(); } if (date.before(new Date())) throw new InvalidAttributeValueException("Data inválida"); }
public Date bind( String name, Annotation[] annotations, String value, Class actualClass, Type genericType) throws Exception { if (value == null || value.trim().length() == 0) { return null; } Date date = AnnotationHelper.getDateAs(annotations, value); if (date != null) { return date; } try { SimpleDateFormat sdf = new SimpleDateFormat(I18N.getDateFormat()); sdf.setLenient(false); return sdf.parse(value); } catch (ParseException e) { // Ignore } try { SimpleDateFormat sdf = new SimpleDateFormat(ISO8601); sdf.setLenient(false); return sdf.parse(value); } catch (Exception e) { throw new IllegalArgumentException( "Cannot convert [" + value + "] to a Date: " + e.toString()); } }
/** * 计算函数 * * @param dateString the dateString in the format of "yyyy-MM-dd HH:mm:ss" or HH:mm:ss". * @return an int from 0 to 59. null if the dateString is not a valid date string */ public Integer evaluate(String dateString) { if (dateString == null) { return null; } if (formatter1 == null) { formatter1 = new SimpleDateFormat(UDFConstants.TIMESTAMP_FORMAT); formatter2 = new SimpleDateFormat(UDFConstants.TIME_FORMAT); calendar = Calendar.getInstance(); /* * 设置时间严格匹配 */ formatter1.setLenient(false); formatter2.setLenient(false); } try { Date date = null; if (dateString.length() > UDFConstants.TIME_FORMAT.length()) { date = formatter1.parse(dateString); } else { date = formatter2.parse(dateString); } calendar.setTime(date); return calendar.get(Calendar.MINUTE); } catch (ParseException e) { LOG.warn(EVALUATE_IGNORE_MESSAGE); return null; } }
static { formatForDate = new SimpleDateFormat(DATE_FORMAT); formatForTimeStamp = new SimpleDateFormat(TIMESTAMP_FORMAT); formatForDecimal = new DecimalFormat(NUMBER_FORMAT); formatForDate.setLenient(false); formatForTimeStamp.setLenient(false); }
static { tempCalGMT.setLenient(false); sdfd.setCalendar(new GregorianCalendar(TimeZone.getTimeZone("GMT"))); sdfd.setLenient(false); sdft.setCalendar(new GregorianCalendar(TimeZone.getTimeZone("GMT"))); sdft.setLenient(false); sdfts.setCalendar(new GregorianCalendar(TimeZone.getTimeZone("GMT"))); sdfts.setLenient(false); }
public static String date2UTC(Date dateTime, TimeZone timeZone) throws UtilitiesException { try { DecimalFormat twoDigits; String utc; String sign; int hours; int minutes; SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); dateFormatter.setLenient(false); dateFormatter.setTimeZone(timeZone); twoDigits = new DecimalFormat("00"); utc = dateFormatter.format(dateTime); int tzOffset = timeZone.getOffset(dateTime.getTime()); sign = "+"; if (tzOffset < 0) { sign = "-"; tzOffset = -tzOffset; } hours = tzOffset / 0x36ee80; minutes = (tzOffset % 0x36ee80) / 60000; return (new StringBuffer(utc.length() + 7)) .append(utc) .append(sign) .append(twoDigits.format(hours)) .append(":") .append(twoDigits.format(minutes)) .toString(); } catch (Exception ex) { throw new hk.hku.cecid.piazza.commons.util.UtilitiesException(ex); } }
/** * Checks if the field is a valid date. * * <p>The pattern is used with {@code java.text.SimpleDateFormat}. 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 {@link java.text.SimpleDateFormat#setLenient(boolean)} * method is set to {@code false} for all. * * @param value The value validation is being performed on. * @param datePattern The pattern passed to {@code SimpleDateFormat}. * @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 && 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; }
private static String getRelativeTimeAgo(String rawJsonDate) { String twitterFormat = "EEE MMM dd HH:mm:ss ZZZZZ yyyy"; SimpleDateFormat sf = new SimpleDateFormat(twitterFormat, Locale.ENGLISH); sf.setLenient(true); String relativeDate = ""; try { long dateMillis = sf.parse(rawJsonDate).getTime(); relativeDate = DateUtils.getRelativeTimeSpanString( dateMillis, System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS) .toString(); } catch (java.text.ParseException e) { e.printStackTrace(); } String h1 = relativeDate.replaceAll("hours", "h"); String h2 = h1.replaceAll("hour", "h"); String m1 = h2.replaceAll("minutes", "m"); String m2 = m1.replaceAll("minute", "m"); String s1 = m2.replaceAll("seconds", "s"); String s2 = s1.replaceAll("second", "s"); String f = s2.replaceAll("ago", ""); return f; // relativeDate; }
public static Date getTimeStamp(String date) throws ParseException { final String TWITTER = "EEE MMM dd HH:mm:ss z yyyy"; SimpleDateFormat sf = new SimpleDateFormat(TWITTER); sf.setTimeZone(TimeZone.getTimeZone("GMT-04:00")); sf.setLenient(true); return sf.parse(date); }
protected static SimpleDateFormat getDateFormatter() { if (c_sdfFormatter == null) { c_sdfFormatter = new SimpleDateFormat("MMddyyyy"); c_sdfFormatter.setLenient(false); } return c_sdfFormatter; }
private Object getValueFromField(int propertyType) throws ParseException { switch (nodePropertyParameters.propertyType) { case PropertyType.BINARY: return null; case PropertyType.BOOLEAN: return booleanValue.isSelected(); case PropertyType.DATE: SimpleDateFormat dateFormat = new SimpleDateFormat(); dateFormat.setLenient(true); String fieldText = dateValue.getText(); dateFormat.parse(fieldText); return dateFormat.getCalendar(); case PropertyType.DOUBLE: return Double.parseDouble(doubleValue.getText()); case PropertyType.LONG: return Long.parseLong(longValue.getText()); case PropertyType.NAME: return nameValue.getText(); case PropertyType.PATH: return pathValue.getText(); case PropertyType.REFERENCE: return referenceValue.getText(); case PropertyType.STRING: return stringValue.getText(); default: return null; } }
/** * Get (Short) Date Format. The date format must parseable by org.compiere.grid.ed.MDocDate i.e. * leading zero for date and month * * @return date format MM/dd/yyyy - dd.MM.yyyy */ public SimpleDateFormat getDateFormat() { if (m_dateFormat == null) { m_dateFormat = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT, m_locale); String sFormat = m_dateFormat.toPattern(); // some short formats have only one M and/or d (e.g. ths US) if (sFormat.indexOf("MM") == -1 || sFormat.indexOf("dd") == -1) { sFormat = sFormat.replaceFirst("d+", "dd"); sFormat = sFormat.replaceFirst("M+", "MM"); // log.finer(sFormat + " => " + nFormat); m_dateFormat.applyPattern(sFormat); } // Unknown short format => use JDBC if (m_dateFormat.toPattern().length() != 8) m_dateFormat.applyPattern("yyyy-MM-dd"); // 4 digit year if (m_dateFormat.toPattern().indexOf("yyyy") == -1) { sFormat = m_dateFormat.toPattern(); String nFormat = ""; for (int i = 0; i < sFormat.length(); i++) { if (sFormat.charAt(i) == 'y') nFormat += "yy"; else nFormat += sFormat.charAt(i); } m_dateFormat.applyPattern(nFormat); } m_dateFormat.setLenient(true); } return m_dateFormat; } // getDateFormat
private static Date parseDateWithLeniency(String str, String[] parsePatterns, boolean lenient) throws ParseException { if ((str == null) || (parsePatterns == null)) { throw new IllegalArgumentException("Date and Patterns must not be null"); } SimpleDateFormat parser = new SimpleDateFormat(); parser.setLenient(lenient); ParsePosition pos = new ParsePosition(0); for (int i = 0; i < parsePatterns.length; i++) { String pattern = parsePatterns[i]; if (parsePatterns[i].endsWith("ZZ")) { pattern = pattern.substring(0, pattern.length() - 1); } parser.applyPattern(pattern); pos.setIndex(0); String str2 = str; if (parsePatterns[i].endsWith("ZZ")) { int signIdx = indexOfSignChars(str2, 0); while (signIdx >= 0) { str2 = reformatTimezone(str2, signIdx); signIdx = indexOfSignChars(str2, ++signIdx); } } Date date = parser.parse(str2, pos); if ((date != null) && (pos.getIndex() == str2.length())) { return date; } } throw new ParseException("Unable to parse the date: " + str, -1); }
public static Date parseDate(final String source, final String conversionPattern) throws ParseException { SimpleDateFormat sdf = DATE_FORMAT.get(); sdf.applyPattern(conversionPattern); sdf.setLenient(false); return sdf.parse(source); }
@SuppressWarnings("unused") @InitBinder({"myCommand", "date"}) private void initBinder(WebDataBinder binder) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); dateFormat.setLenient(false); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false)); }
public static Date getTwitterDate(String date) throws ParseException { final String format = "EEE MMM dd HH:mm:ss ZZZZZ yyyy"; SimpleDateFormat sf = new SimpleDateFormat(format); sf.setLenient(true); return sf.parse(date); }
private boolean isValid(String dateString, String pattern) { boolean valid = true; Date date; SimpleDateFormat sdf = new SimpleDateFormat(pattern); sdf.setLenient(false); try { dateString = dateString.trim(); date = sdf.parse(dateString); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int year = calendar.get(Calendar.YEAR); int era = calendar.get(Calendar.ERA); if (era == GregorianCalendar.AD) { if (year > 9999) { valid = false; } } // System.out.println("pattern is "+ pattern); // System.out.println("Year is "+year); // System.out.println("Calendar date is "+date.toString()); // System.out.println("Era is "+calendar.get(Calendar.ERA)); } catch (ParseException e) { valid = false; } if (dateString.length() > pattern.length()) { valid = false; } return valid; }
/** * SubjectUploader constructor * * @param study study identifier in context * @param iArkCommonService common ARK service to perform select/insert/updates to the database * @param iPhenotypicService study service to perform select/insert/updates to the study database */ public CustomDataUploader( Study study, IArkCommonService iArkCommonService, IPhenotypicService iPhenotypicService) { this.study = study; this.iArkCommonService = iArkCommonService; this.iPhenotypicService = iPhenotypicService; simpleDateFormat.setLenient(false); }
private void b() { a = ((TextView)findViewById(2131624176)); e = new SimpleDateFormat("HH:mm:ss"); e.setLenient(false); long l = System.currentTimeMillis(); c = (ManagerApp.n().e() + 1000L - l); if (c <= 1000L) { dismiss(); return; } a.setText(com.tinder.utils.g.c(c)); f = new CountDownTimer(c, 1000L) { public void onFinish() { dismiss(); } public void onTick(long paramAnonymousLong) { s.a(s.this, paramAnonymousLong - 1000L); s.b(s.this).setText(com.tinder.utils.g.c(s.a(s.this))); } }; f.start(); }
@InitBinder public void initBinder(WebDataBinder binder) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); sdf.setLenient(true); binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true)); }
@InitBinder public void dataBinding(WebDataBinder binder) { binder.addValidators(feriasValidation); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); dateFormat.setLenient(false); binder.registerCustomEditor(Date.class, "dataInicio", new CustomDateEditor(dateFormat, true)); }
@InitBinder public void initBinder(WebDataBinder binder) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); dateFormat.setLenient(false); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); }
private Date parseDateForFormat(final String format, final String date) { // test date string matches format structure using regex // - weed out illegal characters and enforce 4-digit year // - create the regex based on the local format string String reFormat = Pattern.compile("d+|M+|H+|m+") .matcher(Matcher.quoteReplacement(format)) .replaceAll("\\\\d{1,2}"); reFormat = Pattern.compile("y+").matcher(reFormat).replaceAll("\\\\d{4,}"); if (Pattern.compile(reFormat).matcher(date).matches()) { // date string matches format structure, // - now test it can be converted to a valid date SimpleDateFormat sdf = (SimpleDateFormat) DateFormat.getDateInstance(); sdf.applyPattern(format); sdf.setTimeZone(timeZoneManager.getLoggedInUserTimeZone()); sdf.setLenient(false); try { return sdf.parse(date); } catch (ParseException e) { return null; } } return null; }
private long parse3339(String time) { if (time == null || time.length() == 0) { return 0; } if (timestamper != null) { try { timestamper.parse3339(time); } catch (TimeFormatException e) { parse_trace("got TimeFormatException parsing timestamp: \"" + time + '"'); e.printStackTrace(); return 0; } return timestamper.normalize(false); } else { Date timestamp = new Date(); SimpleDateFormat rfc3339 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); rfc3339.setLenient(true); try { timestamp = rfc3339.parse(time); } catch (ParseException e) { parse_trace("got ParseException parsing timestamp: \"" + time + '"'); e.printStackTrace(); return 0; } return timestamp.getTime(); } }
@Override public void initBinder(WebDataBinder binder, WebRequest request) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); dateFormat.setLenient(false); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false)); binder.registerCustomEditor(String.class, new StringTrimmerEditor(false)); }
@InitBinder public void initBinder(WebDataBinder binder, @PathVariable("hotel") String hotel) { assertEquals("Invalid path variable value", "42", hotel); binder.initBeanPropertyAccess(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); dateFormat.setLenient(false); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false)); }
static { expressaoRegularPorFormatoData = new HashMap<String, String>(); expressaoRegularPorFormatoData.put( "dd/MM/yyyy", "^(0[1-9]|[12][0-9]|3[01])[/]?([0][1-9]|[1][0-2])[/]?\\d{4}$"); expressaoRegularPorFormatoData.put("MM/yyyy", "^([0][1-9]|[1][0-2])[/]?\\d{4}$"); dateFormat = new SimpleDateFormat(); dateFormat.setLenient(false); }
public static String format( final Date date, final boolean lenient, final String conversionPattern) { SimpleDateFormat sdf = DATE_FORMAT.get(); if (conversionPattern != null) { sdf.applyPattern(conversionPattern); } sdf.setLenient(lenient); return sdf.format(date); }
public Object fromString(String date) { try { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); dateFormat.setLenient(true); return dateFormat.parse(date); } catch (Exception e) { return null; } }
public URLUnwrapperWorker(String threadName) { super(threadName); log.debug("-- URLUnwrapperWorker()"); // "Mon Jan 07 12:35:06 +0000 2013" dateFormatter = new SimpleDateFormat("EEE MMM d HH:mm:ss ZZZZZ yyyy", Locale.ENGLISH); dateFormatter.setLenient(true); }