Example usage for org.apache.commons.lang3.time DateUtils parseDate

List of usage examples for org.apache.commons.lang3.time DateUtils parseDate

Introduction

In this page you can find the example usage for org.apache.commons.lang3.time DateUtils parseDate.

Prototype

public static Date parseDate(final String str, final String... parsePatterns) throws ParseException 

Source Link

Document

Parses a string representing a date by trying a variety of different parsers.

The parse will try each parse pattern in turn.

Usage

From source file:org.activiti5.standalone.calendar.DurationHelperTest.java

private Calendar parseCalendarWithOffset(String str, TimeZone timeZone) throws Exception {

    Calendar cal = Calendar.getInstance();
    cal.setTime(DateUtils.parseDate(str, "yyyyMMdd-HH:mm:ssZZ"));
    return cal;/*from   www. j  av  a  2  s. c  o m*/
}

From source file:org.aliuge.crawler.extractor.selector.DateElementCssSelector.java

@Override
public Date getContent() {
    try {// ww w .  j  a  v  a2s  .co  m
        if (null != this.date && !newDoc) {
            return date;
        }
        if (null != document) {
            Elements elements = super.document.select(value);
            if (elements.isEmpty())
                return null;
            String temp;
            switch ($Attr) {
            case text:
                temp = getExtractText(elements);
                break;
            default:
                temp = getExtractAttr(elements, attr);
                break;
            }
            if (StringUtils.isNotBlank(temp)) {
                try {
                    this.date = DateUtils.parseDate(temp, patterns.toArray(new String[0]));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            newDoc = false;
            return this.date;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.apache.syncope.core.misc.DataFormat.java

public static Date parseDate(final String source) throws ParseException {
    return DateUtils.parseDate(source, SyncopeConstants.DATE_PATTERNS);
}

From source file:org.cloud.mblog.utils.DateFormatTag.java

private String getOffsetTime(String newTime) {
    try {/*from  w w  w. j  ava 2 s  .c o  m*/
        Date lastPost = DateUtils.parseDate(newTime, "yyyy-MM-dd HH:mm:ss.SSS");
        Date now = new Date();
        long duration = (now.getTime() - lastPost.getTime()) / 1000;
        if (duration < 60) {
            return duration + "?";
        } else if (duration < 3600) {
            return duration / 60 + "?";
        } else if (duration < 86400) {
            return duration / 3600 + "??";
        } else {
            Date zeroTime = DateUtils.truncate(now, Calendar.DATE);
            long offset_day = (zeroTime.getTime() - lastPost.getTime()) / 1000 / 3600 / 24;
            if (offset_day < 1) {
                return "";
            } else if (offset_day < 2) {
                return "?";
            } else if (offset_day < 365) {
                return offset_day + "?";
            } else {
                return offset_day / 365 + "?";
            }
        }
    } catch (ParseException e) {
        logger.error("error", e);
    }
    return "";
}

From source file:org.eclipse.winery.librarytests.DateTest.java

public static void main(String[] args) throws ParseException {
    // In case the following line is commented, this method throws a ParseException
    Locale.setDefault(Locale.ENGLISH);
    String modified = "Fri, 23 Mar 2012 11:04:56 GMT";
    Date modifiedDate = DateUtils.parseDate(modified, org.apache.http.impl.cookie.DateUtils.DEFAULT_PATTERNS);
    System.out.println(modifiedDate);
}

From source file:org.eclipse.winery.repository.backend.BackendUtils.java

/**
 * @return true if given fileDate is newer then the modified date (or
 *         modified is null)//from   w w  w .  j a v a  2 s . c  o m
 */
public static boolean isFileNewerThanModifiedDate(long millis, String modified) {
    if (modified == null) {
        return true;
    }

    Date modifiedDate = null;

    assert (Locale.getDefault() == Locale.ENGLISH);
    try {
        modifiedDate = DateUtils.parseDate(modified, org.apache.http.impl.cookie.DateUtils.DEFAULT_PATTERNS);
    } catch (ParseException e) {
        BackendUtils.logger.error(e.getMessage(), e);
    }

    if (modifiedDate != null) {
        // modifiedDate does not carry milliseconds, but fileDate does
        // therefore we have to do a range-based comparison
        if ((millis - modifiedDate.getTime()) < DateUtils.MILLIS_PER_SECOND) {
            return false;
        }
    }

    return true;
}

From source file:org.faster.util.DateTimes.java

/**
 * ???/*  w  ww .j  a va 2 s  . co  m*/
 */
public static Date parseStringToDate(String str, String pattern) {
    try {
        return DateUtils.parseDate(str, pattern);
    } catch (ParseException e) {
        throw new IllegalArgumentException("Date[" + str + "] doesn't match pattern[" + pattern + "].", e);
    }
}

From source file:org.faster.util.DateTimes.java

/**
 * ???//from www  .  ja  v  a  2s .co  m
 *
 * @param str
 *            ??
 * @param patterns
 *            2010-05-26 16:11:00
 * @return 
 */
public static Date parseStringToDate(String str, String[] patterns) {
    try {
        return DateUtils.parseDate(str, patterns);
    } catch (ParseException e) {
        String pattern = StringUtils.join(patterns, ",");
        throw new IllegalArgumentException("Date[" + str + "] doesn't match pattern[" + pattern + "].", e);
    }
}

From source file:org.finra.herd.app.AbstractAppTest.java

/**
 * Retrieves the user from the current spring security context and asserts that each of the properties of the user matches the given expected values.
 * Asserts that the principal stored in the current security context user is an instance of {@link SecurityUserWrapper}.
 *
 * @param expectedUserId the expected user Id.
 * @param expectedFirstName the expected first name.
 * @param expectedLastName the expected last name.
 * @param expectedEmail the expected e-mail.
 * @param expectedRoles the expected roles.
 * @param expectedSessionInitTime the expected session init time.
 * @param expectedFunctions the expected functions.
 *
 * @throws Exception if any errors were encountered.
 *///w  w  w. ja va 2s .c  o m
protected void validateHttpHeaderApplicationUser(String expectedUserId, String expectedFirstName,
        String expectedLastName, String expectedEmail, Set<String> expectedRoles,
        String expectedSessionInitTime, String[] expectedFunctions,
        Set<NamespaceAuthorization> expectedNamespaceAuthorizations) throws Exception {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    assertNotNull(authentication);

    Object principal = authentication.getPrincipal();
    assertNotNull("expected principal to be not null, but was null", principal);
    assertTrue(
            "expected principal to be an instance of " + SecurityUserWrapper.class
                    + ", but was an instance of  " + principal.getClass(),
            principal instanceof SecurityUserWrapper);
    SecurityUserWrapper user = (SecurityUserWrapper) principal;
    ApplicationUser applicationUser = user.getApplicationUser();
    assertEquals(expectedUserId, applicationUser.getUserId());
    assertEquals(expectedFirstName, applicationUser.getFirstName());
    assertEquals(expectedLastName, applicationUser.getLastName());
    assertEquals(expectedEmail, applicationUser.getEmail());

    assertEquals(expectedRoles, applicationUser.getRoles());
    if (StringUtils.isNotBlank(expectedSessionInitTime)) {
        assertEquals(
                DateUtils.parseDate(expectedSessionInitTime,
                        HttpHeaderApplicationUserBuilder.CALENDAR_PATTERNS),
                applicationUser.getSessionInitTime());
    }

    assertNotNull(applicationUser.getSessionId());

    assertEquals(HttpHeaderApplicationUserBuilder.class, applicationUser.getGeneratedByClass());

    // Validate functions.
    if (expectedFunctions != null) {
        Set<String> functions = new HashSet<>();
        for (GrantedAuthority grantedAuthority : user.getAuthorities()) {
            functions.add(grantedAuthority.getAuthority());
        }

        for (String expectedFunction : expectedFunctions) {
            assertTrue(functions.contains(expectedFunction));
        }
    }

    // Validate namespace authorizations.
    if (expectedNamespaceAuthorizations != null) {
        assertEquals(expectedNamespaceAuthorizations, applicationUser.getNamespaceAuthorizations());
    }
}

From source file:org.finra.herd.app.security.HttpHeaderApplicationUserBuilder.java

/**
 * Gets an HTTP header date value from the map of headers for the specified header name. If the header value is blank or can't be converted to a Date, null
 * will be returned in its place./*  w  w  w  .ja va  2s.c om*/
 *
 * @param headerName the HTTP header name to get.
 * @param httpHeaders the HTTP headers.
 *
 * @return the HTTP header value.
 */
public Date getHeaderValueDate(String headerName, Map<String, String> httpHeaders) {
    // Default the return value to null.
    Date dateValue = null;

    // Get the string value for the header name.
    String stringValue = getHeaderValueString(headerName, httpHeaders);

    // Try to convert it to a Date if possible
    if (stringValue != null) {
        try {
            dateValue = DateUtils.parseDate(stringValue, CALENDAR_PATTERNS);
        } catch (Exception ex) {
            // The value couldn't be converted to a Date so leave the dateValue null.
            dateValue = null;
        }
    }

    // Return the calendar value.
    return dateValue;
}