Search in sources :

Example 21 with DateTimeParseException

use of java.time.format.DateTimeParseException in project knime-core by knime.

the class DialogComponentDateTimeSelection method updateSpinnerFormat.

/**
 * Allows the user to determine by himself whether milliseconds shall be used or not. Checks if the text in the text
 * field of the time spinner contains milliseconds or not and updates the format of the spinner editor accordingly.
 */
private void updateSpinnerFormat() {
    final JFormattedTextField field = (JFormattedTextField) m_editor.getComponent(0);
    try {
        DateTimeFormatter.ofPattern(TIME_FORMAT_WITH_MS).parse(field.getText());
        if (!m_useMillis) {
            setUseMillis(true);
            ((DateFormatter) m_editor.getTextField().getFormatter()).setFormat(new SimpleDateFormat(TIME_FORMAT_WITH_MS, Locale.getDefault()));
            updateModel();
        }
    } catch (final DateTimeParseException iae) {
        try {
            DateTimeFormatter.ofPattern(TIME_FORMAT_WITHOUT_MS).parse(field.getText());
            if (m_useMillis) {
                setUseMillis(false);
                ((DateFormatter) m_editor.getTextField().getFormatter()).setFormat(new SimpleDateFormat(TIME_FORMAT_WITHOUT_MS, Locale.getDefault()));
                updateModel();
            }
        } catch (final DateTimeParseException iae2) {
        }
    }
}
Also used : DateTimeParseException(java.time.format.DateTimeParseException) JFormattedTextField(javax.swing.JFormattedTextField) DateFormatter(javax.swing.text.DateFormatter) SimpleDateFormat(java.text.SimpleDateFormat)

Example 22 with DateTimeParseException

use of java.time.format.DateTimeParseException in project grakn by graknlabs.

the class DateMacro method convertDateFormat.

private String convertDateFormat(String originalDate, String originalFormat) {
    originalFormat = removeQuotes(originalFormat);
    try {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(originalFormat);
        TemporalAccessor parsedDate = formatter.parseBest(originalDate, LocalDateTime::from, LocalDate::from, LocalTime::from);
        return extractLocalDateTime(parsedDate).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
    } catch (IllegalArgumentException e) {
        throw GraqlQueryException.cannotParseDateFormat(originalFormat);
    } catch (DateTimeParseException e) {
        throw GraqlQueryException.cannotParseDateString(originalDate, originalFormat, e);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) DateTimeParseException(java.time.format.DateTimeParseException) TemporalAccessor(java.time.temporal.TemporalAccessor) LocalTime(java.time.LocalTime) DateTimeFormatter(java.time.format.DateTimeFormatter) LocalDate(java.time.LocalDate)

Example 23 with DateTimeParseException

use of java.time.format.DateTimeParseException in project cxf by apache.

the class DefaultConditionsProvider method getConditions.

/**
 * Get a ConditionsBean object.
 */
public ConditionsBean getConditions(TokenProviderParameters providerParameters) {
    ConditionsBean conditions = new ConditionsBean();
    Lifetime tokenLifetime = providerParameters.getTokenRequirements().getLifetime();
    if (lifetime > 0) {
        if (acceptClientLifetime && tokenLifetime != null && tokenLifetime.getCreated() != null && tokenLifetime.getExpires() != null) {
            Instant creationTime = null;
            Instant expirationTime = null;
            try {
                creationTime = ZonedDateTime.parse(tokenLifetime.getCreated()).toInstant();
                expirationTime = ZonedDateTime.parse(tokenLifetime.getExpires()).toInstant();
            } catch (DateTimeParseException ex) {
                LOG.fine("Error in parsing Timestamp Created or Expiration Strings");
                throw new STSException("Error in parsing Timestamp Created or Expiration Strings", STSException.INVALID_TIME);
            }
            // Check to see if the created time is in the future
            Instant validCreation = Instant.now();
            if (futureTimeToLive > 0) {
                validCreation = validCreation.plusSeconds(futureTimeToLive);
            }
            if (creationTime.isAfter(validCreation)) {
                LOG.fine("The Created Time is too far in the future");
                throw new STSException("The Created Time is too far in the future", STSException.INVALID_TIME);
            }
            long requestedLifetime = Duration.between(creationTime, expirationTime).getSeconds();
            if (requestedLifetime > getMaxLifetime()) {
                StringBuilder sb = new StringBuilder();
                sb.append("Requested lifetime [").append(requestedLifetime);
                sb.append(" sec] exceed configured maximum lifetime [").append(getMaxLifetime());
                sb.append(" sec]");
                LOG.warning(sb.toString());
                if (isFailLifetimeExceedance()) {
                    throw new STSException("Requested lifetime exceeds maximum lifetime", STSException.INVALID_TIME);
                }
                expirationTime = creationTime.plusSeconds(getMaxLifetime());
            }
            conditions.setNotAfter(expirationTime);
            conditions.setNotBefore(creationTime);
        } else {
            conditions.setTokenPeriodSeconds(lifetime);
        }
    } else {
        conditions.setTokenPeriodMinutes(5);
    }
    List<AudienceRestrictionBean> audienceRestrictions = createAudienceRestrictions(providerParameters);
    if (audienceRestrictions != null && !audienceRestrictions.isEmpty()) {
        conditions.setAudienceRestrictions(audienceRestrictions);
    }
    return conditions;
}
Also used : DateTimeParseException(java.time.format.DateTimeParseException) Lifetime(org.apache.cxf.sts.request.Lifetime) AudienceRestrictionBean(org.apache.wss4j.common.saml.bean.AudienceRestrictionBean) Instant(java.time.Instant) ConditionsBean(org.apache.wss4j.common.saml.bean.ConditionsBean) STSException(org.apache.cxf.ws.security.sts.provider.STSException)

Example 24 with DateTimeParseException

use of java.time.format.DateTimeParseException in project cxf by apache.

the class SecurityToken method processLifeTime.

/**
 * @param lifetimeElem
 * @throws TrustException
 */
private void processLifeTime(Element lifetimeElem) {
    try {
        Element createdElem = DOMUtils.getFirstChildWithName(lifetimeElem, WSS4JConstants.WSU_NS, WSS4JConstants.CREATED_LN);
        this.created = ZonedDateTime.parse(DOMUtils.getContent(createdElem)).toInstant();
        Element expiresElem = DOMUtils.getFirstChildWithName(lifetimeElem, WSS4JConstants.WSU_NS, WSS4JConstants.EXPIRES_LN);
        this.expires = ZonedDateTime.parse(DOMUtils.getContent(expiresElem)).toInstant();
    } catch (DateTimeParseException e) {
    // shouldn't happen
    }
}
Also used : DateTimeParseException(java.time.format.DateTimeParseException) Element(org.w3c.dom.Element)

Example 25 with DateTimeParseException

use of java.time.format.DateTimeParseException in project quickstart by wildfly.

the class DateService method showDaysUntil.

@GET
@Path("daysuntil/{targetdate}")
@Produces(MediaType.TEXT_PLAIN)
public long showDaysUntil(@PathParam("targetdate") String targetDate) {
    DateLogger.LOGGER.logDaysUntilRequest(targetDate);
    final long days;
    try {
        final LocalDate date = LocalDate.parse(targetDate, DateTimeFormatter.ISO_DATE);
        days = ChronoUnit.DAYS.between(LocalDate.now(), date);
    } catch (DateTimeParseException ex) {
        // ** DISCLAIMER **
        // This example is contrived and overly verbose for the purposes of showing the
        // different logging methods. It's generally not recommended to recreate exceptions
        // or log exceptions that are being thrown.
        // create localized ParseException using method from bundle with details from ex
        DateTimeParseException nex = DateExceptionsBundle.EXCEPTIONS.targetDateStringDidntParse(targetDate, ex.getParsedString(), ex.getErrorIndex());
        // log a message using nex as the cause
        DateLogger.LOGGER.logStringCouldntParseAsDate(targetDate, nex);
        // throw a WebApplicationException (400) with the localized message from nex
        throw new WebApplicationException(Response.status(400).entity(nex.getLocalizedMessage()).type(MediaType.TEXT_PLAIN).build());
    }
    return days;
}
Also used : DateTimeParseException(java.time.format.DateTimeParseException) WebApplicationException(javax.ws.rs.WebApplicationException) LocalDate(java.time.LocalDate) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

DateTimeParseException (java.time.format.DateTimeParseException)51 DateTimeFormatter (java.time.format.DateTimeFormatter)17 LocalDate (java.time.LocalDate)15 Test (org.testng.annotations.Test)14 TemporalAccessor (java.time.temporal.TemporalAccessor)13 LocalDateTime (java.time.LocalDateTime)11 DateTimeFormatterBuilder (java.time.format.DateTimeFormatterBuilder)10 Instant (java.time.Instant)7 Duration (java.time.Duration)5 ZonedDateTime (java.time.ZonedDateTime)5 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)5 Period (java.time.Period)4 LocalTime (java.time.LocalTime)3 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)3 DataflowException (edu.uci.ics.texera.api.exception.DataflowException)2 ParseException (java.text.ParseException)2 DateTimeException (java.time.DateTimeException)2 ZoneId (java.time.ZoneId)2 ResolverStyle (java.time.format.ResolverStyle)2 Temporal (java.time.temporal.Temporal)2