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

Android examples for java.util:Date Parse

Description

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

Demo Code

import android.content.Context;
import android.text.format.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class Main{
    public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
    public static Date toDate(String strDate) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
        try {//w w  w.j a v a 2 s. co m
            return sdf.parse(strDate);
        } catch (ParseException ex) {
            return cal.getTime();
        }
    }

}

Related Tutorials