Java Code Examples for org.apache.commons.lang3.time.FastDateFormat#parse()

The following examples show how to use org.apache.commons.lang3.time.FastDateFormat#parse() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CalendarPatterns.java    From openmeetings with Apache License 2.0 6 votes vote down vote up
private static Date validDate(FastDateFormat sdf, String testdate) {
	Date resultDate = null;
	try {
		resultDate = sdf.parse(testdate);
	} catch (ParseException|NumberFormatException e) {
		// if the format of the string provided doesn't match the format we
		// declared in SimpleDateFormat() we will get an exception
		return null;
	}

	if (!sdf.format(resultDate).equals(testdate)) {
		return null;
	}

	return resultDate;
}
 
Example 2
Source File: IcalUtils.java    From openmeetings with Apache License 2.0 6 votes vote down vote up
/**
 * Adapted from DateUtils to support Timezones, and parse ical dates into {@link java.util.Date}.
 * Note: Replace FastDateFormat to java.time, when shifting to Java 8 or higher.
 *
 * @param str      Date representation in String.
 * @param patterns Patterns to parse the date against
 * @param inTimeZone Timezone of the Date.
 * @return <code>java.util.Date</code> representation of string or
 * <code>null</code> if the Date could not be parsed.
 */
public Date parseDate(String str, String[] patterns, TimeZone inTimeZone) {
	FastDateFormat parser;
	Locale locale = WebSession.get().getLocale();

	TimeZone timeZone = str.endsWith("Z") ? TimeZone.getTimeZone("UTC") : inTimeZone;

	ParsePosition pos = new ParsePosition(0);
	for (String pattern : patterns) {
		parser = FastDateFormat.getInstance(pattern, timeZone, locale);
		pos.setIndex(0);
		Date date = parser.parse(str, pos);
		if (date != null && pos.getIndex() == str.length()) {
			return date;
		}
	}
	log.error("Unable to parse the date: {} at {}", str, -1);
	return null;
}
 
Example 3
Source File: DateExtractorTransformator.java    From SkaETL with Apache License 2.0 5 votes vote down vote up
public void apply(String idProcess, ParameterTransformation parameterTransformation, ObjectNode jsonValue) {
    String valueToFormat = at(parameterTransformation.getFormatDateValue().getKeyField(), jsonValue).asText();
    if (StringUtils.isNotBlank(valueToFormat)) {
        FastDateFormat srcFormatter = srcFormats.computeIfAbsent(parameterTransformation.getFormatDateValue().getSrcFormat(), key -> FastDateFormat.getInstance(key));
        try {
            Date asDate = srcFormatter.parse(valueToFormat);
            DateTimeFormatter dateTimeFormatter = destFormats.computeIfAbsent(parameterTransformation.getFormatDateValue().getTargetFormat(), key -> DateTimeFormatter.ofPattern(key));
            String result = asDate.toInstant().atZone(ZoneId.systemDefault()).format(dateTimeFormatter);
            put(jsonValue, parameterTransformation.getFormatDateValue().getTargetField(), result);
        } catch (ParseException e) {
            log.error("ExecutionException on field {} for value {}", parameterTransformation.getFormatDateValue(), jsonValue.toString());
        }

    }
}
 
Example 4
Source File: DateAdapter.java    From recheck with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Date unmarshal( final String value ) {
	if ( value == null ) {
		return null;
	}
	for ( final FastDateFormat dateFormat : possibleDateFormats ) {
		try {
			return dateFormat.parse( value );
		} catch ( final ParseException ignored ) {/* NOP */}
	}
	throw new RuntimeException( "Can't parse date '" + value + "'!" );
}
 
Example 5
Source File: CleanUpScheduler.java    From vjtools with Apache License 2.0 5 votes vote down vote up
/**
 * return current date time by specified hour:minute
 * 
 * @param plan format: hh:mm
 */
public static Date getCurrentDateByPlan(String plan, String pattern) {
	try {
		FastDateFormat format = FastDateFormat.getInstance(pattern);
		Date end = format.parse(plan);
		Calendar today = Calendar.getInstance();
		end = DateUtils.setYears(end, (today.get(Calendar.YEAR)));
		end = DateUtils.setMonths(end, today.get(Calendar.MONTH));
		end = DateUtils.setDays(end, today.get(Calendar.DAY_OF_MONTH));
		return end;
	} catch (Exception e) {
		throw ExceptionUtil.unchecked(e);
	}
}
 
Example 6
Source File: CleanUpScheduler.java    From vjtools with Apache License 2.0 5 votes vote down vote up
/**
 * return current date time by specified hour:minute
 * 
 * @param plan format: hh:mm
 */
public static Date getCurrentDateByPlan(String plan, String pattern) {
	try {
		FastDateFormat format = FastDateFormat.getInstance(pattern);
		Date end = format.parse(plan);
		Calendar today = Calendar.getInstance();
		end = DateUtils.setYears(end, (today.get(Calendar.YEAR)));
		end = DateUtils.setMonths(end, today.get(Calendar.MONTH));
		end = DateUtils.setDays(end, today.get(Calendar.DAY_OF_MONTH));
		return end;
	} catch (Exception e) {
		throw ExceptionUtil.unchecked(e);
	}
}
 
Example 7
Source File: CleanUpScheduler.java    From feeyo-redisproxy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * return current date time by specified hour:minute
 * 
 * @param plan format: hh:mm
 */
public static Date getCurrentDateByPlan(String plan, String pattern) {
	try {
		FastDateFormat format = FastDateFormat.getInstance(pattern);
		Date end = format.parse(plan);
		Calendar today = Calendar.getInstance();
		end = DateUtils.setYears(end, (today.get(Calendar.YEAR)));
		end = DateUtils.setMonths(end, today.get(Calendar.MONTH));
		end = DateUtils.setDays(end, today.get(Calendar.DAY_OF_MONTH));
		return end;
	} catch (Exception e) {
		throw new RuntimeException( e );
	}
}
 
Example 8
Source File: DateParamConverter.java    From openmeetings with Apache License 2.0 5 votes vote down vote up
public static Date get(String val) {
	if (Strings.isEmpty(val)) {
		return null;
	}
	for (FastDateFormat df : formats) {
		try {
			return df.parse(val);
		} catch (ParseException e) {
			// no-op
		}
	}
	throw new IllegalArgumentException("Unparsable format: " + val);
}