Parse string to Date in yyyy-MM-dd HH:mm:ss format - Android java.util

Android examples for java.util:Date Parse

Description

Parse string to Date in yyyy-MM-dd HH:mm:ss format

Demo Code

import android.annotation.SuppressLint;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main{

    public static Date getSimpleDate(String date) {
        Date result = null;//from w  w w .j  a  v a  2s. co m
        if (null != date && !"".equals(date)) {
            try {
                result = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                        .parse(date);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return null;
            }
        }
        return result;
    }
    public static Date getSimpleDate(String date, String format) {
        Date result = null;
        if (null != date && !"".equals(date)) {
            try {
                result = new SimpleDateFormat(format).parse(date);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return null;
            }
        }
        return result;
    }

}

Related Tutorials