Example usage for org.apache.commons.lang.time DateFormatUtils format

List of usage examples for org.apache.commons.lang.time DateFormatUtils format

Introduction

In this page you can find the example usage for org.apache.commons.lang.time DateFormatUtils format.

Prototype

public static String format(Date date, String pattern, Locale locale) 

Source Link

Document

Formats a date/time into a specific pattern in a locale.

Usage

From source file:org.b3log.solo.model.feed.atom.Entry.java

@Override
public String toString() {
    final StringBuilder stringBuilder = new StringBuilder();

    stringBuilder.append(START_ENTRY_ELEMENT).append(START_TITLE_ELEMENT);
    stringBuilder.append(title);//from w w w.  ja  v a 2 s.c  om
    stringBuilder.append(END_TITLE_ELEMENT);

    stringBuilder.append(START_AUTHOR_ELEMENT);
    stringBuilder.append(START_NAME_ELEMENT);
    stringBuilder.append(author);
    stringBuilder.append(END_NAME_ELEMENT);
    stringBuilder.append(START_URI_ELEMENT);
    stringBuilder.append(uri);
    stringBuilder.append(END_URI_ELEMENT);
    stringBuilder.append(END_AUTHOR_ELEMENT);

    for (final Category category : categories) {
        stringBuilder.append(category.toString());
    }

    stringBuilder.append(LINK_ELEMENT.replace(LINK_VARIABLE, link));

    stringBuilder.append(START_ID_ELEMENT);
    stringBuilder.append(id);
    stringBuilder.append(END_ID_ELEMENT);

    stringBuilder.append(START_UPDATED_ELEMENT);
    stringBuilder.append(DateFormatUtils.format(// using ISO-8601 instead of RFC-3339
            updated, DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern(),
            TimeZone.getTimeZone(Feed.TIME_ZONE_ID)));
    stringBuilder.append(END_UPDATED_ELEMENT);

    stringBuilder.append(START_SUMMARY_ELEMENT);
    stringBuilder.append(summary);
    stringBuilder.append(END_SUMMARY_ELEMENT);

    stringBuilder.append(END_ENTRY_ELEMENT);

    return stringBuilder.toString();
}

From source file:org.b3log.solo.model.feed.atom.Feed.java

@Override
public String toString() {
    final StringBuilder stringBuilder = new StringBuilder();

    stringBuilder.append(START_DOCUMENT);
    stringBuilder.append(START_FEED_ELEMENT);

    stringBuilder.append(START_ID_ELEMENT);
    stringBuilder.append(id);//w ww  . j av  a  2 s .  c  o  m
    stringBuilder.append(END_ID_ELEMENT);

    stringBuilder.append(START_TITLE_ELEMENT);
    stringBuilder.append(title);
    stringBuilder.append(END_TITLE_ELEMENT);

    stringBuilder.append(START_SUBTITLE_ELEMENT);
    stringBuilder.append(subtitle);
    stringBuilder.append(END_SUBTITLE_ELEMENT);

    stringBuilder.append(START_UPDATED_ELEMENT);
    stringBuilder.append(DateFormatUtils.format(// using ISO-8601 instead of RFC-3339
            updated, DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern(),
            TimeZone.getTimeZone(TIME_ZONE_ID)));
    stringBuilder.append(END_UPDATED_ELEMENT);

    stringBuilder.append(START_AUTHOR_ELEMENT);
    stringBuilder.append(START_NAME_ELEMENT);
    stringBuilder.append(author);
    stringBuilder.append(END_NAME_ELEMENT);
    stringBuilder.append(END_AUTHOR_ELEMENT);

    stringBuilder.append(LINK_ELEMENT.replace(LINK_VARIABLE, link));

    for (final Entry entry : entries) {
        stringBuilder.append(entry.toString());
    }

    stringBuilder.append(END_FEED_ELEMENT);

    return stringBuilder.toString();
}

From source file:org.b3log.solo.model.feed.rss.Item.java

@Override
public String toString() {
    final StringBuilder stringBuilder = new StringBuilder();

    stringBuilder.append("<item>").append(START_TITLE_ELEMENT);
    stringBuilder.append(title);//from  w w  w.j  a  v  a  2s.  c  o m
    stringBuilder.append(END_TITLE_ELEMENT);

    stringBuilder.append(START_LINK_ELEMENT);
    stringBuilder.append(link);
    stringBuilder.append(END_LINK_ELEMENT);

    stringBuilder.append(START_DESCRIPTION_ELEMENT);
    stringBuilder.append(description);
    stringBuilder.append(END_DESCRIPTION_ELEMENT);

    stringBuilder.append(START_AUTHOR_ELEMENT);
    stringBuilder.append(author);
    stringBuilder.append(END_AUTHOR_ELEMENT);

    stringBuilder.append(START_GUID_ELEMENT);
    stringBuilder.append(guid);
    stringBuilder.append(END_GUID_ELEMENT);

    for (final Category category : categories) {
        stringBuilder.append(category.toString());
    }

    stringBuilder.append(START_PUB_DATE_ELEMENT);
    stringBuilder.append(DateFormatUtils.format(pubDate, "EEE, dd MMM yyyy HH:mm:ss Z", Locale.US));
    stringBuilder.append(END_PUB_DATE_ELEMENT).append("</item>");

    return stringBuilder.toString();
}

From source file:org.b3log.solo.model.rss.Item.java

@Override
public String toString() {
    final StringBuilder stringBuilder = new StringBuilder();

    stringBuilder.append("<item>").append(START_TITLE_ELEMENT);
    stringBuilder.append(StringEscapeUtils.escapeXml(title));
    stringBuilder.append(END_TITLE_ELEMENT);

    stringBuilder.append(START_LINK_ELEMENT);
    stringBuilder.append(StringEscapeUtils.escapeXml(link));
    stringBuilder.append(END_LINK_ELEMENT);

    stringBuilder.append(START_DESCRIPTION_ELEMENT);
    stringBuilder.append(StringEscapeUtils.escapeXml(description));
    stringBuilder.append(END_DESCRIPTION_ELEMENT);

    stringBuilder.append(START_AUTHOR_ELEMENT);
    stringBuilder.append(StringEscapeUtils.escapeXml(author));
    stringBuilder.append(END_AUTHOR_ELEMENT);

    stringBuilder.append(START_GUID_ELEMENT);
    stringBuilder.append(StringEscapeUtils.escapeXml(guid));
    stringBuilder.append(END_GUID_ELEMENT);

    for (final Category category : categories) {
        stringBuilder.append(category.toString());
    }// w  w w  .  j av  a 2  s.co m

    stringBuilder.append(START_PUB_DATE_ELEMENT);
    stringBuilder.append(DateFormatUtils.format(pubDate, "EEE, dd MMM yyyy HH:mm:ss Z", Locale.US));
    stringBuilder.append(END_PUB_DATE_ELEMENT).append("</item>");

    return stringBuilder.toString();
}

From source file:org.b3log.symphony.processor.JournalProcessor.java

/**
 * Generates today's journal section./*  w  w w .  j  a v a  2  s .co  m*/
 *
 * @param context the specified context
 * @param request the specified request
 * @param response the specified response
 * @throws IOException io exception
 * @throws ServletException servlet exception
 */
@RequestProcessing(value = "/journal/gen/section", method = HTTPRequestMethod.GET)
@Before(adviceClass = StopwatchStartAdvice.class)
@After(adviceClass = StopwatchEndAdvice.class)
public void genSection(final HTTPRequestContext context, final HttpServletRequest request,
        final HttpServletResponse response) throws IOException, ServletException {
    final String key = Symphonys.get("keyOfSymphony");
    if (!key.equals(request.getParameter("key"))) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);

        return;
    }

    context.renderJSON();

    if (journalQueryService.hasSectionToday()) {
        return;
    }

    try {

        final JSONObject admin = userQueryService.getSA();

        final JSONObject section = new JSONObject();
        final String title = DateFormatUtils.format(new Date(), "yyyyMMdd E", Locale.US);
        section.put(Article.ARTICLE_TITLE, title);
        section.put(Article.ARTICLE_TAGS, ",");
        section.put(Article.ARTICLE_CONTENT, "");
        section.put(Article.ARTICLE_EDITOR_TYPE, 0);
        section.put(Article.ARTICLE_AUTHOR_EMAIL, admin.optString(User.USER_EMAIL));
        section.put(Article.ARTICLE_AUTHOR_ID, admin.optString(Keys.OBJECT_ID));
        section.put(Article.ARTICLE_TYPE, Article.ARTICLE_TYPE_C_JOURNAL_SECTION);

        articleMgmtService.addArticle(section);
        archiveMgmtService.refreshTeams(System.currentTimeMillis());

        context.renderTrueResult();
    } catch (final ServiceException e) {
        LOGGER.log(Level.ERROR, "Generates section failed", e);
    }
}

From source file:org.extremecomponents.util.ExtremeUtils.java

/**
 * // ww  w. ja  va2  s .  c om
 * @param parse
 * @param format
 * @param value
 * @return
 */
public static String formatDate(String parse, String format, Object value, Locale locale) {
    if (value == null) {
        return null;
    }

    if (StringUtils.isBlank(format)) {
        String valueAsString = value.toString();
        logger.error("The format was not defined for date [" + valueAsString + "].");
        return valueAsString;
    }

    Date date = null;
    if (value instanceof Date) {
        date = (Date) value;
    } else {
        String valueAsString = value.toString();

        if (StringUtils.isBlank(valueAsString)) {
            return valueAsString;
        }

        if (StringUtils.isBlank(parse)) {
            logger.error("The parse was not defined for date String [" + valueAsString + "].");
            return valueAsString;
        }

        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(parse, locale);
            date = simpleDateFormat.parse(valueAsString);
        } catch (Exception e) {
            logger.error("The parse was incorrectly defined for date String [" + valueAsString + "].");
            return valueAsString;
        }
    }

    return DateFormatUtils.format(date, format, locale);
}

From source file:org.failearly.dataz.internal.template.generator.DateEncoderTest.java

@Test
public void now__should_be_converted_to_today() throws Exception {
    // arrange / given
    final String format = "dd-MM-yyyy";
    final String timezone = "GMT+1";
    final long now = System.currentTimeMillis();
    final DateEncoder encoder = new DateEncoder(format, timezone);

    // act / when
    final String date = encoder.toDateString(now);

    // assert / then
    assertThat(date, is(DateFormatUtils.format(now, format, TimeZone.getTimeZone(timezone))));
}

From source file:org.jmesa.core.filter.DateFilterMatcher.java

@Override
public boolean evaluate(Object itemValue, String filterValue) {

    if (itemValue == null) {
        return false;
    }/*  www.  j  ava2s .c  o  m*/

    String pattern = getPattern();
    if (pattern == null) {
        logger.debug("The filter (value " + filterValue + ") is trying to match against a date column using "
                + "the DateFilterMatcher, but there is no pattern defined. You need to register a DateFilterMatcher "
                + "to be able to filter against this column.");
        return false;
    }

    Locale locale = null;

    WebContext webContext = getWebContext();
    if (webContext != null) {
        locale = webContext.getLocale();
    }

    if (locale != null) {
        itemValue = DateFormatUtils.format((Date) itemValue, pattern, locale);
    } else {
        itemValue = DateFormatUtils.format((Date) itemValue, pattern);
    }

    String item = String.valueOf(itemValue);
    String filter = String.valueOf(filterValue);
    if (StringUtils.contains(item, filter)) {
        return true;
    }

    return false;
}

From source file:org.jmesa.view.editor.DateCellEditor.java

/**
 * Get the formatted date value based on the pattern set.
 *//* w  ww. j  a  va  2  s. c o  m*/
@Override
public Object getValue(Object item, String property, int rowcount) {

    Object itemValue = null;

    try {
        itemValue = ItemUtils.getItemValue(item, property);
        if (itemValue == null || StringUtils.isBlank(String.valueOf(itemValue))) {
            return null;
        }

        Locale locale = getWebContext().getLocale();
        itemValue = DateFormatUtils.format((Date) itemValue, getPattern(), locale);
    } catch (Exception e) {
        logger.warn("Could not process date editor with property " + property, e);
    }

    return itemValue;
}

From source file:org.jtalks.jcommune.plugin.questionsandanswers.controller.QuestionsAndAnswersController.java

/**
 * Writes icon to response and set apropriate response headers
 *
 * @param request HttpServletRequest/*w  w w . j  av a 2 s.c  o  m*/
 * @param response HttpServletResponse
 * @param iconPath path to icon to be writed
 *
 * @throws IOException if icon not found
 */
private void processIconRequest(HttpServletRequest request, HttpServletResponse response, String iconPath)
        throws IOException {
    if (request.getHeader(IF_MODIFIED_SINCE_HEADER) != null) {
        response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        return;
    }
    byte[] icon = ByteStreams.toByteArray(getClass().getResourceAsStream(iconPath));
    response.setContentType("image/png");
    response.setContentLength(icon.length);
    response.getOutputStream().write(icon);
    response.setHeader("Pragma", "public");
    response.setHeader("Cache-Control", "public");
    response.addHeader("Cache-Control", "must-revalidate");
    response.addHeader("Cache-Control", "max-age=0");
    String formattedDateExpires = DateFormatUtils.format(new Date(), HTTP_HEADER_DATETIME_PATTERN, Locale.US);
    response.setHeader("Expires", formattedDateExpires);
    Date lastModificationDate = new Date(0);
    response.setHeader("Last-Modified",
            DateFormatUtils.format(lastModificationDate, HTTP_HEADER_DATETIME_PATTERN, Locale.US));
}