Parses date string and converts to java.util.Date object with provided java.text.DateFormat - Android java.util

Android examples for java.util:Date Parse

Description

Parses date string and converts to java.util.Date object with provided java.text.DateFormat

Demo Code

import android.util.Log;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;

public class Main{

    /**//from   w w w.j  av a  2  s. c o m
     * Parses date string and converts to {@link java.util.Date} object with provided
     * {@link java.text.DateFormat}
     *
     * @param dateString
     *            date string
     * @param dateFormat
     *            date format used to parse date
     * @return {@link java.util.Date} representation of date string or null if parsing
     *         fails
     */
    public static Date toDate(String dateString, final DateFormat dateFormat) {
        Date date = null;
        if (dateString == null) {
            return null;
        }

        try {
            date = dateFormat.parse(dateString);
        } catch (ParseException ex) {
            Log.e(TAG, "Incorrect date format=" + dateString);
        }
        return date;
    }

}

Related Tutorials