Example usage for org.joda.time.format ISODateTimeFormat dateParser

List of usage examples for org.joda.time.format ISODateTimeFormat dateParser

Introduction

In this page you can find the example usage for org.joda.time.format ISODateTimeFormat dateParser.

Prototype

public static DateTimeFormatter dateParser() 

Source Link

Document

Returns a generic ISO date parser for parsing dates with a possible zone.

Usage

From source file:annis.gui.converter.DateTimeStringConverter.java

License:Apache License

@Override
public DateTime convertToModel(String value, Class<? extends DateTime> targetType, Locale locale)
        throws ConversionException {
    if (value == null) {
        return null;
    }//from w  w w . j a va 2  s . c  o m
    return ISODateTimeFormat.dateParser().parseDateTime(value);
}

From source file:de.berlios.jedi.common.jisp.JispStoredToJispPackage.java

License:Open Source License

/**
 * Sets the metadata in a JispPackage from the icondef element in the
 * icondef.xml file.<br>//from  www .  j av  a 2  s  .  c om
 * If creation date in icondef.xml can't be parsed as ISO 8601, a new date
 * representing the time at which it was allocated is used.
 * 
 * @param jispPackage
 *            The JispPackage to set metadata in.
 * @param icondefElement
 *            The icondef JDOM Element to get the data from.
 */
private void setMetadata(JispPackage jispPackage, Element icondefElement) {
    JispMetadata jispMetadata = new JispMetadata();
    Element metaElement = icondefElement.getChild("meta");

    jispMetadata.setName(metaElement.getChildText("name"));
    jispMetadata.setVersion(metaElement.getChildText("version"));
    jispMetadata.setDescription(metaElement.getChildText("description"));
    setMetadataAuthors(jispMetadata, metaElement);
    try {
        jispMetadata.setCreation(
                new Date(ISODateTimeFormat.dateParser().parseMillis(metaElement.getChildText("creation"))));
    } catch (IllegalArgumentException e) {
        LogFactory.getLog(JispStoredToJispPackage.class)
                .error("Icondef.xml creation date can not be parsed as ISO 8601", e);
        jispMetadata.setCreation(new Date());
    }
    if (metaElement.getChildText("home") != null)
        jispMetadata.setHome(metaElement.getChildText("home"));

    jispPackage.setJispMetadata(jispMetadata);
}

From source file:nz.net.ultraq.jaxb.adapters.XMLLocalDateAdapter.java

License:Apache License

/**
 * Converts any ISO8601 date/time string into a Joda DateTime object.
 * //from  www .j  av a  2s .  co  m
 * @param value
 * @return Joda DateTime.
 */
@Override
public LocalDate unmarshal(String value) {
    return value == null ? null
            : new DateTimeFormatterBuilder()
                    .append(ISODateTimeFormat.dateParser()).appendOptional(new DateTimeFormatterBuilder()
                            .appendTimeZoneOffset("Z", true, 2, 4).toFormatter().getParser())
                    .toFormatter().parseLocalDate(value);
}

From source file:org.aksw.simba.bengal.triple2nl.converter.LiteralConverter.java

License:Apache License

private String convertDateLiteral(LiteralLabel lit) {
    RDFDatatype dt = lit.getDatatype();//from  w w  w.j a  va 2 s. c  om
    String s = lit.getLexicalForm();

    try {
        Object value = lit.getValue();
        if (value instanceof XSDDateTime) {
            Calendar calendar = ((XSDDateTime) value).asCalendar();
            if (dt.equals(XSDDatatype.XSDgMonth)) {
                s = calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, ENGLISH_LOCAL);
            } else if (dt.equals(XSDDatatype.XSDgMonthDay)) {
                s = calendar.get(Calendar.DAY_OF_MONTH)
                        + calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, ENGLISH_LOCAL);
            } else if (dt.equals(XSDDatatype.XSDgYearMonth)) {
                s = calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, ENGLISH_LOCAL) + " "
                        + calendar.get(Calendar.YEAR);
            } else if (dt.equals(XSDDatatype.XSDgYear)) {
                s = "" + calendar.get(Calendar.YEAR);
            } else {
                s = calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, ENGLISH_LOCAL) + " "
                        + calendar.get(Calendar.DAY_OF_MONTH) + ", " + calendar.get(Calendar.YEAR);
                // dateFormat.format(calendar.getTime());
            }
        }
    } catch (DatatypeFormatException | IllegalDateTimeFieldException e) {
        logger.error("Conversion of date literal " + lit + " failed. Reason: " + e.getMessage());
        // fallback
        // DateTime time = ISODateTimeFormat.dateTime
        DateTime time;
        try {
            time = ISODateTimeFormat.dateTimeParser().parseDateTime(lit.getLexicalForm());
            s = time.toString("MMMM dd, yyyy");
        } catch (Exception e1) {
            try {
                time = ISODateTimeFormat.localDateParser().parseDateTime(lit.getLexicalForm());
                s = time.toString("MMMM dd, yyyy");
            } catch (Exception e2) {
                e2.printStackTrace();
                time = ISODateTimeFormat.dateParser().parseDateTime(lit.getLexicalForm());
                s = time.toString("MMMM dd, yyyy");
            }
        }
    }
    return s;
}

From source file:org.ddialliance.ddiftp.util.Translator.java

License:Open Source License

/**
 * Format an ISO8601 time string defined by into a Calendar<BR>
 * Defined by '-'? yyyy '-' mm '-' dd 'T' hh ':' mm ':' ss ('.' s+)?
 * (zzzzzz)?//from  w w w  .j  a  v  a 2s. c om
 * 
 * @see http://www.w3.org/TR/xmlschema-2/#dateTime
 * @param time
 *            string
 * @return calendar
 * @throws DDIFtpException
 */
public static Calendar formatIso8601DateTime(String time) throws DDIFtpException {
    // yyyy-MM-dd'T'HH:mm:ss.SSSZZ full format
    DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
    try {
        DateTime dateTime = fmt.parseDateTime(time);
        return dateTime.toCalendar(getLocale());
    } catch (IllegalArgumentException e) {
        try {
            // yyyy-MM-dd'T'HH:mm:ssZZ with out millisecond
            fmt = ISODateTimeFormat.dateTimeNoMillis();
            fmt.withLocale(getLocale());
            fmt.withZone(DateTimeZone.forTimeZone(getTimeZone()));
            DateTime dateTime = fmt.parseDateTime(time);
            return dateTime.toCalendar(getLocale());
        } catch (IllegalArgumentException e1) {
            try {
                // yyyy-MM-dd'T'HH:mm:ss.SS with out time zone
                fmt = ISODateTimeFormat.dateHourMinuteSecondFraction();
                fmt.withLocale(getLocale());
                fmt.withZone(DateTimeZone.forTimeZone(getTimeZone()));
                DateTime dateTime = fmt.parseDateTime(time);
                return dateTime.toCalendar(getLocale());
            } catch (Exception e2) {
                try {
                    // yyyy-MM-dd'T'HH:mm:ss with out millisecond and time
                    // zone
                    fmt = ISODateTimeFormat.dateHourMinuteSecond();
                    fmt.withLocale(getLocale());
                    fmt.withZone(DateTimeZone.forTimeZone(getTimeZone()));
                    DateTime dateTime = fmt.parseDateTime(time);
                    return dateTime.toCalendar(getLocale());
                } catch (IllegalArgumentException e3) {
                    try {
                        fmt = ISODateTimeFormat.dateParser();
                        fmt.withLocale(getLocale());
                        fmt.withZone(DateTimeZone.forTimeZone(getTimeZone()));
                        DateTime dateTime = fmt.parseDateTime(time);
                        return dateTime.toCalendar(getLocale());
                    } catch (Exception e4) {
                        throw new DDIFtpException("translate.timeformat.error",
                                new Object[] { time,
                                        "'-'? yyyy '-' mm '-' dd 'T' hh ':' mm ':' ss ('.' s+)? (zzzzzz)?" },
                                e);
                    }
                }
            }
        }
    }
}