Android XmlPullParser Read readDate(XmlPullParser parser, String tagName, String dateFormat)

Here you can find the source of readDate(XmlPullParser parser, String tagName, String dateFormat)

Description

Reads a date from the next xml tag

Parameter

Parameter Description
parser The parser to read the date from
tagName the name of the tag that contains the date
dateFormat The date format see SimpleDateFormat for formatting options

Exception

Parameter Description
IOException an exception
XmlPullParserException an exception

Return

or null on parseError

Declaration

public static Date readDate(XmlPullParser parser, String tagName,
        String dateFormat) throws IOException, XmlPullParserException 

Method Source Code

//package com.java2s;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Main {
    public static final String NAMESPACE = null;

    /**//from  w ww .j av  a2  s  . c  o  m
     * Reads a date from the next xml tag
     *
     * @param parser     The parser to read the date from
     * @param tagName    the name of the tag that contains the date
     * @param dateFormat The date format see {@link SimpleDateFormat} for formatting options
     * @return {@link Date} or null on parseError
     * @throws IOException
     * @throws XmlPullParserException
     */
    public static Date readDate(XmlPullParser parser, String tagName,
            String dateFormat) throws IOException, XmlPullParserException {

        parser.require(XmlPullParser.START_TAG, NAMESPACE, tagName);
        Date date = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
                dateFormat, Locale.ENGLISH);
        if (parser.next() == XmlPullParser.TEXT) {
            try {
                date = simpleDateFormat.parse(parser.getText());
            } catch (ParseException e) {
                return null;
            }
            parser.next();
        }
        parser.require(XmlPullParser.END_TAG, NAMESPACE, tagName);
        return date;
    }
}

Related

  1. readBool(XmlPullParser parser, String tagName)
  2. readBooleanAttribute(XmlPullParser in, String name)
  3. readBooleanAttribute(XmlPullParser in, String name)
  4. readBooleanAttribute(XmlPullParser in, String name, boolean defaultValue)
  5. readByteArrayAttribute(XmlPullParser in, String name)
  6. readFloatAttribute(XmlPullParser in, String name)
  7. readInt(XmlPullParser parser)
  8. readIntAttribute(XmlPullParser in, String name)
  9. readIntAttribute(XmlPullParser in, String name)