Example usage for java.text DateFormat parse

List of usage examples for java.text DateFormat parse

Introduction

In this page you can find the example usage for java.text DateFormat parse.

Prototype

public Date parse(String source) throws ParseException 

Source Link

Document

Parses text from the beginning of the given string to produce a date.

Usage

From source file:com.evozon.evoportal.my_account.util.MyAccountUtil.java

public static Date convertStringToDate(String dateInput, String pattern, Locale locale) throws ParseException {
    Date stringToDate = null;//from   w  ww  .  j  a  v  a  2s .c om

    if (!dateInput.isEmpty() && !pattern.isEmpty()) {
        DateFormat df = new SimpleDateFormat(pattern, locale);
        stringToDate = df.parse(dateInput);
    }

    return stringToDate;
}

From source file:com.taobao.adfs.database.tdhsocket.client.util.ConvertUtil.java

public static Long getTimeFromString(String stringVal, @Nullable Calendar cal) throws SQLException {
    if (stringVal == null) {
        return null;
    }/*from ww  w  .  ja va 2s . c  om*/
    String val = stringVal.trim();
    if (val.length() == 0) {
        return null;
    }
    if (val.equals("0") || val.equals("0000-00-00") || val.equals("0000-00-00 00:00:00")
            || val.equals("00000000000000") || val.equals("0")) {
        Calendar calendar = null;
        if (cal != null) {
            calendar = Calendar.getInstance(cal.getTimeZone());
        } else {
            calendar = Calendar.getInstance();
        }
        calendar.set(Calendar.YEAR, 1);
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTimeInMillis();
    }

    DateFormat dateFormat = DateFormat.getDateTimeInstance();
    if (cal != null) {
        TimeZone timeZone = cal.getTimeZone();
        dateFormat.setTimeZone(timeZone);
    }
    try {
        return dateFormat.parse(val).getTime();
    } catch (ParseException e) {
        throw new SQLException("Parse date failure:" + val);
    }
}

From source file:eu.e43.impeller.Utils.java

public static long parseDate(String date) {
    if (date == null)
        return new Date().getTime();

    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    df.setTimeZone(TimeZone.getTimeZone("Zulu"));
    try {// ww  w .  j a  v a 2 s.c  o m
        return df.parse(date).getTime();
    } catch (ParseException e) {
        return new Date().getTime();
    }
}

From source file:hu.javaforum.commons.ReflectionHelper.java

/**
 * Converts String to Date.//from w w w. j a va  2  s  .c om
 *
 * @param stringValue The string value
 * @return The Date instance
 * @throws ParseException If the string is not parseable
 */
private static Date stringToDate(final String stringValue) throws ParseException {
    try {
        String pattern = "yyyy-MM-dd'T'HH:mm:ssZ";
        for (Map.Entry<String, String> entry : DATE_FORMAT_PATTERNS.entrySet()) {
            if (stringValue.matches(entry.getKey())) {
                pattern = entry.getValue();
                break;
            }
        }

        DateFormat format = new SimpleDateFormat(pattern, Locale.ENGLISH);
        return format.parse(stringValue);
    } catch (ParseException except) {
        throw except;
    }
}

From source file:microsoft.exchange.webservices.data.misc.MapiTypeConverter.java

private static Object parseDateTime(String s) {
    String utcPattern = "yyyy-MM-dd'T'HH:mm:ss'Z'";
    String errMsg = String.format("Date String %s not in " + "valid UTC/local format", s);
    DateFormat utcFormatter = new SimpleDateFormat(utcPattern);
    Date dt;//from w w  w. j  ava 2 s.  com

    if (s.endsWith("Z")) {
        try {
            dt = utcFormatter.parse(s);
        } catch (ParseException e) {
            s = s.substring(0, 10) + "T12:00:00Z";
            try {
                dt = utcFormatter.parse(s);
            } catch (ParseException e1) {
                LOG.error(e);
                throw new IllegalArgumentException(errMsg, e);
            }
        }
    } else if (s.endsWith("z")) {
        // String in UTC format yyyy-MM-ddTHH:mm:ssZ
        utcFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'z'");
        try {
            dt = utcFormatter.parse(s);
        } catch (ParseException e) {
            throw new IllegalArgumentException(e);
        }
    } else {
        utcFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        try {
            dt = utcFormatter.parse(s);
        } catch (ParseException e) {
            throw new IllegalArgumentException(e);
        }
    }
    return dt;
}

From source file:eu.smartfp7.terrier.sensor.ParserUtility.java

public static EdgeNodeSnapShot parse(InputStream is) throws Exception {
    DocumentBuilderFactory xmlfact = DocumentBuilderFactory.newInstance();
    xmlfact.setNamespaceAware(true);//  w  w  w.  ja  va 2  s  . c  o  m
    Document document = xmlfact.newDocumentBuilder().parse(is);

    NamespaceContext ctx = new NamespaceContext() {

        @Override
        public Iterator getPrefixes(String namespaceURI) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public String getPrefix(String namespaceURI) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public String getNamespaceURI(String prefix) {

            String uri = null;

            /*
             *    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
            xmlns:smart="http://www.ait.gr/ait_web_site/faculty/apne/schema.xml#"
            xmlns:dc="http://purl.org/dc/elements/1.1/"
            xmlns:contact="http://www.w3.org/2000/10/swap/pim/contact#"
            xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
            xmlns:time="http://www.w3.org/2006/time#"
            xml:base="http://www.ait.gr/ait_web_site/faculty/apne/report.xml"
                    
            *
            */

            if (prefix.equals("rdf")) {
                uri = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
            }
            if (prefix.equals("smart")) {
                uri = "http://www.ait.gr/ait_web_site/faculty/apne/schema.xml#";
            }
            if (prefix.equals("dc")) {
                uri = "http://purl.org/dc/elements/1.1/#";
            }
            if (prefix.equals("geo")) {
                uri = "http://www.w3.org/2003/01/geo/wgs84_pos#";
            }
            if (prefix.equals("time")) {
                uri = "http://www.w3.org/2006/time#";
            }
            return uri;

        }
    };

    // find the node data

    XPath xpath = XPathFactory.newInstance().newXPath();
    xpath.setNamespaceContext(ctx);

    String id = (String) xpath.compile("//smart:Node/@rdf:ID").evaluate(document, XPathConstants.STRING);
    String lat = (String) xpath.compile("//smart:Node/geo:lat/text()").evaluate(document,
            XPathConstants.STRING);
    String lon = (String) xpath.compile("//smart:Node/geo:long/text()").evaluate(document,
            XPathConstants.STRING);
    String fullName = (String) xpath.compile("//smart:Node/dc:fullName/text()").evaluate(document,
            XPathConstants.STRING);
    Node node = new Node(new Double(lat), new Double(lon), id, fullName);

    String time = (String) xpath.compile("//smart:Report/time:inXSDDateTime/text()").evaluate(document,
            XPathConstants.STRING);
    String reportId = (String) xpath.compile("//smart:Report/@rdf:ID").evaluate(document,
            XPathConstants.STRING);

    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    Calendar c = Calendar.getInstance();
    ;
    c.setTime(df.parse(time));

    String density = (String) xpath.compile("//smart:Crowd/smart:density/text()").evaluate(document,
            XPathConstants.STRING);
    String cameraGain = (String) xpath.compile("//smart:Crowd/smart:cameraGain/text()").evaluate(document,
            XPathConstants.STRING);

    NodeList list = (NodeList) xpath.compile("//smart:Crowd/smart:colour").evaluate(document,
            XPathConstants.NODESET);
    double[] colors = new double[list.getLength()];
    for (int i = 0; i < list.getLength(); i++) {
        org.w3c.dom.Node colorNode = list.item(i);
        String v = colorNode.getFirstChild().getTextContent();
        colors[i] = new Double(v);
    }

    CrowdReport crowdReport = new CrowdReport(reportId, new Double(density), new Double(cameraGain), colors);

    EdgeNodeSnapShot snapShot = new EdgeNodeSnapShot(node, id, c, crowdReport);

    return snapShot;
}

From source file:hu.javaforum.util.internal.ReflectionHelper.java

/**
 * Converts String to Date.//from w  w  w.  ja  v  a2s . c  o m
 *
 * @param stringValue The string value
 * @return The Date instance
 * @throws ParseException If the string is not parseable
 */
private static Date stringToDate(final String stringValue) throws ParseException {
    PerfLogger logger = new PerfLogger(LOGGER);

    try {
        String pattern = "yyyy-MM-dd'T'HH:mm:ssZ";
        for (Map.Entry<String, String> entry : DATE_FORMAT_PATTERNS.entrySet()) {
            if (stringValue.matches(entry.getKey())) {
                pattern = entry.getValue();
                break;
            }
        }

        DateFormat format = new SimpleDateFormat(pattern, Locale.ENGLISH);
        return format.parse(stringValue);
    } catch (ParseException except) {
        logger.warn("Unsupported date format '%1$s'", stringValue);
        throw except;
    }
}

From source file:edu.monash.merc.util.DMUtil.java

public static Date formatDate(final String dateStr) {
    Date date = null;//from  ww  w  .  j a va 2 s.c o  m
    DateFormat df = new SimpleDateFormat(DATE_FORMAT);
    try {
        date = df.parse(dateStr);
    } catch (ParseException e) {
        throw new DMException(e.getMessage());
    }
    return date;
}

From source file:com.ushahidi.android.app.util.Util.java

/**
 * Format date into more readable format.
 * //  www  .j  a  v  a  2  s.  co  m
 * @param date - the date to be formatted.
 * @return String
 */
public static String formatDate(String dateFormat, String date, String toFormat) {

    String formatted = "";

    DateFormat formatter = new SimpleDateFormat(dateFormat);
    try {
        Date dateStr = formatter.parse(date);
        formatted = formatter.format(dateStr);
        Date formatDate = formatter.parse(formatted);
        formatter = new SimpleDateFormat(toFormat);
        formatted = formatter.format(formatDate);

    } catch (ParseException e) {

        e.printStackTrace();
    }
    return formatted;
}

From source file:edu.monash.merc.util.DMUtil.java

public static Date formatYMDDate(final String dateStr) {
    Date date = null;/*w ww  .  ja v  a2 s  .co  m*/
    DateFormat df = new SimpleDateFormat(DATE_YYYYMMDD_FORMAT);
    try {
        date = df.parse(dateStr);
    } catch (ParseException e) {
        throw new DMException(e.getMessage());
    }
    return date;
}