Parse a date/time string of the specified format into a Date object - Android java.util

Android examples for java.util:Time

Description

Parse a date/time string of the specified format into a Date object

Demo Code


import android.util.Log;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class Main{
    private static final String TAG = TimeUtils.class.getName();
    /**// w  ww.j ava  2  s  .  co  m
     * Parse a date/time string of the specified format into a Date object
     *
     * @param timeString string to be parsed
     * @param format format of string to be parsed
     * @return a Date based on parsed string
     */
    public static Date parseStringToDate(String timeString, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
        try {
            return sdf.parse(timeString);
        } catch (ParseException e) {
            Log.e(TAG, "Could not parse date string", e);
            return null;
        }

    }
}

Related Tutorials