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

Android examples for java.util:Date Parse

Description

Parse string To Timestamp 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;
import java.util.Locale;

public class Main{

    public static long strToTimestamp(String date) {
        return strToTimestamp(date, "yyyy-MM-dd HH:mm:ss");
    }//from   w  ww . j a  v a  2 s  . c o  m

    public static long strToTimestamp(String date, String format) {
        long timestamp = 0;
        try {
            timestamp = new SimpleDateFormat(format).parse(date).getTime() / 1000;

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return timestamp;
    }

}

Related Tutorials