Transform a SQL date string from format %y-%M-%d %H:%M:%S to a Date object - Android java.util

Android examples for java.util:Date Convert

Description

Transform a SQL date string from format %y-%M-%d %H:%M:%S to a Date object

Demo Code

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Main{
private static final String SDF_FORMAT = "dd-MM-yyyy HH:mm:ss";
    /**/*from   www.  jav  a  2  s . co m*/
     * Transform a SQL date string from format %y-%M-%d %H:%M:%S to a Date object 
     * @param date
     * @return a Date object or throw a ParseException
     */
    public static Date StringSQLToDate(String date) throws ParseException {
        if (date == null || "".equals(date))
            throw new ParseException("Date " + date
                    + " is impossible to parse", 0);

        SimpleDateFormat sdf = new SimpleDateFormat(
                SDF_FORMAT_TO_DATE);
        return sdf.parse(date);
    }

}

Related Tutorials