convert String To Date - Android java.util

Android examples for java.util:Date Convert

Description

convert String To Date

Demo Code

import android.content.Context;
import android.provider.Settings;
import android.telephony.PhoneNumberUtils;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{
    public static final String SERVER_RACE_MEETING_DATE_FORMAT = "yyyy-MM-dd";
    /**/*from  w  w w  .  j a  va  2  s  . co m*/
     * Convert a date string to a date
     * 
     * @param time
     * @return
     */
    public static Date convertStringToDate(String time) {
        SimpleDateFormat dateformat = new SimpleDateFormat(
                SERVER_RACE_MEETING_DATE_FORMAT);
        Date date = null;
        try {
            date = dateformat.parse(time);
            return date;
        } catch (ParseException e) {
            return new Date(System.currentTimeMillis());
        }
    }
    public static Date convertStringToDate(String format, String time) {
        SimpleDateFormat dateformat = new SimpleDateFormat(format);
        Date date = null;
        try {
            date = dateformat.parse(time);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

}

Related Tutorials