Example usage for java.text SimpleDateFormat setDateFormatSymbols

List of usage examples for java.text SimpleDateFormat setDateFormatSymbols

Introduction

In this page you can find the example usage for java.text SimpleDateFormat setDateFormatSymbols.

Prototype

public void setDateFormatSymbols(DateFormatSymbols newFormatSymbols) 

Source Link

Document

Sets the date and time format symbols of this date format.

Usage

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    SimpleDateFormat sdf = new SimpleDateFormat();
    DateFormatSymbols dfs = sdf.getDateFormatSymbols();

    String era[] = { "BCE", "CE" };
    dfs.setEras(era);/*from   www .j  a va  2 s  .c om*/
    sdf.setDateFormatSymbols(dfs);

    sdf.applyPattern("MMMM d yyyy G");
    System.out.println(sdf.format(new Date()));
}

From source file:ChangeEra.java

public static void main(String s[]) {
    SimpleDateFormat sdf = new SimpleDateFormat();
    DateFormatSymbols dfs = sdf.getDateFormatSymbols();

    String era[] = { "BCE", "CE" };
    dfs.setEras(era);/*from w  w  w. ja v a 2 s.c o m*/
    sdf.setDateFormatSymbols(dfs);

    sdf.applyPattern("MMMM d yyyy G");
    System.out.println(sdf.format(new Date()));
}

From source file:net.sf.logsaw.dialect.log4j.pattern.Log4JConversionPatternTranslator.java

@Override
public void applyLocale(Locale loc, List<ConversionRule> rules) {
    for (ConversionRule rule : rules) {
        if (rule.getPlaceholderName().equals("d")) { //$NON-NLS-1$
            SimpleDateFormat df = rule.getProperty(PROP_DATEFORMAT, SimpleDateFormat.class);
            Assert.isNotNull(df, "dateFormat"); //$NON-NLS-1$
            df.setDateFormatSymbols(DateFormatSymbols.getInstance(loc));
            return;
        }//from ww w.j a v a2 s .com
    }
}

From source file:be.ibridge.kettle.trans.step.textfileinput.TextFileInput.java

public static final Value convertValue(String pol, String field_name, int field_type, String field_format,
        int field_length, int field_precision, String num_group, String num_decimal, String num_currency,
        String nullif, String ifNull, int trim_type, DecimalFormat ldf, DecimalFormatSymbols ldfs,
        SimpleDateFormat ldaf, DateFormatSymbols ldafs) throws Exception {
    Value value = new Value(field_name, field_type); // build a value!

    // If no nullif field is supplied, take the default.
    String null_value = nullif;/*from  w w  w .  j  a v  a  2  s . co m*/
    if (null_value == null) {
        switch (field_type) {
        case Value.VALUE_TYPE_BOOLEAN:
            null_value = Const.NULL_BOOLEAN;
            break;
        case Value.VALUE_TYPE_STRING:
            null_value = Const.NULL_STRING;
            break;
        case Value.VALUE_TYPE_BIGNUMBER:
            null_value = Const.NULL_BIGNUMBER;
            break;
        case Value.VALUE_TYPE_NUMBER:
            null_value = Const.NULL_NUMBER;
            break;
        case Value.VALUE_TYPE_INTEGER:
            null_value = Const.NULL_INTEGER;
            break;
        case Value.VALUE_TYPE_DATE:
            null_value = Const.NULL_DATE;
            break;
        case Value.VALUE_TYPE_BINARY:
            null_value = Const.NULL_BINARY;
            break;
        default:
            null_value = Const.NULL_NONE;
            break;
        }
    }
    String null_cmp = Const.rightPad(new StringBuffer(null_value), pol.length());

    if (pol == null || pol.length() == 0 || pol.equalsIgnoreCase(null_cmp)) {
        if (ifNull != null && ifNull.length() != 0)
            pol = ifNull;
    }

    if (pol == null || pol.length() == 0 || pol.equalsIgnoreCase(null_cmp)) {
        value.setNull();
    } else {
        if (value.isNumeric()) {
            try {
                StringBuffer strpol = new StringBuffer(pol);

                switch (trim_type) {
                case TextFileInputMeta.TYPE_TRIM_LEFT:
                    while (strpol.length() > 0 && strpol.charAt(0) == ' ')
                        strpol.deleteCharAt(0);
                    break;
                case TextFileInputMeta.TYPE_TRIM_RIGHT:
                    while (strpol.length() > 0 && strpol.charAt(strpol.length() - 1) == ' ')
                        strpol.deleteCharAt(strpol.length() - 1);
                    break;
                case TextFileInputMeta.TYPE_TRIM_BOTH:
                    while (strpol.length() > 0 && strpol.charAt(0) == ' ')
                        strpol.deleteCharAt(0);
                    while (strpol.length() > 0 && strpol.charAt(strpol.length() - 1) == ' ')
                        strpol.deleteCharAt(strpol.length() - 1);
                    break;
                default:
                    break;
                }
                pol = strpol.toString();

                if (value.isNumber()) {
                    if (field_format != null) {
                        ldf.applyPattern(field_format);

                        if (num_decimal != null && num_decimal.length() >= 1)
                            ldfs.setDecimalSeparator(num_decimal.charAt(0));
                        if (num_group != null && num_group.length() >= 1)
                            ldfs.setGroupingSeparator(num_group.charAt(0));
                        if (num_currency != null && num_currency.length() >= 1)
                            ldfs.setCurrencySymbol(num_currency);

                        ldf.setDecimalFormatSymbols(ldfs);
                    }

                    value.setValue(ldf.parse(pol).doubleValue());
                } else {
                    if (value.isInteger()) {
                        value.setValue(Long.parseLong(pol));
                    } else {
                        if (value.isBigNumber()) {
                            value.setValue(new BigDecimal(pol));
                        } else {
                            throw new KettleValueException("Unknown numeric type: contact vendor!");
                        }
                    }
                }
            } catch (Exception e) {
                throw (e);
            }
        } else {
            if (value.isString()) {
                value.setValue(pol);
                switch (trim_type) {
                case TextFileInputMeta.TYPE_TRIM_LEFT:
                    value.ltrim();
                    break;
                case TextFileInputMeta.TYPE_TRIM_RIGHT:
                    value.rtrim();
                    break;
                case TextFileInputMeta.TYPE_TRIM_BOTH:
                    value.trim();
                    break;
                default:
                    break;
                }
                if (pol.length() == 0)
                    value.setNull();
            } else {
                if (value.isDate()) {
                    try {
                        if (field_format != null) {
                            ldaf.applyPattern(field_format);
                            ldaf.setDateFormatSymbols(ldafs);
                        }

                        value.setValue(ldaf.parse(pol));
                    } catch (Exception e) {
                        throw (e);
                    }
                } else {
                    if (value.isBinary()) {
                        value.setValue(pol.getBytes());
                    }
                }
            }
        }
    }

    value.setLength(field_length, field_precision);

    return value;
}

From source file:org.glimpse.server.news.dom.DomServerNewsChannelBuilder.java

private List<ServerEntry> getRSSEntries(Document doc) throws Exception {
    XPath xpath = XPathFactory.newInstance().newXPath();
    List<ServerEntry> rssEntries = new LinkedList<ServerEntry>();
    NodeList entries = (NodeList) xpath.evaluate("/rss/channel/item", doc, XPathConstants.NODESET);
    for (int i = 0; i < entries.getLength(); i++) {
        ServerEntry rssEntry = new ServerEntry();
        Element elmEntry = (Element) entries.item(i);
        String id = (String) xpath.evaluate("link", elmEntry, XPathConstants.STRING);
        String title = (String) xpath.evaluate("title", elmEntry, XPathConstants.STRING);
        String entryUrl = (String) xpath.evaluate("link", elmEntry, XPathConstants.STRING);
        String pubDate = (String) xpath.evaluate("pubDate", elmEntry, XPathConstants.STRING);

        NodeList enclosureList = (NodeList) xpath.evaluate("enclosure", elmEntry, XPathConstants.NODESET);
        List<Enclosure> enclosures = new LinkedList<Enclosure>();
        for (int j = 0; j < enclosureList.getLength(); j++) {
            Element elmEnclosure = (Element) enclosureList.item(j);
            Enclosure enclosure = new Enclosure();
            enclosure.setUrl(elmEnclosure.getAttribute("url"));
            enclosure.setType(elmEnclosure.getAttribute("type"));
            enclosures.add(enclosure);/*from w ww . j  av  a  2 s .  co m*/
        }

        String content = (String) xpath.evaluate("description", elmEntry, XPathConstants.STRING);
        content += "<p><a href=\"" + entryUrl + "\" target=\"_blank\">Lire la suite</a></p>";

        if (StringUtils.isBlank(id)) {
            id = pubDate;
        }
        if (StringUtils.isBlank(entryUrl)) {
            entryUrl = getRSSLink(doc);
        }

        rssEntry.setId(id);
        rssEntry.setTitle(title);
        rssEntry.setUrl(entryUrl);
        rssEntry.setEnclosures(enclosures);
        rssEntry.setContent(content);

        if (StringUtils.isNotBlank(pubDate)) {
            SimpleDateFormat formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");
            formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
            formatter.setDateFormatSymbols(new DateFormatSymbols(Locale.ENGLISH));
            try {
                Date date = formatter.parse(pubDate);
                rssEntry.setDate(date);
            } catch (Exception e) {
                // unparsable date
                logger.warn("Unable to parse date <" + pubDate + ">", e);
            }
        }

        if (StringUtils.isNotBlank(id) && StringUtils.isNotBlank(title) && StringUtils.isNotBlank(entryUrl)) {
            rssEntries.add(rssEntry);
        }
    }

    return rssEntries;
}

From source file:org.glimpse.server.news.sax.ChannelHandler.java

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
    if (Type.ATOM.equals(type)) {
        String path = getCurrentPath();
        if (path.equals("/feed/title")) {
            title = buffer.toString();// w w w  .  ja  va 2 s.c  o  m
        } else if (path.equals("/feed/entry")) {
            String content = currentContent;
            if (StringUtils.isBlank(content)) {
                content = currentSummary == null ? "" : currentSummary;
                content += "<p><a href=\"" + currentEntry.getUrl()
                        + "\" target=\"_blank\">Lire la suite</a></p>";
            }
            currentEntry.setContent(content);

            if (StringUtils.isNotBlank(currentEntry.getId()) && StringUtils.isNotBlank(currentEntry.getTitle())
                    && StringUtils.isNotBlank(currentEntry.getUrl())) {
                entries.add(currentEntry);
            }

            currentEntry = null;
            currentContent = null;
            currentSummary = null;
        } else if (path.equals("/feed/entry/id")) {
            currentEntry.setId(buffer.toString());
        } else if (path.equals("/feed/entry/title")) {
            currentEntry.setTitle(buffer.toString());
        } else if (path.equals("/feed/entry/updated")) {
            String updated = buffer.toString();
            if (StringUtils.length(updated) > 19) {
                try {
                    Date date = null;
                    if (updated.endsWith("Z")) {
                        // No timezone
                        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
                        formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
                        date = formatter.parse(updated.substring(0, 19));
                    } else {
                        String timezone = updated.substring(updated.length() - 6);
                        timezone = timezone.substring(0, 3) + timezone.substring(4);
                        updated = updated.substring(0, 19) + timezone;
                        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
                        formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
                        date = formatter.parse(updated);
                    }
                    if (date != null) {
                        currentEntry.setDate(date);
                    }
                } catch (Exception e) {
                    // unparsable date
                    logger.warn("Unable to parse date <" + updated + ">", e);
                }
            }
        } else if (path.equals("/feed/entry/content")) {
            currentContent = buffer.toString();
        } else if (path.equals("/feed/entry/summary")) {
            currentSummary = buffer.toString();
        }
    } else if (Type.RSS_2_0.equals(type)) {
        String path = getCurrentPath();
        if (path.equals("/rss/channel/title")) {
            title = buffer.toString();
        } else if (path.equals("/rss/channel/link")) {
            url = buffer.toString();
        } else if (path.equals("/rss/channel/item")) {
            String content = currentSummary == null ? "" : currentSummary;
            content += "<p><a href=\"" + currentEntry.getUrl() + "\" target=\"_blank\">Lire la suite</a></p>";
            currentEntry.setContent(content);

            if (StringUtils.isBlank(currentEntry.getId()) && currentEntry.getDate() != null) {
                currentEntry.setId(currentEntry.getDate().toString());
            }

            if (StringUtils.isBlank(currentEntry.getUrl())) {
                currentEntry.setUrl(url);
            }

            if (StringUtils.isNotBlank(currentEntry.getId()) && StringUtils.isNotBlank(currentEntry.getTitle())
                    && StringUtils.isNotBlank(currentEntry.getUrl())) {
                entries.add(currentEntry);
            }

            currentEntry = null;
            currentContent = null;
            currentSummary = null;
        } else if (path.equals("/rss/channel/item/link")) {
            currentEntry.setId(buffer.toString());
            currentEntry.setUrl(buffer.toString());
        } else if (path.equals("/rss/channel/item/title")) {
            currentEntry.setTitle(buffer.toString());
        } else if (path.equals("/rss/channel/item/pubDate")) {
            String pubDate = buffer.toString();
            if (StringUtils.isNotBlank(pubDate)) {
                SimpleDateFormat formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");
                formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
                formatter.setDateFormatSymbols(new DateFormatSymbols(Locale.ENGLISH));
                try {
                    Date date = formatter.parse(pubDate);
                    currentEntry.setDate(date);
                } catch (Exception e) {
                    // unparsable date
                    logger.debug("Unable to parse date <" + pubDate + ">", e);
                }
            }
        } else if (path.equals("/rss/channel/item/description")) {
            currentSummary = buffer.toString();
        } else if (path.equals("/rss/channel/item/enclosure/url")) {
            currentEnclosure.setUrl(buffer.toString());
        } else if (path.equals("/rss/channel/item/enclosure/type")) {
            currentEnclosure.setType(buffer.toString());
        }
    } else if (Type.RSS_1_0.equals(type)) {
        String path = getCurrentPath();
        if (path.equals("/rdf:RDF/channel/title")) {
            title = buffer.toString();
        } else if (path.equals("/rdf:RDF/channel/link")) {
            url = buffer.toString();
        } else if (path.equals("/rdf:RDF/item")) {
            String content = currentSummary == null ? "" : currentSummary;
            content += "<p><a href=\"" + currentEntry.getUrl() + "\" target=\"_blank\">Lire la suite</a></p>";
            currentEntry.setContent(content);

            if (StringUtils.isBlank(currentEntry.getUrl())) {
                currentEntry.setUrl(url);
            }

            if (StringUtils.isNotBlank(currentEntry.getId()) && StringUtils.isNotBlank(currentEntry.getTitle())
                    && StringUtils.isNotBlank(currentEntry.getUrl())) {
                entries.add(currentEntry);
            }

            currentEntry = null;
            currentContent = null;
            currentSummary = null;
        } else if (path.equals("/rdf:RDF/item/title")) {
            currentEntry.setTitle(buffer.toString());
        } else if (path.equals("/rdf:RDF/item/link")) {
            currentEntry.setId(buffer.toString());
            currentEntry.setUrl(buffer.toString());
        } else if (path.equals("/rdf:RDF/item/description")) {
            currentSummary = buffer.toString();
        } else if (path.equals("/rdf:RDF/item/dc:date")) {
            String updated = buffer.toString();
            if (StringUtils.length(updated) > 19) {
                try {
                    Date date = null;
                    if (updated.endsWith("Z")) {
                        // No timezone
                        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
                        formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
                        date = formatter.parse(updated.substring(0, 19));
                    } else {
                        String timezone = updated.substring(updated.length() - 6);
                        timezone = timezone.substring(0, 3) + timezone.substring(4);
                        updated = updated.substring(0, 19) + timezone;
                        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
                        formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
                        date = formatter.parse(updated);
                    }
                    if (date != null) {
                        currentEntry.setDate(date);
                    }
                } catch (Exception e) {
                    // unparsable date
                    logger.warn("Unable to parse date <" + updated + ">", e);
                }
            }
        }
    }

    String s = elementsStack.remove(elementsStack.size() - 1);
    assert s.equals(qName);
}