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;// w w w .  j  a  v a 2 s  .  co 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