Parse string in MM/dd/yyyy format to Date and return as long value - Android java.util

Android examples for java.util:Date Parse

Description

Parse string in MM/dd/yyyy format to Date and return as long value

Demo Code

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main{
    /** The default formatter for short date */
    private static final DateFormat SHORT_DATE_FORMATTER = new SimpleDateFormat(
            "MM/dd/yyyy");


    public static long getFormatDate(String dateStr) {
        if (dateStr == null)
            return 0;
        Date date = null;//from w  w  w .j  a  v  a2  s .  c  o m
        try {
            date = SHORT_DATE_FORMATTER.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long timestamp = 0;
        if (date != null)
            timestamp = date.getTime();
        return timestamp;
    }

}

Related Tutorials