List of usage examples for org.apache.commons.lang.time DateUtils parseDateStrictly
public static Date parseDateStrictly(String str, String... parsePatterns) throws ParseException
Parses a string representing a date by trying a variety of different parsers.
The parse will try each parse pattern in turn.
From source file:com.vamonossoftware.core.DateUtil.java
/** * Converts the String to a Date using the set patterns. *///from w w w . j a va 2s. c o m public static Date date(String date) { try { return DateUtils.parseDateStrictly(date, activePatterns); } catch (ParseException ex) { throw new RuntimeException(ex); } }
From source file:com.nearinfinity.honeycomb.hbase.bulkload.FieldParser.java
private static ByteBuffer extractDate(String val, String dateFormat, String... parseFormats) throws ParseException { Date d = DateUtils.parseDateStrictly(val, parseFormats); SimpleDateFormat format = new SimpleDateFormat(dateFormat); return ByteBuffer.wrap(format.format(d).getBytes()); }
From source file:com.ikanow.aleph2.data_model.utils.TimeUtils.java
/** Returns the date corresponding to a string in one of the formats returned by getTimeBasedSuffix * @param suffix - the date string//from w w w . j a v a 2 s . c om * @return - either the date, or an error if the string is not correctly formatted */ public static Validation<String, Date> getDateFromSuffix(final String suffix) { try { return Validation.success(DateUtils.parseDateStrictly(suffix, SUPPORTED_DATE_SUFFIXES)); } catch (ParseException e) { return Validation.fail(ErrorUtils.getLongForm("getDateFromSuffix {0}", e)); } }
From source file:org.apache.cassandra.db.marshal.TimestampType.java
public static long dateStringToTimestamp(String source) throws MarshalException { long millis;//from w w w. j av a 2 s. com if (source.toLowerCase().equals("now")) { millis = System.currentTimeMillis(); } // Milliseconds since epoch? else if (source.matches("^\\d+$")) { try { millis = Long.parseLong(source); } catch (NumberFormatException e) { throw new MarshalException(String.format("unable to make long (for date) from: '%s'", source), e); } } // Last chance, attempt to parse as date-time string else { try { millis = DateUtils.parseDateStrictly(source, TimestampSerializer.iso8601Patterns).getTime(); } catch (ParseException e1) { throw new MarshalException( String.format("unable to coerce '%s' to a formatted date (long)", source), e1); } } return millis; }
From source file:org.apache.nutch.util.JexlUtil.java
/** * Parses the given experssion to a Jexl expression. This supports * date parsing.//from w w w.java2 s . c o m * * @param expr the Jexl expression * @return parsed Jexl expression or null in case of parse error */ public static Expression parseExpression(String expr) { if (expr == null) return null; try { // Translate any date object into a long, dates must be specified as 20-03-2016T00:00:00Z Matcher matcher = datePattern.matcher(expr); if (matcher.find()) { String date = matcher.group(); // Parse the thing and get epoch! Date parsedDate = DateUtils.parseDateStrictly(date, new String[] { "yyyy-MM-dd'T'HH:mm:ss'Z'" }); long time = parsedDate.getTime(); // Replace in the original expression expr = expr.replace(date, Long.toString(time)); } JexlEngine jexl = new JexlEngine(); jexl.setSilent(true); jexl.setStrict(true); return jexl.createExpression(expr); } catch (Exception e) { LOG.error(e.getMessage()); } return null; }
From source file:org.cesecore.certificates.endentity.ExtendedInformation.java
/** Implementation of UpgradableDataHashMap function upgrade. */ public void upgrade() { if (Float.compare(LATEST_VERSION, getVersion()) != 0) { // New version of the class, upgrade String msg = intres.getLocalizedMessage("endentity.extendedinfoupgrade", new Float(getVersion())); log.info(msg);/*ww w . j av a2 s.c om*/ if (data.get(SUBJECTDIRATTRIBUTES) == null) { data.put(SUBJECTDIRATTRIBUTES, ""); } if (data.get(MAXFAILEDLOGINATTEMPTS) == null) { setMaxLoginAttempts(DEFAULT_MAXLOGINATTEMPTS); } if (data.get(REMAININGLOGINATTEMPTS) == null) { setRemainingLoginAttempts(DEFAULT_REMAININGLOGINATTEMPTS); } // In EJBCA 4.0.0 we changed the date format if (getVersion() < 3) { final DateFormat oldDateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, Locale.US); final FastDateFormat newDateFormat = FastDateFormat.getInstance("yyyy-MM-dd HH:mm"); try { final String oldCustomStartTime = getCustomData(ExtendedInformation.CUSTOM_STARTTIME); if (!isEmptyOrRelative(oldCustomStartTime)) { // We use an absolute time format, so we need to upgrade final String newCustomStartTime = newDateFormat .format(oldDateFormat.parse(oldCustomStartTime)); setCustomData(ExtendedInformation.CUSTOM_STARTTIME, newCustomStartTime); if (log.isDebugEnabled()) { log.debug("Upgraded " + ExtendedInformation.CUSTOM_STARTTIME + " from \"" + oldCustomStartTime + "\" to \"" + newCustomStartTime + "\" in ExtendedInformation."); } } } catch (ParseException e) { log.error("Unable to upgrade " + ExtendedInformation.CUSTOM_STARTTIME + " in extended user information.", e); } try { final String oldCustomEndTime = getCustomData(ExtendedInformation.CUSTOM_ENDTIME); if (!isEmptyOrRelative(oldCustomEndTime)) { // We use an absolute time format, so we need to upgrade final String newCustomEndTime = newDateFormat.format(oldDateFormat.parse(oldCustomEndTime)); setCustomData(ExtendedInformation.CUSTOM_ENDTIME, newCustomEndTime); if (log.isDebugEnabled()) { log.debug( "Upgraded " + ExtendedInformation.CUSTOM_ENDTIME + " from \"" + oldCustomEndTime + "\" to \"" + newCustomEndTime + "\" in ExtendedInformation."); } } } catch (ParseException e) { log.error("Unable to upgrade " + ExtendedInformation.CUSTOM_ENDTIME + " in extended user information.", e); } } // In 4.0.2 we further specify the storage format by saying that UTC TimeZone is implied instead of local server time if (getVersion() < 4) { final String[] timePatterns = { "yyyy-MM-dd HH:mm" }; final String oldStartTime = getCustomData(ExtendedInformation.CUSTOM_STARTTIME); if (!isEmptyOrRelative(oldStartTime)) { try { final String newStartTime = ValidityDate .formatAsUTC(DateUtils.parseDateStrictly(oldStartTime, timePatterns)); setCustomData(ExtendedInformation.CUSTOM_STARTTIME, newStartTime); if (log.isDebugEnabled()) { log.debug("Upgraded " + ExtendedInformation.CUSTOM_STARTTIME + " from \"" + oldStartTime + "\" to \"" + newStartTime + "\" in EndEntityProfile."); } } catch (ParseException e) { log.error("Unable to upgrade " + ExtendedInformation.CUSTOM_STARTTIME + " to UTC in EndEntityProfile! Manual interaction is required (edit and verify).", e); } } final String oldEndTime = getCustomData(ExtendedInformation.CUSTOM_ENDTIME); if (!isEmptyOrRelative(oldEndTime)) { // We use an absolute time format, so we need to upgrade try { final String newEndTime = ValidityDate .formatAsUTC(DateUtils.parseDateStrictly(oldEndTime, timePatterns)); setCustomData(ExtendedInformation.CUSTOM_ENDTIME, newEndTime); if (log.isDebugEnabled()) { log.debug("Upgraded " + ExtendedInformation.CUSTOM_ENDTIME + " from \"" + oldEndTime + "\" to \"" + newEndTime + "\" in EndEntityProfile."); } } catch (ParseException e) { log.error("Unable to upgrade " + ExtendedInformation.CUSTOM_ENDTIME + " to UTC in EndEntityProfile! Manual interaction is required (edit and verify).", e); } } } data.put(VERSION, Float.valueOf(LATEST_VERSION)); } }
From source file:org.cesecore.util.ValidityDate.java
/** Parse a String in the format "yyyy-MM-dd HH:mm" as a date with implied TimeZone UTC. */ public static Date parseAsUTC(final String dateString) throws ParseException { return DateUtils.parseDateStrictly(dateString + "+00:00", IMPLIED_UTC_PATTERN_TZ); }
From source file:org.cesecore.util.ValidityDate.java
/** Parse a String in the format "yyyy-MM-dd HH:mm:ssZZ". The hour/minutes, seconds and timezone are optional parts. */ public static Date parseAsIso8601(final String dateString) throws ParseException { try {/* w w w .ja v a 2 s. co m*/ return DateUtils.parseDateStrictly(dateString, ISO8601_PATTERNS); } catch (ParseException e) { // Try again with timezone. In DateUtils, the default timezone seems to be the server // timezone and not UTC, so we can't have date formats without "ZZ". return DateUtils.parseDateStrictly(dateString + "+00:00", ISO8601_PATTERNS); } }
From source file:org.ejbca.core.model.ra.ExtendedInformation.java
/** Implementation of UpgradableDataHashMap function upgrade. */ public void upgrade() { if (Float.compare(LATEST_VERSION, getVersion()) != 0) { // New version of the class, upgrade String msg = intres.getLocalizedMessage("endentity.extendedinfoupgrade", new Float(getVersion())); log.info(msg);/*from w w w . ja v a2 s . c o m*/ if (data.get(SUBJECTDIRATTRIBUTES) == null) { data.put(SUBJECTDIRATTRIBUTES, ""); } if (data.get(MAXFAILEDLOGINATTEMPTS) == null) { setMaxLoginAttempts(DEFAULT_MAXLOGINATTEMPTS); } if (data.get(REMAININGLOGINATTEMPTS) == null) { setRemainingLoginAttempts(DEFAULT_REMAININGLOGINATTEMPTS); } // In EJBCA 4.0.0 we changed the date format if (getVersion() < 3) { final DateFormat oldDateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, Locale.US); final FastDateFormat newDateFormat = FastDateFormat.getInstance("yyyy-MM-dd HH:mm"); try { final String oldCustomStartTime = getCustomData(ExtendedInformation.CUSTOM_STARTTIME); if (!isEmptyOrRelative(oldCustomStartTime)) { // We use an absolute time format, so we need to upgrade final String newCustomStartTime = newDateFormat .format(oldDateFormat.parse(oldCustomStartTime)); setCustomData(ExtendedInformation.CUSTOM_STARTTIME, newCustomStartTime); if (log.isDebugEnabled()) { log.debug("Upgraded " + ExtendedInformation.CUSTOM_STARTTIME + " from \"" + oldCustomStartTime + "\" to \"" + newCustomStartTime + "\" in ExtendedInformation."); } } } catch (ParseException e) { log.error("Unable to upgrade " + ExtendedInformation.CUSTOM_STARTTIME + " in extended user information.", e); } try { final String oldCustomEndTime = getCustomData(ExtendedInformation.CUSTOM_ENDTIME); if (!isEmptyOrRelative(oldCustomEndTime)) { // We use an absolute time format, so we need to upgrade final String newCustomEndTime = newDateFormat.format(oldDateFormat.parse(oldCustomEndTime)); setCustomData(ExtendedInformation.CUSTOM_ENDTIME, newCustomEndTime); if (log.isDebugEnabled()) { log.debug( "Upgraded " + ExtendedInformation.CUSTOM_ENDTIME + " from \"" + oldCustomEndTime + "\" to \"" + newCustomEndTime + "\" in ExtendedInformation."); } } } catch (ParseException e) { log.error("Unable to upgrade " + ExtendedInformation.CUSTOM_ENDTIME + " in extended user information.", e); } } // In 4.0.2 we further specify the storage format by saying that UTC TimeZone is implied instead of local server time if (getVersion() < 4) { final String[] timePatterns = { "yyyy-MM-dd HH:mm" }; final String oldStartTime = getCustomData(ExtendedInformation.CUSTOM_STARTTIME); if (!isEmptyOrRelative(oldStartTime)) { try { final String newStartTime = ValidityDate .formatAsUTC(DateUtils.parseDateStrictly(oldStartTime, timePatterns)); setCustomData(ExtendedInformation.CUSTOM_STARTTIME, newStartTime); if (log.isDebugEnabled()) { log.debug("Upgraded " + ExtendedInformation.CUSTOM_STARTTIME + " from \"" + oldStartTime + "\" to \"" + newStartTime + "\" in EndEntityProfile."); } } catch (ParseException e) { log.error("Unable to upgrade " + ExtendedInformation.CUSTOM_STARTTIME + " to UTC in EndEntityProfile! Manual interaction is required (edit and verify).", e); } } final String oldEndTime = getCustomData(ExtendedInformation.CUSTOM_ENDTIME); if (!isEmptyOrRelative(oldEndTime)) { // We use an absolute time format, so we need to upgrade try { final String newEndTime = ValidityDate .formatAsUTC(DateUtils.parseDateStrictly(oldEndTime, timePatterns)); setCustomData(ExtendedInformation.CUSTOM_ENDTIME, newEndTime); if (log.isDebugEnabled()) { log.debug("Upgraded " + ExtendedInformation.CUSTOM_ENDTIME + " from \"" + oldEndTime + "\" to \"" + newEndTime + "\" in EndEntityProfile."); } } catch (ParseException e) { log.error("Unable to upgrade " + ExtendedInformation.CUSTOM_ENDTIME + " to UTC in EndEntityProfile! Manual interaction is required (edit and verify).", e); } } } data.put(VERSION, new Float(LATEST_VERSION)); } }
From source file:org.ejbca.core.model.ra.raadmin.EndEntityProfile.java
/** Implementation of UpgradableDataHashMap function upgrade. */ public void upgrade() { log.trace(">upgrade"); if (Float.compare(LATEST_VERSION, getVersion()) != 0) { String msg = intres.getLocalizedMessage("ra.eeprofileupgrade", new Float(getVersion())); log.info(msg);//w w w .j ava2 s . com // New version of the class, upgrade if (getVersion() < 1) { @SuppressWarnings("unchecked") ArrayList<Integer> numberarray = (ArrayList<Integer>) data.get(NUMBERARRAY); while (numberarray.size() < 37) { numberarray.add(Integer.valueOf(0)); } data.put(NUMBERARRAY, numberarray); } if (getVersion() < 2) { @SuppressWarnings("unchecked") ArrayList<Integer> numberarray = (ArrayList<Integer>) data.get(NUMBERARRAY); while (numberarray.size() < 39) { numberarray.add(Integer.valueOf(0)); } data.put(NUMBERARRAY, numberarray); addField(AVAILCAS); addField(DEFAULTCA); setRequired(AVAILCAS, 0, true); setRequired(DEFAULTCA, 0, true); } if (getVersion() < 3) { // These fields have been removed in version 8, no need for this upgrade //setNotificationSubject(""); //setNotificationSender(""); //setNotificationMessage(""); } if (getVersion() < 4) { @SuppressWarnings("unchecked") ArrayList<Integer> numberoffields = (ArrayList<Integer>) data.get(NUMBERARRAY); for (int i = numberoffields.size(); i < dataConstants.size(); i++) { numberoffields.add(Integer.valueOf(0)); } data.put(NUMBERARRAY, numberoffields); } // Support for DirectoryName altname field in profile version 5 if (getVersion() < 5) { addField(DnComponents.DIRECTORYNAME); setValue(DnComponents.DIRECTORYNAME, 0, ""); setRequired(DnComponents.DIRECTORYNAME, 0, false); setUse(DnComponents.DIRECTORYNAME, 0, true); setModifyable(DnComponents.DIRECTORYNAME, 0, true); } // Support for Subject Directory Attributes field in profile version 6 if (getVersion() < 6) { @SuppressWarnings("unchecked") ArrayList<Integer> numberoffields = (ArrayList<Integer>) data.get(NUMBERARRAY); for (int i = numberoffields.size(); i < dataConstants.size(); i++) { numberoffields.add(Integer.valueOf(0)); } data.put(NUMBERARRAY, numberoffields); data.put(SUBJECTDIRATTRFIELDORDER, new ArrayList<Integer>()); for (int i = getParameterNumber(DnComponents.DATEOFBIRTH); i <= getParameterNumber( DnComponents.COUNTRYOFRESIDENCE); i++) { addField(getParameter(i)); setValue(getParameter(i), 0, ""); setRequired(getParameter(i), 0, false); setUse(getParameter(i), 0, false); setModifyable(getParameter(i), 0, true); } } // Support for Start Time and End Time field in profile version 7 if (getVersion() < 7) { @SuppressWarnings("unchecked") ArrayList<Integer> numberoffields = (ArrayList<Integer>) data.get(NUMBERARRAY); for (int i = numberoffields.size(); i < dataConstants.size(); i++) { numberoffields.add(Integer.valueOf(0)); } data.put(NUMBERARRAY, numberoffields); addField(STARTTIME); setValue(STARTTIME, 0, ""); setRequired(STARTTIME, 0, false); setUse(STARTTIME, 0, false); setModifyable(STARTTIME, 0, true); addField(ENDTIME); setValue(ENDTIME, 0, ""); setRequired(ENDTIME, 0, false); setUse(ENDTIME, 0, false); setModifyable(ENDTIME, 0, true); } // Notifications is now a more general mechanism in version 8 if (getVersion() < 8) { log.debug("Upgrading User Notifications"); if (data.get(UserNotification.NOTIFICATIONSENDER) != null) { UserNotification not = new UserNotification(); not.setNotificationSender((String) data.get(UserNotification.NOTIFICATIONSENDER)); if (data.get(UserNotification.NOTIFICATIONSUBJECT) != null) { not.setNotificationSubject((String) data.get(UserNotification.NOTIFICATIONSUBJECT)); } if (data.get(UserNotification.NOTIFICATIONMESSAGE) != null) { not.setNotificationMessage((String) data.get(UserNotification.NOTIFICATIONMESSAGE)); } // Add the statuschanges we used to send notifications about String events = UserNotification.EVENTS_EDITUSER; not.setNotificationEvents(events); // The old recipients where always the user not.setNotificationRecipient(UserNotification.RCPT_USER); addUserNotification(not); } } // Support for allowed requests in profile version 9 if (getVersion() < 9) { @SuppressWarnings("unchecked") ArrayList<Integer> numberoffields = (ArrayList<Integer>) data.get(NUMBERARRAY); for (int i = numberoffields.size(); i < dataConstants.size(); i++) { numberoffields.add(Integer.valueOf(0)); } data.put(NUMBERARRAY, numberoffields); addField(ALLOWEDREQUESTS); setValue(ALLOWEDREQUESTS, 0, ""); setRequired(ALLOWEDREQUESTS, 0, false); setUse(ALLOWEDREQUESTS, 0, false); setModifyable(ALLOWEDREQUESTS, 0, true); } // Support for merging DN from WS-API with default values in profile, in profile version 10 if (getVersion() < 10) { setAllowMergeDnWebServices(false); } // Support for issuance revocation status in profile version 11 if (getVersion() < 11) { setRequired(ISSUANCEREVOCATIONREASON, 0, false); setUse(ISSUANCEREVOCATIONREASON, 0, false); setModifyable(ISSUANCEREVOCATIONREASON, 0, true); setValue(ISSUANCEREVOCATIONREASON, 0, "" + RevokedCertInfo.NOT_REVOKED); setRequired(CARDNUMBER, 0, false); setUse(CARDNUMBER, 0, false); setModifyable(CARDNUMBER, 0, true); } // Support for maximum number of failed login attempts in profile version 12 if (getVersion() < 12) { setRequired(MAXFAILEDLOGINS, 0, false); setUse(MAXFAILEDLOGINS, 0, false); setModifyable(MAXFAILEDLOGINS, 0, true); setValue(MAXFAILEDLOGINS, 0, Integer.toString(ExtendedInformation.DEFAULT_MAXLOGINATTEMPTS)); } /* In EJBCA 4.0.0 we changed the date format to ISO 8601. * In the Admin GUI the example was: * DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, ejbcawebbean.getLocale()) * but the only absolute format that could have worked is the same enforced by the * doesUserFullfillEndEntityProfile check and this is what need to upgrade from: * DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, Locale.US) */ if (getVersion() < 13) { final DateFormat oldDateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, Locale.US); final FastDateFormat newDateFormat = FastDateFormat.getInstance("yyyy-MM-dd HH:mm"); try { final String oldStartTime = getValue(STARTTIME, 0); if (!isEmptyOrRelative(oldStartTime)) { // We use an absolute time format, so we need to upgrade final String newStartTime = newDateFormat.format(oldDateFormat.parse(oldStartTime)); setValue(STARTTIME, 0, newStartTime); if (log.isDebugEnabled()) { log.debug("Upgraded " + STARTTIME + " from \"" + oldStartTime + "\" to \"" + newStartTime + "\" in EndEntityProfile."); } } } catch (ParseException e) { log.error("Unable to upgrade " + STARTTIME + " in EndEntityProfile! Manual interaction is required (edit and verify).", e); } try { final String oldEndTime = getValue(ENDTIME, 0); if (!isEmptyOrRelative(oldEndTime)) { // We use an absolute time format, so we need to upgrade final String newEndTime = newDateFormat.format(oldDateFormat.parse(oldEndTime)); setValue(ENDTIME, 0, newEndTime); if (log.isDebugEnabled()) { log.debug("Upgraded " + ENDTIME + " from \"" + oldEndTime + "\" to \"" + newEndTime + "\" in EndEntityProfile."); } } } catch (ParseException e) { log.error("Unable to upgrade " + ENDTIME + " in EndEntityProfile! Manual interaction is required (edit and verify).", e); } } /* * In version 13 we converted some dates to the "yyyy-MM-dd HH:mm" format using default Locale. * These needs to be converted to the same format but should be stored in UTC, so we always know what the times are. */ if (getVersion() < 14) { final String[] timePatterns = { "yyyy-MM-dd HH:mm" }; final String oldStartTime = getValue(STARTTIME, 0); if (!isEmptyOrRelative(oldStartTime)) { try { final String newStartTime = ValidityDate .formatAsUTC(DateUtils.parseDateStrictly(oldStartTime, timePatterns)); setValue(STARTTIME, 0, newStartTime); if (log.isDebugEnabled()) { log.debug("Upgraded " + STARTTIME + " from \"" + oldStartTime + "\" to \"" + newStartTime + "\" in EndEntityProfile."); } } catch (ParseException e) { log.error("Unable to upgrade " + STARTTIME + " to UTC in EndEntityProfile! Manual interaction is required (edit and verify).", e); } } final String oldEndTime = getValue(ENDTIME, 0); if (!isEmptyOrRelative(oldEndTime)) { // We use an absolute time format, so we need to upgrade try { final String newEndTime = ValidityDate .formatAsUTC(DateUtils.parseDateStrictly(oldEndTime, timePatterns)); setValue(ENDTIME, 0, newEndTime); if (log.isDebugEnabled()) { log.debug("Upgraded " + ENDTIME + " from \"" + oldEndTime + "\" to \"" + newEndTime + "\" in EndEntityProfile."); } } catch (ParseException e) { log.error("Unable to upgrade " + ENDTIME + " to UTC in EndEntityProfile! Manual interaction is required (edit and verify).", e); } } } // Finally, update the version stored in the map to the current version data.put(VERSION, new Float(LATEST_VERSION)); } log.trace("<upgrade"); }