Convert date String in yyyy-MM-dd HH:mm:ss format to Long - Android java.util

Android examples for java.util:Date Convert

Description

Convert date String in yyyy-MM-dd HH:mm:ss format to Long

Demo Code

import android.annotation.SuppressLint;
import android.widget.TextView;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main{


    public static long dateStr2Long(String dateStr) {
        try {//w  w w  .j  a va 2 s  .  c  o  m
            Calendar c = Calendar.getInstance();
            c.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                    .parse(dateStr));
            return c.getTimeInMillis();
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }

    }

}

Related Tutorials